3 ## notify_done : unit -> unit
5 local -r _status_code
="$?"
8 _timestamp
="$(timestamp)"
9 local -r _msg
="$_timestamp [$_program] done "
10 if [[ "$_status_code" -eq 0 ]]
12 notify-send
-u normal
"$_msg OK: $_status_code"
14 notify-send
-u critical
"$_msg ERROR: $_status_code"
23 BEGIN {_s = tolower(_s)}
25 # TODO fzf/dmenu select instead of searching:
26 /^[a-zA-Z]/ && tolower($1) ~ _s && NF >= 2 {
39 } # TODO What would NF > 4 mean?
41 printf("%d [O] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
46 printf("%d [C] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
49 printf "%s", p # XXX Intentionally avoiding newline in the result.
56 printf "[ERROR] Found nothing.\n" > "/dev/stderr"
59 # TODO fzf-select which of the records the user (ahem, me) wants.
60 printf "[WARNING] Found more than one record.\n" > "/dev/stderr"
70 ## ws : string -> unit
72 local line search_string0 search_string
75 case "$search_string0" in
77 while read -r line
; do
78 search_string
="${search_string} ${line}"
81 search_string
="$search_string0";;
84 firefox
--search "$search_string"
89 ## d : string -> string list
91 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
95 ## shell_activity_report : (mon | dow) -> string list
96 shell_activity_report
() {
97 # TODO: optional concrete number output
98 # TODO: optional combinations of granularities: hour, weekday, month, year
105 echo "Usage: $0 [mon|dow]" >&2
109 |
awk -v group_by
="$group_by" '
110 function date2dow(y, m, d, _t, _i) {
112 # y > 1752, 1 <= m <= 12.
115 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
129 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
130 _i = _i == 0 ? 7 : _i # Make Sunday last
136 # NOTE: $2 & $3 are specific to oh-my-zsh history output
139 d_fields = split(date, d, "-")
140 t_fields = split(time, t, ":")
141 if (t_fields && d_fields) {
142 # +0 to coerce number from string
147 dow = date2dow(year, month, day)
148 g = group_by == "mon" ? month : dow # dow is default
177 n = group_by == "mon" ? 12 : 7 # dow is default
179 for (gid = 1; gid <= n; gid++) {
180 group = group_by == "mon" ? m[gid] : w[gid]
181 printf "%s\n", group;
182 for (hour=0; hour<24; hour++) {
185 for (i = 1; i <= (c * 100) / max; i++)
193 ## top_commands : unit -> (command:string * count:number * bar:string) list
203 print count[cmd], cmd
210 c = count[NR] = $1 + 0 # + 0 to coerce number from string
216 for (i = 1; i <= NR; i++) {
218 printf "%s %d ", cmd[i], c
219 scaled = (c * 100) / max
220 for (j = 1; j <= scaled; j++)
228 ## Top Disk-Using directories
229 ## TODO: Consider using numfmt instead of awk
230 ## tdu : path-string -> (size:number * directory:path-string) list
232 local -r root_path
="$1"
239 sub("^" $1 "\t+", "", path)
246 for (path in paths) {
248 pct = 100 * (size / max)
249 gb = size / 1024 / 1024
250 printf("%6.2f %3d%% %s\n", gb, pct, path)
257 # A slight optimization: head can exit before traversing the full input.
260 ## Top Disk-Using Files
261 ## tduf : path-string list -> (size:number * file:path-string) list
263 find "$@" -type f
-printf '%s\t%p\0' \
270 sub("^" $1 "\t+", "", path)
271 gb = size / 1024 / 1024 / 1024
272 printf("%f\t%s\n", gb, path)
276 # Most-recently modified file system objects
277 ## recent : ?(path-string list) -> path-string list
280 # - %T+ is a GNU extension;
281 # - gawk is able to split records on \0, while awk cannot.
282 find "$@" -printf '%T@ %T+ %p\0' \
283 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
284 |
sort -z -k 1 -n -r \
285 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
288 sub("^" $1 " +", "") # Remove epoch time
289 sub("+", " ") # Blank-out the default separator
290 sub("\\.[0-9]+", "") # Remove fractional seconds
295 ## recent_dirs : ?(path-string list) -> path-string list
300 ## recent_files : ?(path-string list) -> path-string list
305 ## pa_def_sink : unit -> string
307 pactl info |
awk '/^Default Sink:/ {print $3}'
310 ## void_pkgs : ?(string) -> json
312 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
316 ## man : string -> string
320 # me: end bold, blink and underline
322 # so: begin standout (reverse video)
325 # us: begin underline
328 LESS_TERMCAP_md
=$
'\e[01;30m' \
329 LESS_TERMCAP_me
=$
'\e[0m' \
330 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
331 LESS_TERMCAP_se
=$
'\e[0m' \
332 LESS_TERMCAP_us
=$
'\e[01;33m' \
333 LESS_TERMCAP_ue
=$
'\e[0m' \
338 ## x : string list -> unit
340 cd "$(~/bin/x $@)" ||
kill -INT $$
344 ## hump : unit -> unit
346 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
350 ## howto : unit -> string
352 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
356 local -r base_dir
="$1"
360 local -r yt
=youtube-dl
361 local -r id
=$
("$yt" --get-id "$uri")
362 local -r title
=$
("$yt" --get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
363 local -r dir
="${base_dir}/${title}--${id}"
366 cd "$dir" ||
kill -INT $$
368 "$yt" $opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
373 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
378 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
382 local -r user_type
="$1"
383 local -r user_name
="$2"
385 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
389 local -r gh_user_type
="$1"
390 local -r gh_user_name
="$2"
392 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
394 cd "$gh_dir" ||
kill -INT $$
395 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
396 | jq
--raw-output '.[] | select(.fork | not) | .clone_url' \
402 gh_clone
'users' "$1"
410 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
411 gh_dir
="${DIR_GITHUB}/${gh_username}"
413 cd "$gh_dir" ||
kill -INT $$
417 work_log_template
() {
439 mkdir
-p "$DIR_WORK_LOG"
440 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
441 if [ ! -f "$file_work_log_today" ]
443 work_log_template
> "$file_work_log_today"
445 vim
-c 'set spell' "$file_work_log_today"
450 mkdir
-p "$DIR_NOTES"
451 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
455 # grep's defintion of a line does not include \r, wile awk's does and
456 # which bluetoothctl outputs
457 awk '/^Device +/ {print $2}' \
458 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
459 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
463 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
467 echo 'devices' | bluetoothctl | _bt_devs_infos
471 local -r stderr
="$(mktemp)"
475 $@
2> >(tee "$stderr")
478 0) urgency
='normal';;
479 *) urgency
='critical'
481 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
489 width = width ? width : 80
490 ch_left = ch_left ? ch_left : "["
491 ch_right = ch_right ? ch_right : "]"
492 ch_blank = ch_blank ? ch_blank : "-"
493 ch_used = ch_used ? ch_used : "|"
503 cur_scaled = num_scale(cur, max, 1, width)
507 lab ? lab " " : "", \
508 num ? cur "/" max " " : "", \
509 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
511 for (i=1; i<=width; i++) {
512 c = i <= cur_scaled ? ch_used : ch_blank
515 printf "%s\n", ch_right
518 function num_scale(src_cur, src_max, dst_min, dst_max) {
519 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
532 pct = cur / max * 100
533 printf "%s%s %.2f%%", sep, name, pct
542 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
548 if which upower
> /dev
/null
557 / battery/ && device["path"] {
558 device["is_battery"] = 1
562 / percentage:/ && device["is_battery"] {
563 device["battery_percentage"] = $2
564 sub("%$", "", device["battery_percentage"])
569 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
570 print device["battery_percentage"], 100, "batt"
580 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
584 local -r indent_unit
=' '
594 printf '%stmux\n%ssessions %d, clients %d\n' \
596 "${indent_unit}${indent_unit}" \
597 "$(tmux list-sessions 2> /dev/null | wc -l)" \
598 "$(tmux list-clients 2> /dev/null | wc -l)"
602 printf '%sprocs by user\n' "${indent_unit}"
611 for (user in count_by_user)
612 print count_by_user[user], total, user
616 | indent
"${indent_unit}${indent_unit}"
622 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
623 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
626 | bar_gauge
-v width
=60 -v pct
=1 \
628 | indent
"$indent_unit"
632 printf '%smem by proc\n' "$indent_unit"
634 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
642 for (proc in by_proc)
643 print by_proc[proc], total, proc
647 | indent
"${indent_unit}${indent_unit}"
651 local _dir temp_input label_file label
653 printf '%sthermal\n' "$indent_unit"
654 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
656 find "$_dir"/ -name 'temp*_input' \
657 |
while read -r temp_input
; do
658 label_file
=${temp_input//_input/_label}
659 if [ -f "$label_file" ]; then
660 label
=$
(< "$label_file")
664 awk -v label
="$label" '{
666 label = sprintf(" (%s)", label)
667 printf("%.2f°C%s\n", $1 / 1000, label)
672 | indent
"$indent_unit"
674 | indent
"${indent_unit}${indent_unit}"
677 #local -r internet_addr=$(internet_addr 0.5)
678 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
680 #echo "${indent_unit}internet"
681 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
682 echo "${indent_unit}if"
683 (ifconfig
; iwconfig
) 2> /dev
/null \
687 sub(":$", "", device)
690 sub("^ESSID:\"", "", _essid)
691 sub("\"$", "", _essid)
692 essid[device] = _essid
697 /^ / && $1 == "inet" {
702 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
703 split($2, lq_parts_eq, "=")
704 split(lq_parts_eq[2], lq_parts_slash, "/")
705 cur = lq_parts_slash[1]
706 max = lq_parts_slash[2]
707 link[device] = cur / max * 100
712 for (device in address)
713 if (device != "lo") {
716 l = l ? sprintf("%.0f%%", l) : "--"
718 print device, address[device], e, l
723 | indent
"${indent_unit}${indent_unit}"
725 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
727 echo "${indent_unit}-->"
729 sudo
-n netstat
-tulnp \
730 |
awk -v indent
="${indent_unit}${indent_unit}" '
731 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
734 port = a[split(addr, a, ":")]
735 name = p[split(proc, p, "/")]
737 protocols[protocol] = 1
738 if (!seen[protocol, name, port]++)
739 ports[protocol, name, ++seen[protocol, name]] = port
743 for (protocol in protocols) {
744 printf "%s%s\t", indent, toupper(protocol)
745 for (name in names) {
746 if (n = seen[protocol, name]) {
749 for (i = 1; i <= n; i++) {
750 printf "%s%d", sep, ports[protocol, name, i]
760 echo "${indent_unit}<->"
762 printf '%sTCP\t' "${indent_unit}${indent_unit}"
763 sudo
-n netstat
-tnp \
764 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
765 |
awk '{sub("^[0-9]+/", ""); print}' \
770 # TODO: iptables summary
773 ssh_invalid_by_addr
() {
775 /: Invalid user/ && $5 ~ /^sshd/ {
776 addr=$10 == "port" ? $9 : $10
782 for (addr in by_addr)
783 if ((c = by_addr[addr]) > 1)
784 printf "%d %d %s\n", c, max, addr
788 /var
/log
/auth.log
.1 \
790 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
794 ssh_invalid_by_day
() {
811 /: Invalid user/ && $5 ~ /^sshd/ {
819 if ((c = by_day[day]) > 1)
820 printf "%d %d %s\n", c, max, day
824 /var
/log
/auth.log
.1 \
826 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
830 ssh_invalid_by_user
() {
832 /: Invalid user/ && $5 ~ /^sshd/ {
839 for (user in by_user)
840 if ((c = by_user[user]) > 1)
841 printf "%d %d %s\n", c, max, user
845 /var
/log
/auth.log
.1 \
847 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
855 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
857 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
860 n = split($1, path, "/") # prog may be in path form
868 print count[prog], total, prog
871 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \