/
format-keyvals.ahk
87 lines (64 loc) · 1.36 KB
/
format-keyvals.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
^!s::
KeyWait s
KeyWait Alt
KeyWait Control
gui, font, s10 Verdana
gui, add, text,, Set Delimiter and Separator
gui, font, s30 Verdana
gui, add, edit, center section xs+40 vdelimiter, `:
gui, add, edit, center ys vseparator, `,
gui, font, s10 Verdana
gui, add, Button, section Default, OK
gui, show
return
ButtonOK:
Gui, Submit
;in case the vars are empty
if delimiter is space
delimiter = `:
if separator is space
separator = `,
;grabs selected text
Send ^c
text := Clipboard
;needed data will be stored in this array
t := Object()
maxlen = 0
;finding out the biggest key and storing all the data
i = 1
Loop, parse, text, %separator%, `n`r
{
;RTrim() preserves identation. change to Trim() to ignore it
key := RTrim(SubStr(A_LoopField, 1, InStr(A_LoopField, delimiter) - 1))
keylen := StrLen(key)
val := Trim(SubStr(A_LoopField, InStr(A_LoopField, delimiter) + 1))
IfGreater, keylen, 0
{
t[i] := key delimiter val delimiter keylen
if (keylen > maxlen)
maxlen := keylen
}
i++
}
;writing the result
i = 1
Loop % t.MaxIndex()
{
rec := t[i]
StringSplit, line, rec, %delimiter%
key := line1
val := line2
keylen := line3
spaces := maxlen - keylen + 1
SendInput {Raw}%key%
SendInput {space %spaces%}%delimiter%{space}
SendInput {Raw}%val%
if (i < t.MaxIndex())
{
SendInput, %separator%`n
SendInput +{home}
SendInput {del}
}
i++
}
return