-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathpgTable.ahk
148 lines (139 loc) · 5.41 KB
/
pgTable.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* pgTable.ahk
Version: 1.1
Last time modified: 2018.10.23 14:48
Description: a script to draw a pseudo-graphical borders to the copied table.
Script author: Drugoy a.k.a. Drugmix
Contacts: [email protected], [email protected]
https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/pgTable/pgTable.ahk
*/
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode, Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir, %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance, Force
;{ Settings
;{ Available border styles
borderThin := "─│┌┬┐├┼┤└┴┘─│"
borderThick := "━┃┏┳┓┣╋┫┗┻┛━┃"
borderThickThin := "━┃┏┯┓┠┼┨┗┷┛─│"
borderDouble := "═║╔╦╗╠╬╣╚╩╝═║"
borderDoubleSingle := "═║╔╤╗╟┼╢╚╧╝─│"
border21 := "─║╓╥╖╟╫╢╙╨╜─║"
border12 := "═│╒╤╕╞╪╡╘╧╛═│"
;}
border := borderThickThin ; Specify the choice of border style here
useTopBorder := 1 ; Choose whether to add a top border to the table
useRowSeparatingBorders := 1 ; Choose whether to use horizontal borders between rows
useBottomBorder := 1 ; Choose whether to add a bottom border to the table
horizontalCellPadding := 0 ; Choose the number of spaces to be added as horizontal paddings (to the left and to the right from each cell's value)
textAlign := 0 ; Choose how to align text in cells: left/center/right = -1/0/1
;}
;}
F12::
;{ Reading table from clipboard
input := Trim(Clipboard, " `t`r`n")
;}
;{ Convert the input table into an array
inputArray := []
Loop, Parse, input, `n ; Per line parsing of input.
{
rows := A_Index ; At the end of loop, variable "rows" will contain the number of rows.
Loop, Parse, A_LoopField, `t
inputArray[A_Index, rows] := RTrim(A_LoopField, "`r`n") ; Have to cut away `r`n from the right edge of cells, since every row's last cell has them.
}
;}
;{ Transform a table into a text table with pseudo-graphics
columns := inputArray.MaxIndex()
topBorder := bottomBorder := output := rowSeparator := ""
Loop, % rows
{
thisRow := A_Index
Loop, % columns
{
thisColMaxWidth := getColMaxWidth(A_Index)
thisCellWidth := StrLen(inputArray[A_Index, thisRow])
If (thisRow == 1) ; While parsing first row, the script (based on the specified settings) decides whether to build table's top border, bottom border and row separating horizontal borders.
{
;{ Build the table's top border.
If (useTopBorder)
{
If (A_Index == 1) ; Parsing leftmost cell.
topBorder := SubStr(border, 3, 1) ; ┌
Loop, % thisColMaxWidth + 2 * horizontalCellPadding
topBorder .= SubStr(border, 1, 1) ; ─
If (A_Index != columns)
topBorder .= SubStr(border, 4, 1) ; ┬
Else ; Parsing rightmost cell.
topBorder .= SubStr(border, 5, 1) "`n" ; ┐
}
;}
;{ Build the table's rows horizontal separator.
If (useRowSeparatingBorders)
{
If (A_Index == 1)
rowSeparator .= SubStr(border, 6, 1) ; ├
Loop, % thisColMaxWidth + 2 * horizontalCellPadding
rowSeparator .= SubStr(border, 12, 1) ; ─
If (A_Index != columns)
rowSeparator .= SubStr(border, 7, 1) ; ┼
Else ; Parsing rightmost cell.
rowSeparator .= SubStr(border, 8, 1) "`n" ; ┤
}
;}
;{ Build the table's bottommost line.
If (useBottomBorder)
{
If (A_Index == 1) ; Parsing leftmost cell.
bottomBorder := "`n" SubStr(border, 9, 1) ; └
Loop, % thisColMaxWidth + 2 * horizontalCellPadding
bottomBorder .= SubStr(border, 1, 1) ; ─
If (A_Index != columns)
bottomBorder .= SubStr(border, 10, 1) ; ┴
Else ; Parsing rightmost cell.
bottomBorder .= SubStr(border, 11, 1) ; ┘
}
;}
}
;{ Defining the number of spaces at left and at right from the cell's text value.
If (A_Index = 1) ; Parsing leftmost cell.
output .= SubStr(border, 2, 1) ; │
cellLeftSpacing := cellRightSpacing := horizontalCellPadding ; Counting the number of spaces to add at left from the value.
If (textAlign = "1") ; Right text alignment
cellLeftSpacing += thisColMaxWidth - thisCellWidth
Else If (textAlign = "-1") ; Left text alignment
cellRightSpacing += thisColMaxWidth - thisCellWidth
Else If (thisColMaxWidth - thisCellWidth) ;
{
cellLeftSpacing += Ceil((thisColMaxWidth - thisCellWidth) / 2)
cellRightSpacing += Floor((thisColMaxWidth - thisCellWidth) / 2)
}
Loop, % cellLeftSpacing
(A_Index = 1 ? cellLeftSpacing := " " : cellLeftSpacing .= " ")
Loop, % cellRightSpacing
(A_Index = 1 ? cellRightSpacing := " " : cellRightSpacing .= " ")
;}
output .= (cellLeftSpacing ? cellLeftSpacing : "") inputArray[A_Index, thisRow] (cellRightSpacing ? cellRightSpacing : "") SubStr(border, (A_Index == columns ? 2 : 13), 1) ; │
}
If (useRowSeparatingBorders) && (A_Index != rows) ; Concatenate a row separating border to the currently formed row.
output .= "`n" rowSeparator
}
output := topBorder output bottomBorder
;}
;{ Store the pseudo-graphics table into clipboard
Clipboard := output
ClipWait
;}
Return
;{ Functions
getColMaxWidth(colN)
{
Global
Local this, local that
Loop, % rows
{
that := StrLen(inputArray[colN, A_Index])
this < that ? this := that
}
Return this
}
;}