-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.k
More file actions
293 lines (271 loc) · 8.34 KB
/
utils.k
File metadata and controls
293 lines (271 loc) · 8.34 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import "fileio.krh"
func ansi(s) {
let esc = fromCharCode(27)
emit replace(s, "\\x1b", esc)
}
func vislen(s) {
let total = len(s)
let vis = "0"
let i = "0"
let esc = fromCharCode(27)
while toInt(i) < toInt(total) {
let ch = substring(s, toInt(i), toInt(i) + 1)
if ch == esc {
i = toInt(i) + 1 + ""
while toInt(i) < toInt(total) {
let c2 = substring(s, toInt(i), toInt(i) + 1)
i = toInt(i) + 1 + ""
if c2 == "m" { break }
}
} else {
vis = toInt(vis) + 1 + ""
i = toInt(i) + 1 + ""
}
}
emit vis
}
func padansi(s, w) {
let vl = toInt(vislen(s))
let pad = toInt(w) - vl
let result = s
let i = "0"
while toInt(i) < pad {
result = result + " "
i = toInt(i) + 1 + ""
}
emit result
}
func firstpart(s, delim) {
let pos = indexOf(s, delim)
if pos < 0 { emit s }
emit substring(s, 0, toInt(pos))
}
func afterpart(s, delim) {
let pos = indexOf(s, delim)
if pos < 0 { emit "" }
emit substring(s, toInt(pos) + 1, len(s))
}
func linecount(s) {
if s == "" { emit "0" }
let t = trim(s)
if t == "" { emit "0" }
let n = "1"
let i = "0"
while toInt(i) < len(t) {
let ch = substring(t, toInt(i), toInt(i) + 1)
if ch == "\n" { n = toInt(n) + 1 + "" }
i = toInt(i) + 1 + ""
}
emit n
}
func nthline(s, lineIdx) {
let target = toInt(lineIdx)
let cur = "0"
let start = "0"
let done = "0"
while done == "0" {
let rest = substring(s, toInt(start), len(s))
let nlPos = indexOf(rest, "\n")
if nlPos < 0 {
if toInt(cur) == target { emit trim(rest) }
done = "1"
} else {
if toInt(cur) == target {
emit trim(substring(rest, 0, toInt(nlPos)))
}
start = toInt(start) + toInt(nlPos) + 1 + ""
cur = toInt(cur) + 1 + ""
}
}
emit ""
}
func rambar(usedMB, totalMB) {
let used = toInt(usedMB)
let total = toInt(totalMB)
if total == 0 { emit "[ ]" }
let pct = used * 100 / total
let filled = pct * 20 / 100
let empty = 20 - filled
let bar = "["
let i = 0
while i < filled { bar = bar + "▒" i += 1 }
i = 0
while i < empty { bar = bar + "█" i += 1 }
emit bar + "]"
}
func colorpalette() {
let E = fromCharCode(27)
let RST = E + "[0m"
let blk = " "
let r1 = ""
let r2 = ""
let i = "0"
while toInt(i) < 8 {
let c1 = toInt(i) + 40 + ""
let c2 = toInt(i) + 100 + ""
r1 = r1 + E + "[" + c1 + "m" + blk
r2 = r2 + E + "[" + c2 + "m" + blk
i = toInt(i) + 1 + ""
}
kp(" " + r1 + RST)
kp(" " + r2 + RST)
}
func idxfrom(s, sub, start) {
let slen = len(s)
let sublen = len(sub)
let i = toInt(start)
while toInt(i) <= toInt(slen) - toInt(sublen) {
let piece = substring(s, toInt(i), toInt(i) + toInt(sublen))
if piece == sub { emit i + "" }
i = toInt(i) + 1 + ""
}
emit "-1"
}
func countRegSubkeys(hive, path) {
let kread = toHandle("131097")
let hkey = handleOut()
RegOpenKeyExA(hive, path, toHandle("0"), kread, hkey)
let hk = handleGet(hkey)
if handleValid(hk) == "0" { emit "0" }
let buf = bufNew("256")
let sz = bufNew("4")
let cnt = "0"
let idx = "0"
while toInt(idx) < 65536 {
bufSetDword(sz, "256")
let r = handleInt(RegEnumKeyExA(hk, toHandle(idx), buf, sz, toHandle("0"), toHandle("0"), toHandle("0"), toHandle("0")))
if toInt(r) != 0 { break }
cnt = toInt(cnt) + 1 + ""
idx = toInt(idx) + 1 + ""
}
RegCloseKey(hk)
emit cnt
}
func countDirs(path) {
let attrs = handleInt(GetFileAttributesA(path))
if attrs == "-1" { emit "0" }
if bitAnd(attrs, "16") == "0" { emit "0" }
let fd = structNew("WIN32_FIND_DATAA")
let h = FindFirstFileA(path + "\\*", fd)
if handleValid(h) == "0" { emit "0" }
let cnt = "0"
let di = "0"
while toInt(di) < 65536 {
let fa = structGet(fd, "WIN32_FIND_DATAA", "dwFileAttributes")
let name = structGet(fd, "WIN32_FIND_DATAA", "cFileName")
if bitAnd(fa, "16") != "0" {
if name != "." {
if name != ".." {
cnt = toInt(cnt) + 1 + ""
}
}
}
let nxt = handleInt(FindNextFileA(h, fd))
if nxt == "0" { break }
di = toInt(di) + 1 + ""
}
FindClose(h)
emit cnt
}
func kfpkgs() {
let HKLM = toHandle("2147483650")
let HKCU = toHandle("2147483649")
let p1 = countRegSubkeys(HKLM, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
let p2 = countRegSubkeys(HKLM, "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
let p3 = countRegSubkeys(HKCU, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
let winget = toInt(p1) + toInt(p2) + toInt(p3) + ""
let scoopPath = environ("USERPROFILE") + "\\scoop\\apps"
let scoopRaw = countDirs(scoopPath)
let scoop = toInt(scoopRaw) - 1 + ""
if toInt(scoop) < 0 { scoop = "0" }
let chocoBase = environ("ChocolateyInstall")
if chocoBase == "" { chocoBase = regHklmStr("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", "ChocolateyInstall") }
let choco = "0"
if chocoBase != "" {
let chocoLib = chocoBase + "\\lib"
choco = countDirs(chocoLib)
}
let result = ""
if toInt(winget) > 0 { result = winget + " (winget)" }
if toInt(scoop) > 0 {
if result != "" { result = result + ", " }
result = result + scoop + " (scoop)"
}
if toInt(choco) > 0 {
if result != "" { result = result + ", " }
result = result + choco + " (choco)"
}
emit result
}
// ── Krypton registry helpers ──────────────────────────────────────────
// Use bufNew/bufStr/bufGetDword/handleOut/handleGet/toHandle + windows.krh
func regHklmStr(path, name) {
let HKLM = toHandle("2147483650")
let kread = toHandle("131097")
let hkey = handleOut()
RegOpenKeyExA(HKLM, path, toHandle("0"), kread, hkey)
let hk = handleGet(hkey)
if handleValid(hk) == "0" { emit "" }
let buf = bufNew("1024")
let sz = bufNew("4")
bufSetDword(sz, "1024")
RegQueryValueExA(hk, name, toHandle("0"), toHandle("0"), buf, sz)
RegCloseKey(hk)
emit bufStr(buf)
}
func regHkcuStr(path, name) {
let HKCU = toHandle("2147483649")
let kread = toHandle("131097")
let hkey = handleOut()
RegOpenKeyExA(HKCU, path, toHandle("0"), kread, hkey)
let hk = handleGet(hkey)
if handleValid(hk) == "0" { emit "" }
let buf = bufNew("1024")
let sz = bufNew("4")
bufSetDword(sz, "1024")
RegQueryValueExA(hk, name, toHandle("0"), toHandle("0"), buf, sz)
RegCloseKey(hk)
emit bufStr(buf)
}
func regHklmDword(path, name) {
let HKLM = toHandle("2147483650")
let kread = toHandle("131097")
let hkey = handleOut()
RegOpenKeyExA(HKLM, path, toHandle("0"), kread, hkey)
let hk = handleGet(hkey)
if handleValid(hk) == "0" { emit "0" }
let buf = bufNew("4")
let sz = bufNew("4")
bufSetDword(sz, "4")
RegQueryValueExA(hk, name, toHandle("0"), toHandle("0"), buf, sz)
RegCloseKey(hk)
emit bufGetDword(buf)
}
func regHkcuDword(path, name) {
let HKCU = toHandle("2147483649")
let kread = toHandle("131097")
let hkey = handleOut()
RegOpenKeyExA(HKCU, path, toHandle("0"), kread, hkey)
let hk = handleGet(hkey)
if handleValid(hk) == "0" { emit "0" }
let buf = bufNew("4")
let sz = bufNew("4")
bufSetDword(sz, "4")
RegQueryValueExA(hk, name, toHandle("0"), toHandle("0"), buf, sz)
RegCloseKey(hk)
emit bufGetDword(buf)
}
func regHklmQword(path, name) {
let HKLM = toHandle("2147483650")
let kread = toHandle("131097")
let hkey = handleOut()
RegOpenKeyExA(HKLM, path, toHandle("0"), kread, hkey)
let hk = handleGet(hkey)
if handleValid(hk) == "0" { emit "0" }
let buf = bufNew("8")
let sz = bufNew("4")
bufSetDword(sz, "8")
RegQueryValueExA(hk, name, toHandle("0"), toHandle("0"), buf, sz)
RegCloseKey(hk)
emit bufGetQword(buf)
}