8 BEGIN {_s = tolower(_s)}
10 /^[a-zA-Z]/ && tolower($1) ~ _s && NF >= 2 {
23 } # TODO What would NF > 4 mean?
25 printf("%d [O] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
30 printf("%d [C] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
33 printf "%s", p # XXX Intentionally avoiding newline in the result.
41 ## ws : string -> unit
43 local line search_string0 search_string
46 case "$search_string0" in
48 while read -r line
; do
49 search_string
="${search_string} ${line}"
52 search_string
="$search_string0";;
55 firefox
--search "$search_string"
60 ## d : string -> string list
62 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
66 ## shell_activity_report : (mon | dow) -> string list
67 shell_activity_report
() {
68 # TODO: optional concrete number output
69 # TODO: optional combinations of granularities: hour, weekday, month, year
76 echo "Usage: $0 [mon|dow]" >&2
80 |
awk -v group_by
="$group_by" '
81 function date2dow(y, m, d, _t, _i) {
83 # y > 1752, 1 <= m <= 12.
86 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
100 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
101 _i = _i == 0 ? 7 : _i # Make Sunday last
107 # NOTE: $2 & $3 are specific to oh-my-zsh history output
110 d_fields = split(date, d, "-")
111 t_fields = split(time, t, ":")
112 if (t_fields && d_fields) {
113 # +0 to coerce number from string
118 dow = date2dow(year, month, day)
119 g = group_by == "mon" ? month : dow # dow is default
148 n = group_by == "mon" ? 12 : 7 # dow is default
150 for (gid = 1; gid <= n; gid++) {
151 group = group_by == "mon" ? m[gid] : w[gid]
152 printf "%s\n", group;
153 for (hour=0; hour<24; hour++) {
156 for (i = 1; i <= (c * 100) / max; i++)
164 ## top_commands : unit -> (command:string * count:number * bar:string) list
174 print count[cmd], cmd
181 c = count[NR] = $1 + 0 # + 0 to coerce number from string
187 for (i = 1; i <= NR; i++) {
189 printf "%s %d ", cmd[i], c
190 scaled = (c * 100) / max
191 for (j = 1; j <= scaled; j++)
199 ## Top Disk-Using directories
200 ## TODO: Consider using numfmt instead of awk
201 ## tdu : path-string -> (size:number * directory:path-string) list
203 local -r root_path
="$1"
210 sub("^" $1 "\t+", "", path)
217 for (path in paths) {
219 pct = 100 * (size / max)
220 gb = size / 1024 / 1024
221 printf("%6.2f %3d%% %s\n", gb, pct, path)
228 # A slight optimization: head can exit before traversing the full input.
231 ## Top Disk-Using Files
232 ## tduf : path-string list -> (size:number * file:path-string) list
234 find "$@" -type f
-printf '%s\t%p\0' \
241 sub("^" $1 "\t+", "", path)
242 gb = size / 1024 / 1024 / 1024
243 printf("%f\t%s\n", gb, path)
247 # Most-recently modified file system objects
248 ## recent : ?(path-string list) -> path-string list
251 # - %T+ is a GNU extension;
252 # - gawk is able to split records on \0, while awk cannot.
253 find "$@" -printf '%T@ %T+ %p\0' \
254 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
255 |
sort -z -k 1 -n -r \
256 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
259 sub("^" $1 " +", "") # Remove epoch time
260 sub("+", " ") # Blank-out the default separator
261 sub("\\.[0-9]+", "") # Remove fractional seconds
266 ## recent_dirs : ?(path-string list) -> path-string list
271 ## recent_files : ?(path-string list) -> path-string list
276 ## pa_def_sink : unit -> string
278 pactl info |
awk '/^Default Sink:/ {print $3}'
281 ## void_pkgs : ?(string) -> json
283 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
287 ## man : string -> string
291 # me: end bold, blink and underline
293 # so: begin standout (reverse video)
296 # us: begin underline
299 LESS_TERMCAP_md
=$
'\e[01;30m' \
300 LESS_TERMCAP_me
=$
'\e[0m' \
301 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
302 LESS_TERMCAP_se
=$
'\e[0m' \
303 LESS_TERMCAP_us
=$
'\e[01;33m' \
304 LESS_TERMCAP_ue
=$
'\e[0m' \
309 ## x : string list -> unit
311 cd "$(~/bin/x $@)" ||
kill -INT $$
315 ## hump : unit -> unit
317 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
321 ## howto : unit -> string
323 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
327 local -r base_dir
="$1"
331 local -r id
=$
(youtube-dlc
--get-id "$uri")
332 local -r title
=$
(youtube-dlc
--get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
333 local -r dir
="${base_dir}/${title}--${id}"
336 cd "$dir" ||
kill -INT $$
338 youtube-dlc
$opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
343 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
348 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
352 local -r user_type
="$1"
353 local -r user_name
="$2"
355 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
359 local -r gh_user_type
="$1"
360 local -r gh_user_name
="$2"
362 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
364 cd "$gh_dir" ||
kill -INT $$
365 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
366 | jq
--raw-output '.[] | select(.fork | not) | .clone_url' \
372 gh_clone
'users' "$1"
380 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
381 gh_dir
="${DIR_GITHUB}/${gh_username}"
383 cd "$gh_dir" ||
kill -INT $$
387 work_log_template
() {
409 mkdir
-p "$DIR_WORK_LOG"
410 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
411 if [ ! -f "$file_work_log_today" ]
413 work_log_template
> "$file_work_log_today"
415 vim
-c 'set spell' "$file_work_log_today"
420 mkdir
-p "$DIR_NOTES"
421 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
425 # grep's defintion of a line does not include \r, wile awk's does and
426 # which bluetoothctl outputs
427 awk '/^Device +/ {print $2}' \
428 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
429 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
433 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
437 echo 'devices' | bluetoothctl | _bt_devs_infos
441 local -r stderr
="$(mktemp)"
445 $@
2> >(tee "$stderr")
448 0) urgency
='normal';;
449 *) urgency
='critical'
451 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
459 width = width ? width : 80
460 ch_left = ch_left ? ch_left : "["
461 ch_right = ch_right ? ch_right : "]"
462 ch_blank = ch_blank ? ch_blank : "-"
463 ch_used = ch_used ? ch_used : "|"
473 cur_scaled = num_scale(cur, max, 1, width)
477 lab ? lab " " : "", \
478 num ? cur "/" max " " : "", \
479 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
481 for (i=1; i<=width; i++) {
482 c = i <= cur_scaled ? ch_used : ch_blank
485 printf "%s\n", ch_right
488 function num_scale(src_cur, src_max, dst_min, dst_max) {
489 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
502 pct = cur / max * 100
503 printf "%s%s %.2f%%", sep, name, pct
512 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
518 if which upower
> /dev
/null
527 / battery/ && device["path"] {
528 device["is_battery"] = 1
532 / percentage:/ && device["is_battery"] {
533 device["battery_percentage"] = $2
534 sub("%$", "", device["battery_percentage"])
539 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
540 print device["battery_percentage"], 100, "batt"
550 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
554 local -r indent_unit
=' '
564 printf '%stmux\n%ssessions %d, clients %d\n' \
566 "${indent_unit}${indent_unit}" \
567 "$(tmux list-sessions 2> /dev/null | wc -l)" \
568 "$(tmux list-clients 2> /dev/null | wc -l)"
572 printf '%sprocs by user\n' "${indent_unit}"
581 for (user in count_by_user)
582 print count_by_user[user], total, user
586 | indent
"${indent_unit}${indent_unit}"
592 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
593 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
596 | bar_gauge
-v width
=60 -v pct
=1 \
598 | indent
"$indent_unit"
602 printf '%smem by proc\n' "$indent_unit"
604 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
612 for (proc in by_proc)
613 print by_proc[proc], total, proc
617 | indent
"${indent_unit}${indent_unit}"
621 local _dir temp_input label_file label
623 printf '%sthermal\n' "$indent_unit"
624 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
626 find "$_dir"/ -name 'temp*_input' \
627 |
while read -r temp_input
; do
628 label_file
=${temp_input//_input/_label}
629 if [ -f "$label_file" ]; then
630 label
=$
(< "$label_file")
634 awk -v label
="$label" '{
636 label = sprintf(" (%s)", label)
637 printf("%.2f°C%s\n", $1 / 1000, label)
642 | indent
"$indent_unit"
644 | indent
"${indent_unit}${indent_unit}"
647 #local -r internet_addr=$(internet_addr 0.5)
648 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
650 #echo "${indent_unit}internet"
651 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
652 echo "${indent_unit}if"
653 (ifconfig
; iwconfig
) 2> /dev
/null \
657 sub(":$", "", device)
660 sub("^ESSID:\"", "", _essid)
661 sub("\"$", "", _essid)
662 essid[device] = _essid
667 /^ / && $1 == "inet" {
672 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
673 split($2, lq_parts_eq, "=")
674 split(lq_parts_eq[2], lq_parts_slash, "/")
675 cur = lq_parts_slash[1]
676 max = lq_parts_slash[2]
677 link[device] = cur / max * 100
682 for (device in address)
683 if (device != "lo") {
686 l = l ? sprintf("%.0f%%", l) : "--"
688 print device, address[device], e, l
693 | indent
"${indent_unit}${indent_unit}"
695 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
697 echo "${indent_unit}-->"
699 sudo
-n netstat
-tulnp \
700 |
awk -v indent
="${indent_unit}${indent_unit}" '
701 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
704 port = a[split(addr, a, ":")]
705 name = p[split(proc, p, "/")]
707 protocols[protocol] = 1
708 if (!seen[protocol, name, port]++)
709 ports[protocol, name, ++seen[protocol, name]] = port
713 for (protocol in protocols) {
714 printf "%s%s\t", indent, toupper(protocol)
715 for (name in names) {
716 if (n = seen[protocol, name]) {
719 for (i = 1; i <= n; i++) {
720 printf "%s%d", sep, ports[protocol, name, i]
730 echo "${indent_unit}<->"
732 printf '%sTCP\t' "${indent_unit}${indent_unit}"
733 sudo
-n netstat
-tnp \
734 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
735 |
awk '{sub("^[0-9]+/", ""); print}' \
740 # TODO: iptables summary
743 ssh_invalid_by_addr
() {
745 /: Invalid user/ && $5 ~ /^sshd/ {
746 addr=$10 == "port" ? $9 : $10
752 for (addr in by_addr)
753 if ((c = by_addr[addr]) > 1)
754 printf "%d %d %s\n", c, max, addr
758 /var
/log
/auth.log
.1 \
760 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
764 ssh_invalid_by_day
() {
781 /: Invalid user/ && $5 ~ /^sshd/ {
789 if ((c = by_day[day]) > 1)
790 printf "%d %d %s\n", c, max, day
794 /var
/log
/auth.log
.1 \
796 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
800 ssh_invalid_by_user
() {
802 /: Invalid user/ && $5 ~ /^sshd/ {
809 for (user in by_user)
810 if ((c = by_user[user]) > 1)
811 printf "%d %d %s\n", c, max, user
815 /var
/log
/auth.log
.1 \
817 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
825 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
827 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
830 n = split($1, path, "/") # prog may be in path form
838 print count[prog], total, prog
841 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \