This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.nu
613 lines (522 loc) · 14.6 KB
/
misc.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#*
#* _ __ _ _
#* __ _ ___ __ _| |_ / _(_) |___ ___ WEBSITE: https://goatfiles.github.io
#* / _` / _ \/ _` | _| _| | / -_|_-< REPOS: https://github.com/goatfiles
#* \__, \___/\__,_|\__|_| |_|_\___/__/ LICENCE: https://github.com/goatfiles/dotfiles/blob/main/LICENSE
#* |___/
#* MAINTAINERS:
#* AMTOINE: https://github.com/amtoine antoine#1306 7C5EE50BA27B86B7F9D5A7BA37AAE9B486CFF1AB
#* ATXR: https://github.com/atxr atxr#6214 3B25AF716B608D41AB86C3D20E55E4B1DE5B2C8B
#*
use prompt.nu
# TODO
export def yt-dl-names [
--id (-i): string # the id of the playlist
--channel (-c): string # the id of the channel
--path (-p): string = . # the path where to store to final `.csv` file
--all (-a) # download all the playlists from the channel when raised
] {
let format = '"%(playlist)s",%(playlist_id)s,%(playlist_index)s,"%(uploader)s","%(title)s",%(id)s'
let url = if $all {
$"https://www.youtube.com/channel/($channel)/playlists"
} else {
$"https://www.youtube.com/playlist?list=($id)"
}
if (ls | find $path | is-empty) {
mkdir $path
}
let file = ($path | path join $"($id).csv")
print $"Downloading from '($url)' to ($file)..."
(youtube-dl
-o $format
$url
--get-filename
--skip-download
--verbose
) |
from csv --noheaders |
rename playlist "playlist id" "playlist index" uploader title id |
insert url {
|it|
$'https://www.youtube.com/watch?v=($it.id)&list=($it."playlist id")'
} |
save $file
}
# Asks for an entry name in a password store and opens the store.
#
# Uses $env.PASSWORD_STORE_DIR as the store location, asks for
# a passphrase with pinentry-gtk and copies the credentials to
# the system clipboard..
export def pass-menu [
--path (-p): string = "/usr/share/rofi/themes/" # the path to the themes (default to '/usr/share/rofi/themes/')
--theme (-t): string = "sidebar" # the theme to apply (defaults to 'sidebar')
--list-themes (-l) # list all available themes in --path
] {
if ($list_themes) {
ls $path |
select name |
rename theme |
str replace $"^($path)" "" theme |
str replace ".rasi$" "" theme
} else {
let entry = (
ls $"($env.PASSWORD_STORE_DIR)/**/*" |
where type == file |
select name |
str replace $"^($env.PASSWORD_STORE_DIR)/" "" name |
str replace ".gpg$" "" name |
to csv |
rofi -config $"($path)($theme).rasi" -show -dmenu |
str trim
)
if not ($entry | is-empty) {
pass show $entry -c
dunstify $entry "Copied to clipboard for 45 seconds."
} else {
print "User choose to exit..."
}
}
}
# TODO
export def alarm [
time: string
message: string
] {
termdown -e $time --title $message
dunstify "termdown" $message --urgency critical --timeout 0
print $message
}
# TODO
def get-aoc-header [
login: string
] {
let aoc_login = (
gpg --quiet --decrypt ($login | path expand)
| from toml
)
let header = [
Cookie $'session=($aoc_login.cookie)'
User-Agent $'email: ($aoc_login.mail)'
]
$header
}
# TODO
#
# encryption:
# ```bash
# > gpg --symmetric --armor --cipher-algo <algo> <file>
# ```
#
# example login file:
# ```toml
# cookie = "my-cookie: see https://github.com/wimglenn/advent-of-code-wim/issues/1"
# mail = "[email protected]"
# ```
#
export def "aoc fetch input" [
day: int
login: string
] {
let url = $'https://adventofcode.com/2022/day/($day)/input'
http get -H (get-aoc-header $login) $url
}
# TODO
export def "aoc fetch answers" [
day: int
login: string
] {
let url = $'https://adventofcode.com/2022/day/($day)'
let result = (http get -H (get-aoc-header $login) $url)
let answers = (
$result
| lines
| parse "<p>Your puzzle answer was <code>{answer}</code>{rest}"
)
if ($answers | is-empty) {
$result | str trim
} else {
{
silver: $answers.answer.0
gold: $answers.answer.1
}
}
}
# TODO: docstring
export def-env back [] { cd - }
# TODO: docstring
export def "get ldd deps" [exec: string] {
let bin = (which $exec)
if ($bin | is-empty) {
print $"could not find ($exec) in PATH..."
return
}
ldd ($bin | get path)
| lines
| parse '{lib} ({addr})'
| str trim
| update lib {|it|
let tokens = ($it.lib | parse "{lib} => {symlink}")
if ($tokens | is-empty) {
{
lib: $it.lib
symlink: $nothing
}
} else {
$tokens
}
}
| flatten --all
}
# TODO: docstring
export def "open pdf" [
--launcher: string = "okular"
--no-swallow: bool
--swallower: string = "devour"
--from = [~/documents/ ~/downloads/]
] {
let choices = (
$from
| each {|| ls $"($in)/**/*.pdf"}
| flatten
| get name
| to text
)
let choice = (
$choices | prompt fzf_ask "What PDF to open? " "pdftotext {} /dev/stdout"
)
if ($choice | is-empty) {
print "user chose to exit..."
return
}
if ($no_swallow) {
^$launcher $choice
} else {
^$swallower $launcher $choice
}
}
# TODO: docstring
# credit to @fdncred
# https://discord.com/channels/601130461678272522/615253963645911060/1071893062864863293
export def "history stats" [
--summary (-s): int = 5
--last-cmds (-l): int
--verbose (-v): bool
] {
let top_commands = (
history
| if ($last_cmds != $nothing) { last $last_cmds } else { $in }
| get command
| split column ' ' command
| uniq -c
| flatten
| sort-by --reverse count
| first $summary
)
if ($verbose) {
let total_cmds = (history | length)
let unique_cmds = (history | get command | uniq | length)
print $"(ansi green)Total commands in history:(ansi reset) ($total_cmds)"
print $"(ansi green)Unique commands:(ansi reset) ($unique_cmds)"
print ""
print $"(ansi green)Top ($top_commands | length)(ansi reset) most used commands:"
}
$top_commands
}
# TODO
# credit to @fdncred
# https://discord.com/channels/601130461678272522/615253963645911060/1072286261873741854
export def "history search" [
str: string = '' # search string
--cwd(-c) # Filter search result by directory
--exit(-e): int = 0 # Filter search result by exit code
--before(-b): datetime = 2100-01-01 # Only include results added before this date
--after(-a): datetime = 1970-01-01 # Only include results after this date
--limit(-l): int = 25 # How many entries to return at most
] {
history
| where start_timestamp != ""
| update start_timestamp {|r| $r.start_timestamp | into datetime}
| where command =~ $str and exit_status == $exit and start_timestamp > $after and start_timestamp < $before
| if $cwd { where cwd == $env.PWD } else { $in }
| first $limit
}
# TODO: docstring
export def "get wallpapers" [
nb_wallpapers: int
--shuffle (-s): bool
] {
[
/usr/share/backgrounds
($env.GIT_REPOS_HOME | path join "github.com/goatfiles/wallpapers/wallpapers")
]
| each {||
let glob_path = ($in | path join "*")
glob --no-dir $glob_path
}
| flatten
| if ($shuffle) { shuffle } else { $in }
| take $nb_wallpapers
}
# TODO: docstring
export def "glow wide" [file: string] {
^glow --pager --width (term size | get columns) $file
}
# TODO: docstring
export def "youtube share" [
url: string
--pretty: bool
--clip (-c): bool
] {
use std clip
let video = (
http get $url
| str replace --all "<" "\n<" # separate all HTML blocks into `<...> ...` chunks without the closing `</...>`
| str replace --all "</.*>" ""
| lines
| find "var ytInitialPlayerResponse = " # all the data is located in this JSON structure...
| parse --regex 'var ytInitialPlayerResponse = (?<data>.*);'
| get data.0
| from json
| get microformat.playerMicroformatRenderer # ...and more specifically in this subfield
| select embed.iframeUrl uploadDate ownerChannelName lengthSeconds title.simpleText # select the most usefull fields
| rename url date author length title
| update length {|it| [$it.length "sec"] | str join | into duration} # udpate some of the fields for clarity
| update date {|it| $it.date | into datetime}
| update url {|it|
$it.url
| url parse
| reject query params
| update path {|it| $it.path | str replace "/embed/" ""}
| update host youtu.be
| url join
}
)
if $pretty {
let link = $"[*($video.title)*](char lparen)($video.url)(char rparen)"
if not $clip {
return $link
}
$link | clip
return
}
if not $clip {
return $video
}
$video.url | clip
}
# TODO: docstring
export def "list todos" [] {
rg "//.? ?TODO" . -n
| lines
| parse "{file}:{line}:{match}"
| try {
group-by file
| transpose
| reject column1.file
| transpose -rid
} catch {
"no TODOs found in this directory"
}
}
# TODO: docstring
export def "cargo list" [] {
^cargo install --list
| lines
| str replace '^(\w)' "\n${1}"
| str join
| lines | skip 1
| parse --regex '(?<pkg>.*) v(?<version>\d+\.\d+\.\d+)(?<path>.*):(?<bins>.*)'
| str trim
| update bins {|it| $it.bins | str replace '\s+' ' ' | split row ' '}
| update path {|it| $it.path | str replace --string '(' '' | str replace --string ')' ''}
}
# TODO: docstring
export def "watch cpu" [nb_loops = -1] {
let name = $in
mut i = 0
loop {
ps | where name == $name | try { math sum | get cpu }
$i += 1
if ($nb_loops > 0) and ($i >= $nb_loops) {
break
}
}
}
# TODO: docstring
export def "cargo info full" [
crate: string
] {
cargo info $crate
| lines
| parse "{key}: {value}"
| str trim
| transpose -r
| into record
| merge ({
versions: (
cargo info $crate -VV
| lines -s
| skip 1
| parse --regex '(?<version>\d+\.\d+\.\d+)\s+(?<released>.* ago)\s+(?<downloads>\d+)'
| into int downloads
)
})
}
def "qutebrowser list sessions" [] {
ls ($env.XDG_DATA_HOME | path join "qutebrowser" "sessions")
| get name
| path parse
| where extension == "yml"
| get stem
}
# TODO: docstring
export def "qutebrowser open" [session: string = ""] {
let session = if ($session | is-empty) {
qutebrowser list sessions
| to text
| fzf
| str trim
} else {
$session
}
if ($session | is-empty) {
return
}
qutebrowser $":session-load ($session)" --target window
}
# TODO: docstring
export def "qutebrowser import" [] {
let session = $in
$session
| open --raw
| save --force ($env.XDG_DATA_HOME
| path join "qutebrowser" "sessions" $session)
}
# TODO: docstring
export def "qutebrowser export" [session: string = ""] {
let session = if ($session | is-empty) {
qutebrowser list sessions
| to text
| fzf
| str trim
} else {
$session
}
if ($session | is-empty) {
return
}
$env.XDG_DATA_HOME
| path join "qutebrowser" "sessions" $session
| path parse
| update extension yml
| path join
| open --raw
}
# TODO: docstring
export def "into hex" [] {
fmt | get lowerhex
}
# Execute conditional pipelines depending on the previous command output.
#
# see https://discord.com/channels/601130461678272522/615253963645911060/1086437351598870689
#
# Examples:
# >_ 1 == 1 | pipeif true | "OMG 1 is equal to 1"
# OMG 1 is equal to 1
#
# >_ 1 != 1 | pipeif true | "This message will never be printed"
# Error:
# × Breaking pipeline: conditional execution aborted
#
# >_ [7 3 4 9] | find 7 3 | pipeif [7 3] | "Found numbers 7 and 3"
# Found numbers 7 and 3
#
# >_ [7 3 4 9] | find 3 5 | pipeif [3 5] | "This message will never be printed"
# Error:
# × Breaking pipeline: conditional execution aborted
export def pipeif [
expected: any # Expected value to not break the pipeline
--invert (-v): bool
] {
let value = $in
let condition = (if $invert {
($value | sort) == ($expected | sort)
} else {
($value | sort) != ($expected | sort)
})
if $condition {
error make --unspanned {
msg: "Breaking pipeline: conditional execution aborted"
}
}
return $value
}
# TODO
def "nu-complete list-images" [] {
ls ($env.IMAGES_HOME | path join "**" "*") | get name
}
def get-image [
image: path
] {
let image = (if ($image | is-empty) {
nu-complete list-images | to text | fzf | str trim
} else { $image })
if ($image | is-empty) {
error make --unspanned {
msg: "no image selected"
}
}
return $image
}
# TODO
export def "images edit" [
image?: path@"nu-complete list-images"
--editor: string = kolourpaint
--devour (-d): bool
] {
let image = (get-image $image)
if $devour {
devour $editor $image
} else {
^$editor $image
}
}
# TODO
export def "images view" [
image?: path@"nu-complete list-images"
--viewer: string = feh
] {
^$viewer (get-image $image)
}
def _throw-not-a-list-of-strings [files: any] {
error make --unspanned {
msg: $'please give a list of strings to `(ansi default_dimmed)(ansi default_italic)edit(ansi reset)`
=> found `(ansi default_dimmed)(ansi default_italic)($files | describe)(ansi reset)`
($files | table | lines | each {|file| $"($file)" } | str join "\n ")'
}
}
export def edit [
...rest: path
--no-auto-cmd (-n): bool
--auto-cmd: string = "lua require('telescope.builtin').find_files()"
--projects (-p): bool
] {
let files = ($in | default [])
if (not ($files | is-empty)) and (($files | describe) != "list<string>") {
_throw-not-a-list-of-strings $files
}
let files = ($rest | append $files | uniq)
if ($files | is-empty) {
^$env.EDITOR -c (
if $no_auto_cmd {
""
} else if $projects {
"lua require('telescope').extensions.projects.projects{}"
} else {
$auto_cmd
}
)
return
}
^$env.EDITOR $files
}