-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.nu
More file actions
225 lines (208 loc) · 5.39 KB
/
Copy pathlib.nu
File metadata and controls
225 lines (208 loc) · 5.39 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
export def smart-parse [regex] {
lines | each { parse -r $regex } | flatten
}
export def format-any [] {
let input = $in
let type = ($input | describe)
if $type =~ '^(int|float)$' {
print -e '1'
$input | format-num
} else if $type =~ '^(table|list<any>)' {
print -e '2'
$input | each {
transpose key value
| update value { format-any }
| transpose -rdi
}
} else if $type =~ '^record' {
print -e '3'
$input
| transpose key value
| update value { format-any }
| transpose -rdi
} else {
print -e '4'
$input
}
}
export def format-num [] {
let num = $in
def format-int [] {
split chars
| reverse
| chunks 3
| each { str join }
| str join ","
| str reverse
}
match ($num | describe) {
"int" => { $num | into string | format-int }
"float" => {
$num
| into string
| do {
let parts = $in | split row "."
let intpart = $parts | first
let floatpart = $parts | last
$'($intpart | format-int).($floatpart)'
}
}
}
}
export def load-cache [cachefile: path, varname: string, loader: closure] {
print -en $'- Loading ($varname)'
if ($cachefile | path exists) {
print -e ' (cached)'
open $cachefile
} else {
print -e ''
do $loader | tee { save $cachefile }
}
}
export def convert-value [unitID: int] {
let input = $in
let expiry_anchor = ('1970-01-01' | into datetime)
$input | match $unitID {
115 => { into int } # groupID
116 => { into int } # typeID
119 => { into int } # attributeID
129 => { $'($in)hr' | into duration } # hours
136 => { into int } # slot
137 => { into bool } # boolean
139 => { into bool } # plus sign
141 => { into int } # hardpoints
142 => { into int } # 1=Male 2=Unisex 3=Female
143 => { $expiry_anchor + ($'($in)day' | into duration) } # days since UNIX epoch
_ => { $in }
}
}
export def catch-errors [] {
collect
}
export def xray [
comment
--length (-l) # output input's length
] {
let input = $in
print -en $'- ($comment): '
if $length {
print -e ($input | length | into string)
return $input
}
let inputtype = $input | describe
if $inputtype =~ '^(list|table|record)' {
print -e $"\n($input | table)"
} else {
print -e $"($input)"
}
$input
}
export def wrap-bin [] {
wrap text | insert bin { |row| $row.text | into binary }
}
export def smart-group-by [colname] {
group-by { get $colname } --to-table | rename -c { group: $colname } | update items { reject $colname } | sort-by -r $colname
}
export def column-types [] {
# Thanks @Cheer at the Nushell Discord
describe -d
| flatten -a
| get values
| flatten
| where type == record
| get columns
| uniq
| update cells {
if ($in | describe | str starts-with 'record') {
get type
} else {}
}
| transpose column type
}
export def str-wrap [
--wrap-at: number = 20
] {
str replace -a "\r\n" "\n"
| str replace -a "\n" " SINGLENEWLINE "
| str trim
| split row -r "\\s+"
| reduce -f { joined: '' count: 0 } { |word, state|
if ($word == "\n") {
{
joined: ($state.joined + "\n")
count: 0
}
} else if ($state.count < $wrap_at) {
if ($state.joined | is-empty) {
{
joined: $word
count: ($word | str length)
}
} else {
{
joined: ($state.joined + ' ' + $word)
count: ($state.count + 1 + ($word | str length))
}
}
} else {
{
joined: ($state.joined + "\n" + $word)
count: 0
}
}
}
| get joined
| str replace -ar "\\s*SINGLENEWLINE\\s*" "\n"
}
# To be used inside of an `each`, only update a column (key) if it exists.
export def smart-update [key: string, op: closure] {
if ($key in $in) {
update $key { if ($in != null) { do $op } else { null } }
} else {}
}
# Gets the first available column (key).
export def smart-get [
--empty-as-missing (-e) # treat empty values as missing columns
...keys
] {
let input = $in
if ($input | is-empty) {
return ''
}
let result = $keys | reduce -f null { |key, acc|
if ($acc == null) {
if $key in ($input | columns) {
let value = try { $input | get $key } catch { |err|
error make { msg: $'($key) not found in ($input | to json)' }
}
if ($empty_as_missing and ($value | is-empty)) {
$acc
} else {
$value
}
} else {
$acc
}
} else {
$acc
}
}
if $result == null {
error make { msg: $'no values found for keys ($keys)' }
}
$result
}
export def load-units [] {
let filename = '.dogmaunits.json'
$filename | path exists | if not $in {
http get 'https://sde.hoboleaks.space/tq/dogmaunits.json' | save $filename
}
open --raw $filename | from json | transpose unitID data | flatten | update unitID { into int }
}
export def left-join [
other: list<any>
join_on: string
] {
upsert $join_on { default null } # workaround for the `join -l` bug that removes rows
| join -l $other $join_on
}