-
Notifications
You must be signed in to change notification settings - Fork 2
/
defs.nu
485 lines (424 loc) · 13 KB
/
defs.nu
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#grep for nu
#
#Examples;
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
export def grep-nu [
search:string #search term
entrada?:string #file or pipe
] {
let input = $in
let entrada = if ($entrada | is-empty) {
if ($input | is-column name) {
$input | get name
} else {
$input
}
} else {
$entrada
}
if ('*' in $entrada) {
grep -ihHn $search ...(glob $entrada)
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match | nu-highlight
}
| update file {|f|
let info = $f.file | path parse
$info.stem + "." + $info.extension
}
}
export alias grp = grep-nu
#copy pwd
export def cpwd [] {
$env.PWD | xclip -sel clip
}
#xls/ods 2 csv
export def xls2csv [
inputFile:string
--outputFile:string
] {
let output = (
if ($outputFile | is-empty) or (not $outputFile) {
$"($inputFile | path parse | get stem).csv"
} else {
$outputFile
}
)
libreoffice --headless --convert-to csv $inputFile
#add in2csv
}
#check if drive is mounted
export def is-mounted [drive:string] {
(ls ~/media | find $"($drive)" | length) > 0
}
#get phone number from google contacts
export def get-phone-number [search:string] {
goobook dquery $search
| from ssv
| rename results
| where results =~ '(?P<plus>\+)(?P<nums>\d+)'
}
#open mcomix
export def mcx [file?] {
let file = get-input $in $file
bash -c $'mcomix "($file)" 2>/dev/null &'
}
#jdownloader downloads info
export def jd [
--ubb(-b) #check ubb jdownloader
--desktop(-d) #check ubb desktop
] {
match [$ubb,$desktop] {
[true,false] => {jdown -b 1},
[false,true] => {jdown -b 2},
[false,false] => {jdown},
[true,true] => {return-error "please specify only one option"}
}
| from json
}
#nushell source files info
export def nu-sloc [] {
let stats = (
ls **/*.nu
| select name
| insert lines { |it|
open $it.name
| size
| get lines
}
| insert blank {|s|
$s.lines - (open $s.name | lines | find --regex '\S' | length)
}
| insert comments {|s|
open $s.name
| lines
| find --regex '^\s*#'
| length
}
| sort-by lines -r
)
let lines = ($stats | reduce -f 0 {|it, acc| $it.lines + $acc })
let blank = ($stats | reduce -f 0 {|it, acc| $it.blank + $acc })
let comments = ($stats | reduce -f 0 {|it, acc| $it.comments + $acc })
let total = ($stats | length)
let avg = ($lines / $total | math round)
$'(char nl)(ansi pr) SLOC Summary for Nushell (ansi reset)(char nl)'
print { 'Total Lines': $lines, 'Blank Lines': $blank, Comments: $comments, 'Total Nu Scripts': $total, 'Avg Lines/Script': $avg }
$'(char nl)Source file stat detail:'
print $stats
}
#web search in terminal
export def gg [...search: string] {
ddgr -n 5 ($search | str join ' ')
}
#countdown alarm
export def countdown [
n: int #time in seconds
] {
let BEEP = ([$env.MY_ENV_VARS.linux_backup "alarm-clock-elapsed.oga"] | path join)
let muted = (pacmd list-sinks
| lines
| find muted
| parse "{state}: {value}"
| get value
| get 0
)
if $muted == 'no' {
termdown $n
^mpv --no-terminal $BEEP
return
}
termdown $n
unmute
^mpv --no-terminal $BEEP
mute
}
#check validity of a link
export def check-link [link?,timeout?:int] {
let link = get-input $in $link
if ($timeout | is-empty) {
try {
http get $link | ignore;true
} catch {
false
}
} else {
try {
http get $link -m $timeout | ignore;true
} catch {
false
}
}
}
#send email via Gmail with signature files (posfix configuration required)
#
#Examples:
#-Body from cli:
# send-gmail test@gmail.com "the subject" --body "the body"
# echo "the body" | send-gmail test@gmail.com "the subject"
#-Body from a file:
# open file.txt | send-gmail test@gmail.com "the subject"
#-Attachments:
# send-gmail test@gmail.com "the subject" --body "the body" this_file.txt
# echo "the body" | send-gmail test@gmail.com "the subject" this_file.txt,other_file.pdf
# open file.txt | send-gmail test@gmail.com "the subject" this_file.txt,other_file.pdf,other.srt
export def send-gmail [
to:string #email to
subject:string #email subject
--body:string #email body, use double quotes to use escape characters like \n
--from:string #email from, export default: $MY_ENV_VARS.mail
...attachments #email attachments file names list (in current directory), separated by comma
] {
let inp = if ($in | is-empty) { "" } else { $in | into string }
let from = get-input $env.MY_ENV_VARS.mail $from
if ($body | is-empty) and ($inp | is-empty) {
return-error "body unexport defined!!"
}
if not (($from | str contains "@") and ($to | str contains "@")) {
return-error "missing @ in email-from or email-to!!"
}
let signature_file = (
match $from {
($env.MY_ENV_VARS.mail) => {
[$env.MY_ENV_VARS.nu_scripts "send-gmail_kurokirasama_signature"] | path join
},
($env.MY_ENV_VARS.mail_ubb) => {
[$env.MY_ENV_VARS.nu_scripts "send-gmail_ubb_signature"] | path join
},
($env.MY_ENV_VARS.mail_lmgg) => {
[$env.MY_ENV_VARS.nu_scripts "send-gmail_lmgg_signature"] | path join
},
_ => {
[$env.MY_ENV_VARS.nu_scripts "send-gmail_other_signature"] | path join
}
}
)
let signature = (open $signature_file)
let BODY = (
if ($inp | is-empty) {
$"($body)\n" + $signature
} else {
$"($inp)\n" + $signature
}
)
if ($attachments | is-empty) {
echo $BODY | mail -r $from -s $subject $to
} else {
let ATTACHMENTS = ($attachments
| split row ","
| each {|file|
[$env.PWD $file] | path join
}
| str join " --attach="
)
bash -c $"\'echo ($BODY) | mail --attach=($ATTACHMENTS) -r ($from) -s \"($subject)\" ($to) --debug-level 10\'"
}
}
#reset alpine authentification
export def reset-alpine-auth [] {
rm ~/.pine-passfile
touch ~/.pine-passfile
alpine-notify -i
}
#run matlab in cli
export def matlab-cli [
--background(-b) #send process to the background, select input m-file from list
--input(-i):string #input m-file to run in background mode, must be in the same directory
--output(-o):string #output file for log without extension
--log_file(-l):string = "log24" #log file in foreground mode
--kill(-k) #kill current matlab processes
] {
if $kill {
ps -l
| find -i matlab
| find local & MATLAB
| find -v 'MATLAB-language-server' & 'bin/nu' & 'yandex-disk'
| each {|row|
kill -f $row.pid
}
return
}
if not $background {
matlab -nosplash -nodesktop -softwareopengl -sd ($env.PWD) -logfile ("~/Dropbox/matlab" | path join $"($log_file).txt" | path expand) -r "setenv('SHELL', '/bin/bash');"
return
}
let log = (date now | format date "%Y.%m.%d_%H.%M.%S") + "_log.txt"
let input = (
if ($input | is-empty) {
ls *.m
| get name
| path parse
| get stem
| input list -f (echo-g "m-file to run: ")
} else {
$input | path parse | get stem
}
)
let output = if ($output | is-empty) {$log} else {$output + ".txt"}
bash -c ($"matlab -batch \"" + "setenv('SHELL', '/bin/bash'); " + $"($input)\"" + $" > ($output) &")
}
#get files all at once from webpage using wget
export def wget-all [
webpage: string #url to scrap
...extensions #list of extensions separated by space
] {
wget -A ($extensions | str join ",") -m -p -E -k -K -np --restrict-file-names=windows $webpage
}
#my pdflatex
export def my-pdflatex [file?] {
let tex = get-input $in $file -n
texfot pdflatex -interaction=nonstopmode -synctex=1 ($tex | path parse | get stem)
}
#pandoc md compiler
export def my-pandoc [
file?
] {
let file_name = get-input $in $file -n
let file_base_name = $file_name | path parse | get stem
pandoc --quiet $file_name -o $"($file_base_name).pdf" --pdf-engine=xelatex -F mermaid-filter -F pandoc-crossref --number-sections --highlight-style $env.MY_ENV_VARS.pandoc_theme
openf $"($file_base_name).pdf"
}
#maestral status
export def "dpx status" [] {
maestral status | lines | parse "{item} {status}" | str trim | drop nth 0
}
#qr code generator
export def qrenc [url] {
curl $"https://qrenco.de/($url)"
}
#compact with empty strings and nulls
export def scompact [
...columns: string # the columns to compactify
--invert(-i) # select the opposite
] {
mut out = $in
for column in $columns {
if $invert {
$out = ($out | upsert $column {|row| if not ($row | get $column | is-empty) {null} else {$row | get $column}} | compact $column )
} else {
$out = ($out | upsert $column {|row| if ($row | get $column | is-empty) {null} else {$row | get $column}} | compact $column )
}
}
return $out
}
#local http server
export def --wrapped "http server" [
root:string =".",
...rest
] {
simple-http-server $root ...$rest
}
#export nushell.github documentation
export def export-nushell-docs [] {
if ("~/software/nushell.github.io" | path expand | path exists) {
cd ~/software/nushell.github.io;git pull
rm -rf nushell
} else {
cd ~/software
git clone https://github.com/nushell/nushell.github.io.git
cd nushell.github.io
}
mkdir nushell
cd blog;join-text-files md blog;mv blog.md ../nushell;cd ..
cd book;join-text-files md book;mv book.md ../nushell;cd ..
cd commands/categories;join-text-files md categories;mv categories.md ..;cd ..
cd docs;join-text-files md docs;mv docs.md ..;cd ..
join-text-files md commands;mv commands.md ../nushell;cd ..
cd cookbook;join-text-files md cookbook;mv cookbook.md ../nushell;cd ..
cd lang-guide;join-text-files md lang-guide;mv lang-guide.md ../nushell;cd ..
rm -rf ([$env.MY_ENV_VARS.ai_database nushell] | path join)
mv -f nushell/ $env.MY_ENV_VARS.ai_database
cd ~/software/nushell
cp README.md ([$env.MY_ENV_VARS.ai_database nushell] | path join)
cd ([$env.MY_ENV_VARS.ai_database nushell] | path join)
join-text-files md all_nushell
let system_message = (open ([$env.MY_ENV_VARS.chatgpt_config system bash_nushell_programmer.md] | path join)) ++ "\n\nPlease consider the following nushell documentation to elaborate your answer.\n\n"
$system_message ++ (open all_nushell.md) | save -f ([$env.MY_ENV_VARS.chatgpt_config system bash_nushell_programmer_with_nushell_docs.md] | path join)
}
#enable ssh without password
export def ssh-sin-pass [
user:string
ip:string
--port(-p):int = 22
] {
if not ("~/.ssh/id_rsa.pub" | path expand | path exists) {
ssh-keygen -t rsa
}
ssh-copy-id -i ~/.ssh/id_rsa.pub -p $port $"($user)@($ip)"
}
#clean nerd-fonts repo
export def nerd-fonts-clean [] {
cd ~/software/nerd-fonts/
rm -rf .git
rm -rf patched-fonts
}
# Performs logical operations on multiple predicates.
# User has to specify exactly one of the following flags: `--all`, `--any` or `--one-of`.
export def verify [
clausules?
--not(-n) # Negate the test result
--false(-f) # The default behavior is to test truthiness of the predicates. Use this flag to test falsiness instead.
--and(-a) # All of the given predicates should test positive
--or(-o) # At least one of the given predicates should test positive
--xor(-x) # Exactly one of the given predicates should test positive
]: [
list<bool> -> bool
list<closure> -> bool
] {
let inputs = if ($clausules | is-empty) {$in} else {$clausules}
let test_value = not $false
let op = {|it|
match ($it | describe) {
"bool" => $it
"closure" => {do $it}
$x => {error make {msg: $"inputs of type ($x) is not supported. Please check."}}
}
}
let res = match [$and $or $xor] {
[true false false] => { $inputs | all {|it| (do $op $it) == $test_value} }
[false true false] => { $inputs | any {|it| (do $op $it) == $test_value} }
[false false true] => {
mut res = false
mut first_true = false
for $it in $inputs {
match [((do $op $it) == $test_value) $first_true] {
[false _] => {}
[true false] => {$first_true = true; $res = true;}
[true true] => {$res = false;}
}
}
$res
}
}
$not xor $res
}
#flatten a record keys
#
#Example:
# flatten-keys $env.config '$env.config'
def flatten-keys [rec: record, root: string] {
$rec | columns | each {|key|
let is_record = (
$rec | get $key | describe --detailed | get type | $in == record
)
# Recusively return each key plus its subkeys
[$'($root).($key)'] ++ match $is_record {
true => (flatten-keys ($rec | get $key) $'($root).($key)')
false => []
}
} | flatten
}
#################################################################################################
## appimages
#################################################################################################
#open balena-etche
export def balena [] {
bash -c $"([$env.MY_ENV_VARS.backup appimages 'balenaEtcher.AppImage'] | path join) 2>/dev/null &"
}