forked from Tebayaki/AutoHotkeyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint.ahk
More file actions
147 lines (140 loc) · 4.5 KB
/
Print.ahk
File metadata and controls
147 lines (140 loc) · 4.5 KB
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
/* @example
Print("Pass", ["any", "count", "of"], "parameters")
*/
Print(values*) {
static printer_ := Printer()
printer_.Print(values*)
}
/* @example
obj := {
a: "Hello world!",
b: [1, 2, 3],
c: Map("a", "apple", "b", "banana", "c", "cherry"),
}
FPrint(obj)
*/
FPrint(value, breakDeepth := 1, indent := " ") {
static printer_ := Printer()
printer_.BreakDeepth := breakDeepth
printer_.IndentUnit := indent
printer_.Print(value)
}
class Printer {
__New(separator := " ", end := "`n", breakDeepth := 0, indent := " ", outputFile := "*", encoding := "utf-8") {
this.Separator := separator
this.End := end
this.BreakDeepth := breakDeepth
this.OutputFile := outputFile
this.Encoding := encoding
this.IndentUnit := indent
}
Print(values*) {
static _ := DllCall("GetStdHandle", "uint", -11, "ptr") || DllCall("AllocConsole")
output := ""
for v in values {
if A_Index > 1
output .= this.Separator
if IsSet(v) {
if v is Primitive
output .= v
else
stringify(v, 1, "")
}
else
output .= "unset"
}
FileAppend(output this.End, this.OutputFile, this.Encoding)
return
stringify(value, deepth, indent) {
if value is Number
output .= value
else if value is String
output .= '"' value '"'
else if value is Array
stringifyArray(value, deepth, indent)
else if value is Map
stringifyMap(value, deepth, indent)
else if value is Object
stringifyObject(value, deepth, indent)
}
stringifyArray(value, deepth, indent) {
if deepth <= this.BreakDeepth {
output .= "[`n"
for v in value {
if A_Index > 1
output .= ",`n"
output .= indent this.IndentUnit
if IsSet(v)
stringify(v, deepth + 1, indent this.IndentUnit)
else
output .= "unset"
}
output .= "`n" indent "]"
}
else {
output .= "["
for v in value {
if A_Index > 1
output .= ", "
if IsSet(v)
stringify(v, deepth + 1, "")
else
output .= "unset"
}
output .= "]"
}
}
stringifyMap(value, deepth, indent) {
if deepth <= this.BreakDeepth {
output .= "{`n"
for k, v in value {
if A_Index > 1
output .= ",`n"
if k is String
k := '"' k '"'
else if !(k is Number)
k := Type(k) "_" ObjPtr(k)
output .= indent this.IndentUnit k ': '
stringify(v, deepth + 1, indent this.IndentUnit)
}
output .= "`n" indent "}"
}
else {
output .= "{"
for k, v in value {
if A_Index > 1
output .= ", "
if k is String
k := '"' k '"'
else if !(k is Number)
k := Type(k) "_" ObjPtr(k)
output .= k ': '
stringify(v, deepth + 1, "")
}
output .= "}"
}
}
stringifyObject(value, deepth, indent) {
if deepth <= this.BreakDeepth {
output .= "{`n"
for k, v in value.OwnProps() {
if A_Index > 1
output .= ",`n"
output .= indent this.IndentUnit k ": "
stringify(v, deepth + 1, indent this.IndentUnit)
}
output .= "`n" indent "}"
}
else {
output .= "{"
for k, v in value.OwnProps() {
if A_Index > 1
output .= ", "
output .= k ": "
stringify(v, deepth + 1, "")
}
output .= "}"
}
}
}
}