-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0t
executable file
·598 lines (526 loc) · 15.6 KB
/
0t
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
#! /usr/bin/env tclsh
###############################################################################
# 0t: utilities/shortcuts for the graphical shell
source $::env(HOME)/bin/lib.tcl
need notify-send \
rg \
firefox \
fuzzel \
hyprctl \
wl-copy \
bluetoothctl \
ifstat \
light \
kanshictl \
wlr-randr \
grim slurp swappy \
ponymix
set SUDO /usr/sbin/sudo
set ICONS /usr/share/icons/Papirus/24x24
###############################################################################
# helper: notify-send hinting for deduplication
proc __nrep {name} {
return "--hint=string:wired-tag:$name"
}
###############################################################################
# helper: notify-send hinting for icons
proc __nicon {icon} {
global ICONS
return "--hint=string:image-path:$ICONS/$icon"
}
###############################################################################
# for wm
proc _browser {} {
try {
exec firefox --private-window --profile "$::env(HOME)/opt/f1"
} on error e { fail "$e" }
}
###############################################################################
# for wm
proc _bye {} {
set runner "fuzzel --dmenu --lines=10 {--prompt= } --width=20"
set menu {\
Lock system-lock-screen\
Logout system-log-out\
Sleep system-suspend\
Reboot system-reboot\
Shutdown system-shutdown\
}
set menustr {}
foreach {name icon} $menu {
append menustr "$name\\0icon\\x1f$icon\n"
}
set menustr [string trimright $menustr "\n"]
if {[catch {exec echo -e $menustr | {*}$runner} what]} {
return
}
exec 0s bye "[string tolower $what]"
}
###############################################################################
# for wm
proc _decor {} {
set title decor
set notify "notify-send -u low"
set on [exec hyprctl getoption animations:enabled | awk {NR==1{print $2}}]
set next {}
set icon [__nicon panel/redshift-status-off.svg]
if {$on} {
set next off
set icon [__nicon panel/desktoptrusted.svg]
exec hyprctl --batch {
keyword animations:enabled 0;
keyword decoration:shadow:enabled 0;
keyword decoration:blur:enabled 0;
keyword general:gaps_in 0;
keyword general:gaps_out 0;
keyword general:border_size 1;
keyword decoration:rounding 0
}
} else {
set next on
set icon [__nicon panel/disper-panel.svg]
exec hyprctl reload
}
exec {*}$notify $icon $title $next
}
###############################################################################
# for wm
proc _emo {} {
set emoticons $::env(HOME)/opt/emoticons
set runner "fuzzel --dmenu {--prompt= } --width=64"
if {[catch {exec cat $emoticons | {*}$runner} what]} {
return
}
exec wl-copy [lindex $what 0]
}
###############################################################################
# for wm, implicit lum change
proc _lum {arg} {
set title lum
set icon_lo [__nicon panel/redshift-status-off.svg]
set icon_hi [__nicon panel/redshift-status-on.svg]
set icon {}
set notify "notify-send -u low"
switch -- $arg {
- { set icon $icon_lo }
+ { set icon $icon_hi }
}
exec sleep 0.1
exec {*}$notify $icon $title "[format %.0f [exec light -G]]"
}
###############################################################################
# for wm
proc _win {} {
set exp {\
sort_by(.focusHistoryID) .[] |\
(.workspace.name | sub("^special:scratch$"; "#"))\
+ " " + .class\
+ " : " + .title\
+ " ::: " + .address\
}
set lines [exec hyprctl -j clients | jq -r $exp]
set lenmin 48
set lenmax 128
set longest 0
foreach {line} [split $lines "\n"] {
set len [string length $line]
if {$len > $longest} { set longest [expr $len + 2] }
}
if {$longest > $lenmax} { set longest $lenmax }
if {$longest < $lenmin} { set longest $lenmin }
set runner "fuzzel --dmenu {--prompt= } --width=$longest"
if {[catch {exec echo $lines | {*}$runner} what]} {
return
}
set win [lindex $what end]
exec hyprctl dispatch focuswindow address:$win
}
###############################################################################
# for waybar
proc btbat {{id {}}} {
set k DD:A5:E3:61:B3:96
set earfun 00:12:6F:EE:F0:F7
set jbl B0:F1:A3:63:0A:66
set nothing 2C:BE:EB:78:2B:3B]
proc die {} {
global ME
upvar k k earfun earfun jbl jbl nothing nothing
puts stderr "$ME: mac address required
available devices:
k - Corne keyboard $k
earfun - EarFun Free Pro 2 $earfun
jbl - JBL Clip 2 $jbl
nothing - Nothing Ear $nothing"
puts {}
exit
}
try { eval set mac $$id } trap {} {} { die }
if {{} eq $id} { die }
if {![catch {exec bluetoothctl info $mac | rg {Connected: yes}}]} { puts {}; exit }
regsub -all {:} $mac {_} mac
set icon { }
if {{k} eq $id} { set icon { } }
set blue "dbus-send --print-reply=literal --system --dest=org.bluez /org/bluez/hci0/dev_$mac"
if {![catch {exec {*}$blue org.freedesktop.DBus.Introspectable.Introspect | rg {Battery1}}]} { puts {}; exit }
puts "$icon[lindex [exec {*}$blue org.freedesktop.DBus.Properties.Get string:org.bluez.Battery1 string:Percentage] end]"
}
###############################################################################
# for waybar
proc cpu {} {
set dir /sys/devices/system/cpu/cpu0
set gov [readfile $dir/cpufreq/scaling_governor]
#set epb [readfile $dir/power/energy_perf_bias]
switch -- [string index $gov 1] {
e { set gov {*}}
o { set gov {.}}
default { set gov {?}}
}
#puts "$gov$epb"
puts $gov
}
###############################################################################
proc lay {arg} {
global SESS
set icon_lo [__nicon apps/preferences-desktop-keyboard.svg]
set icon_hi [__nicon apps/cinnamon-virtual-keyboard.svg]
if {{x} ne $SESS} {
exec notify-send -u low $icon_lo key {xorg not running}
exit
}
switch -- $arg {
h - hsnt {
set arg hsnt
exec setxkbmap hsnt
exec xset r rate 200 40
}
q - qwerty - u - us {
set arg qwerty
exec setxkbmap -option ctrl:nocaps us
exec xset r rate 200 40
}
default {
ufail {lay h_snt|q_werty}
}
}
exec notify-send -u low $icon_hi key $arg
}
###############################################################################
# for wm, explicit lum change
proc lum {arg} {
set title lum
set notify "notify-send -u low"
set cmd {light}
set cmd_set {-S}
set cmd_dec {-U}
set cmd_inc {-A}
set cmd_get {-G}
set cmd_sub $cmd
set icon_lo [__nicon panel/redshift-status-off.svg]
set icon_hi [__nicon panel/redshift-status-on.svg]
set icon $icon_hi
switch -- [string index $arg 0] {
{} {}
- { append cmd_sub " $cmd_dec"; set icon $icon_lo }
+ { append cmd_sub " $cmd_inc" }
default { append cmd_sub " $cmd_set" }
}
if {[string length $arg]} {
set arg [string range $arg 1 end]
exec {*}$cmd_sub $arg
}
exec {*}$notify $icon $title [exec {*}$cmd $cmd_get]%
}
###############################################################################
# for wm
proc mon {{arg {}}} {
set title mon
set dev {eDP-1}
set cmd_base "wlr-randr --output $dev"
set cmd_on "$cmd_base --on"
set cmd_off "$cmd_base --off"
set cmd_toggle "$cmd_base --toggle"
set cmd_refresh {kanshictl reload}
switch -- [string index $arg 0] {
{} { exec {*}$cmd_toggle }
o - on { exec {*}$cmd_on }
x - off { exec {*}$cmd_off }
}
exec {*}$cmd_refresh
}
###############################################################################
# for waybar
proc net {} {
set cmd {ifstat}
set arg {--json --pretty}
set dev {wlp0s20f3}
set stat [exec $cmd {*}$arg $dev]
proc fmt {n} {
# KB/s
return [format {%.2f} [expr $n / 1024.0 / 1024.0]]
}
set rx [fmt [exec echo $stat | jq ".kernel.$dev.rx_bytes"]]
set tx [fmt [exec echo $stat | jq ".kernel.$dev.tx_bytes"]]
puts "_$rx^$tx"
}
###############################################################################
proc shot {args} {
set one [lindex $args 0]
set two [lindex $args 1]
set three [lindex $args 2]
set dir $::env(HOME)/got
set opt {}
set dst {}
set desc {}
set icon [__nicon mimetypes/image-x-generic.svg]
set notify "notify-send -u normal $icon"
proc help {} {
upvar 1 dir dir
return "\[e_dited|*s_creen] \[j_pg|*p_ng] \[c]
will save to: $dir"
}
if {3 < [llength $args]} {
puts stderr "wrong number of arguments"
ufail "shot [help]"
}
if {{} ne $three} {
if {{c} eq $three} {
append opt { -c}
} else {
puts stderr "unrecognised cursor enabling: $three"
ufail "shot [help]"
}
}
if {{} eq $two} {
set two png
}
switch -- $two {
j - jpg {
set two jpg
append opt { -t jpeg -q 90}
}
p - png {
set two png
append opt { -t png}
}
default {
puts stderr "unrecognised image type: $two"
ufail "shot [help]"
}
}
if {{} eq $one} {
set one screen
}
switch -- $one {
e - edited {
set one e
append opt " -g"
set desc "editing... editedshot-?.$two"
set dst [list [exec slurp -w 0] - | swappy -f -]
}
s - screen {
set desc screenshot-[exec date +%s].$two
set dst $dir/$desc
}
default {
puts stderr "unrecognised command: $one"
ufail "shot [help]"
}
}
if {{e} eq $one} {
exec {*}$notify shot $desc
exec grim {*}$opt {*}$dst
} else {
exec grim {*}$opt {*}$dst
exec {*}$notify shot $desc
}
}
###############################################################################
# for wm
proc vol {args} {
set title vol
set notify "notify-send -u low"
set icon_bt_lo [__nicon panel/bluetooth-disabled.svg]
set icon_usb [__nicon devices/drive-removable-media-usb.svg]
set icon_vol_good [__nicon panel/audio-ready.svg]
set icon_vol_off [__nicon panel/audio-off.svg]
set icon_vol_on [__nicon panel/audio-on.svg]
set icon_vol_lo [__nicon panel/audio-volume-medium.svg]
set icon_vol_hi [__nicon panel/audio-volume-high.svg]
set icon_mic_good [__nicon panel/mic-ready.svg]
set icon_mic_off [__nicon panel/mic-off.svg]
set icon_mic_on [__nicon panel/mic-on.svg]
set icon_mic_lo [__nicon panel/microphone-sensitivity-medium.svg]
set icon_mic_hi [__nicon panel/microphone-sensitivity-high.svg]
# which?
set devs [dict create \
earfun B0_F1_A3_63_0A_66 \
fly 38_A2_8C_F9_CF_DF \
jbl 00_12_6F_EE_F0_F7 \
nothing 2C_BE_EB_78_2B_3B]
set dev laptop
set out {}
set mic {}
set base {ponymix --sink list}
set basemic {ponymix --source list}
set isbt false
set isusb false
switch -- [lindex $args 0] {
{bt} {
set args [lrange $args 1 end]
set isbt true
set pat {: bluez_output}
set patmic {: bluez_input}
if {[catch {exec {*}$base | rg $pat}]} {
exec {*}$notify $icon_bt_lo $title {no bt audio}
exit
}
set out [exec {*}$base | rg $pat | tail -1 | cut {-d } -f3]
if {[catch {exec {*}$basemic | rg $patmic}]} {
set mic {}
} else {
set mic [exec {*}$basemic | rg $patmic | tail -1 | cut {-d } -f3]
}
switch -regexp -- $out \
[dict get $devs earfun] { set dev earfun } \
[dict get $devs fly] { set dev fly } \
[dict get $devs jbl] { set dev jbl } \
[dict get $devs nothing] { set dev nothing } \
[string map {_ :} [dict get $devs earfun]] { set dev earfun } \
[string map {_ :} [dict get $devs fly]] { set dev fly } \
[string map {_ :} [dict get $devs jbl]] { set dev jbl } \
[string map {_ :} [dict get $devs nothing]] { set dev nothing } \
default { fail {unrecognised device} }
} \
{usb} {
set args [lrange $args 1 end]
set isusb true
set pat {_output\.usb-}
set patmic {_input\.usb-}
if {[catch {exec {*}$base | rg $pat}]} {
exec {*}$notify $icon_usb $title {no audio usb}
exit
}
set out [exec {*}$base | rg $pat | tail -1 | cut {-d } -f3]
if {[catch {exec {*}$basemic | rg $patmic}]} {
set mic {}
} else {
set mic [exec {*}$basemic | rg $patmic | tail -1 | cut {-d } -f3]
}
set dev usb
} \
default {
set patbase {_output\.pci-0000_00_1f}
set patmicbase {_input\.pci-0000_00_1f}
#set pat {HiFi__(Headphones|Speaker)__sink}
#set patmic {HiFi__Mic1__source}
set pat {analog-stereo}
set out [exec {*}$base | rg $patbase | rg $pat | cut {-d } -f3]
set mic [exec {*}$basemic | rg $patmicbase | rg $pat | cut {-d } -f3]
}
}
# mic?
set ismic false
if {{mic} eq [lindex $args 0]} {
if {{} eq $mic} {
if {$isbt} {
exec {*}$notify $icon_bt_lo $title {no bt audio mic}
}
if {$isusb} {
exec {*}$notify $icon_usb $title {no audio usb mic}
}
exit
}
set args [lrange $args 1 end]
set ismic true
}
# build command
set cmdbase "ponymix --sink --device $out"
if {$ismic} {
if {{} eq $mic} {
exit
}
set cmdbase "ponymix --source --device $mic"
}
set cmd $cmdbase
switch -- [string index [lindex $args 0] 0] {
{} {}
x { append cmd { toggle} }
- { append cmd " decrease [string range $args 1 end]" }
+ { append cmd " increase [string range $args 1 end]" }
default { ufail {vol [bt] [mic] |x|-N|+N} }
}
# execute ponymix
exec {*}$cmd
# build notification
switch -- [string index [lindex $args 0] 0] {
{} {
set arg " $icon_vol_good"
if {$ismic} { set arg " $icon_mic_good" }
append notify "$arg $title \"$dev [exec {*}$cmdbase get-volume]%\""
}
x {
set arg " $icon_vol_off $title \"$dev muted\""
if {$ismic} { set arg " $icon_mic_off $title \"$dev mic muted\"" }
if {[catch {exec {*}$cmdbase is-muted}]} {
set arg " $icon_vol_on $title \"$dev on\""
if {$ismic} { set arg " $icon_mic_on $title \"$dev mic on\"" }
}
append notify $arg
}
- {
set arg " $icon_vol_lo"
if {$ismic} { set arg " $icon_mic_lo" }
append notify "$arg $title \"$dev [exec {*}$cmdbase get-volume]%\""
}
+ {
set arg " $icon_vol_hi"
if {$ismic} { set arg " $icon_mic_hi" }
append notify "$arg $title \"$dev [exec {*}$cmdbase get-volume]%\""
}
}
exec {*}$notify
}
###############################################################################
# for waybar
proc weather {} {
if {[catch {exec curl -s {https://wttr.in/sg?0AQT&format=%t%x}} weather]} { puts {}; exit }
if {{U} eq [string index $weather 0]} { puts {?}; exit }
regexp {^\+?([0-9-]+.)C(.*)$} $weather weather deg sky
if {{} eq $weather} { puts {?}; exit }
switch -- $sky {
o { set sky }
= { set sky }
mmm { set sky }
mm { set sky }
m { set sky }
/// { set sky }
// { set sky }
/ { set sky }
. { set sky }
x { set sky }
x/ { set sky }
** { set sky }
* { set sky }
*/* { set sky }
*/ { set sky }
/!/ { set sky }
!/ { set sky }
*!* { set sky }
}
puts "$deg$sky"
}
###############################################################################
set arg [lindex $::argv 0]
if {0 eq [llength [info procs $arg]]} {
ufail {btbat|cpu|lay|lum|mon|net|shot|vol|weather
btbat get battery level of bluetooth device
cpu get cpu governor
lay [gs]et keyboard layout _|h_hsnt|q_werty
lum [gs]et screen brightness _|-N|+N
mon toggle monitor _|o|on|x|off
net get net usage
shot take screenshot _|active|*screen*|select |jpg|*png* |c
vol [gs]et audio volume _|bt |mic |x|-N|+N
weather get weather report}
}
$arg {*}[lrange $::argv 1 end]