3 ## open : string -> unit
5 ## Fork xdg-open so we don't block current terminal session when opening
6 ## things like pdf files. For example:
14 ## notify_done : unit -> unit
16 local -r _status_code
="$?"
17 local -r _program
="$1"
19 _timestamp
="$(timestamp)"
20 local -r _msg
="$_timestamp [$_program] done "
21 if [[ "$_status_code" -eq 0 ]]
23 notify-send
-u normal
"$_msg OK: $_status_code"
25 notify-send
-u critical
"$_msg ERROR: $_status_code"
40 local -r timestamp
="$(date --iso-8601=ns)"
41 local -r dir
="$HOME"/dl
/adhoc
/"$timestamp"--"$name"
42 local -r url_file_path
="${dir}/url"
43 local -r dl_file_path
="${dir}/dl"
46 touch "$url_file_path"
49 echo "$url" > "$url_file_path"
51 _dl_script
> "$dl_file_path"
52 chmod +x
"$dl_file_path"
57 ## ws : string -> unit
59 local line search_string0 search_string
62 case "$search_string0" in
64 while read -r line
; do
65 search_string
="${search_string} ${line}"
68 search_string
="$search_string0";;
71 firefox
--search "$search_string"
76 ## d : string -> string list
78 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
82 ## shell_activity_report : (mon | dow) -> string list
83 shell_activity_report
() {
84 # TODO: optional concrete number output
85 # TODO: optional combinations of granularities: hour, weekday, month, year
92 echo "Usage: $0 [mon|dow]" >&2
96 |
awk -v group_by
="$group_by" '
97 function date2dow(y, m, d, _t, _i) {
99 # y > 1752, 1 <= m <= 12.
102 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
116 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
117 _i = _i == 0 ? 7 : _i # Make Sunday last
123 # NOTE: $2 & $3 are specific to oh-my-zsh history output
126 d_fields = split(date, d, "-")
127 t_fields = split(time, t, ":")
128 if (t_fields && d_fields) {
129 # +0 to coerce number from string
134 dow = date2dow(year, month, day)
135 g = group_by == "mon" ? month : dow # dow is default
164 n = group_by == "mon" ? 12 : 7 # dow is default
166 for (gid = 1; gid <= n; gid++) {
167 group = group_by == "mon" ? m[gid] : w[gid]
168 printf "%s\n", group;
169 for (hour=0; hour<24; hour++) {
172 for (i = 1; i <= (c * 100) / max; i++)
180 ## top_commands : unit -> (command:string * count:number * bar:string) list
190 print count[cmd], cmd
197 c = count[NR] = $1 + 0 # + 0 to coerce number from string
203 for (i = 1; i <= NR; i++) {
205 printf "%s %d ", cmd[i], c
206 scaled = (c * 100) / max
207 for (j = 1; j <= scaled; j++)
215 ## Top Disk-Using directories
216 ## tdu : path-string -> (size:number * directory:path-string) list
218 local -r root_path
="$1"
224 # A slight optimization: head can exit before traversing the full input.
227 ## Top Disk-Using Files
228 ## tduf : path-string list -> (size:number * file:path-string) list
230 find "$@" -type f
-printf '%s\t%p\0' \
233 | numfmt
-z --to=iec \
237 # Most-recently modified file system objects
238 ## recent : ?(path-string list) -> path-string list
241 # - %T+ is a GNU extension;
242 # - gawk is able to split records on \0, while awk cannot.
243 find "$@" -printf '%T@ %T+ %p\0' \
244 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
245 |
sort -z -k 1 -n -r \
246 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
249 sub("^" $1 " +", "") # Remove epoch time
250 sub("+", " ") # Blank-out the default separator
251 sub("\\.[0-9]+", "") # Remove fractional seconds
256 ## recent_dirs : ?(path-string list) -> path-string list
261 ## recent_files : ?(path-string list) -> path-string list
266 ## pa_def_sink : unit -> string
268 pactl info |
awk '/^Default Sink:/ {print $3}'
271 ## void_pkgs : ?(string) -> json
273 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
277 ## man : string -> string
281 # me: end bold, blink and underline
283 # so: begin standout (reverse video)
286 # us: begin underline
289 LESS_TERMCAP_md
=$
'\e[01;30m' \
290 LESS_TERMCAP_me
=$
'\e[0m' \
291 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
292 LESS_TERMCAP_se
=$
'\e[0m' \
293 LESS_TERMCAP_us
=$
'\e[01;33m' \
294 LESS_TERMCAP_ue
=$
'\e[0m' \
299 ## x : string list -> unit
301 cd "$(~/bin/x $@)" ||
kill -INT $$
305 ## hump : unit -> unit
307 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
311 ## howto : unit -> string
313 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
317 local -r base_dir
="$1"
321 local -r yt
=youtube-dl
322 local -r id
=$
("$yt" --get-id "$uri")
323 local -r title
=$
("$yt" --get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
324 local -r dir
="${base_dir}/${title}--${id}"
327 cd "$dir" ||
kill -INT $$
329 "$yt" $opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
334 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
339 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
343 local -r user_type
="$1"
344 local -r user_name
="$2"
346 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
350 local -r gh_user_type
="$1"
351 local -r gh_user_name
="$2"
353 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
355 cd "$gh_dir" ||
kill -INT $$
356 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
357 | jq
--raw-output '.[] | select(.fork | not) | .clone_url' \
363 gh_clone
'users' "$1"
371 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
372 gh_dir
="${DIR_GITHUB}/${gh_username}"
374 cd "$gh_dir" ||
kill -INT $$
379 local -r len
="${1:-79}" # 1st arg or 79.
380 local -r char
="${2:--}" # 2nd arg or a dash.
381 for _
in {1..
"$len"}; do
386 daily_todo_file_template
() {
388 ===============================================================================
390 ===============================================================================
392 -------------------------------------------------------------------------------
394 -------------------------------------------------------------------------------
397 -------------------------------------------------------------------------------
399 -------------------------------------------------------------------------------
402 -------------------------------------------------------------------------------
404 -------------------------------------------------------------------------------
407 -------------------------------------------------------------------------------
409 -------------------------------------------------------------------------------
416 local -r dir
="$DIR_TODO/daily"
417 local -r file="$dir/$date.txt"
422 daily_todo_file_template
> "$file"
424 cd "$DIR_TODO" && "$EDITOR" $EDITOR_ARGS "$file"
428 cd "$DIR_TODO" && "$EDITOR" TODO
431 work_log_template
() {
453 mkdir
-p "$DIR_WORK_LOG"
454 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
455 if [ ! -f "$file_work_log_today" ]
457 work_log_template
> "$file_work_log_today"
459 vim
-c 'set spell' "$file_work_log_today"
464 mkdir
-p "$DIR_NOTES"
465 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
469 # grep's defintion of a line does not include \r, wile awk's does and
470 # which bluetoothctl outputs
471 awk '/^Device +/ {print $2}' \
472 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
473 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
477 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
481 echo 'devices' | bluetoothctl | _bt_devs_infos
485 local -r stderr
="$(mktemp)"
489 $@
2> >(tee "$stderr")
492 0) urgency
='normal';;
493 *) urgency
='critical'
495 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
503 width = width ? width : 80
504 ch_left = ch_left ? ch_left : "["
505 ch_right = ch_right ? ch_right : "]"
506 ch_blank = ch_blank ? ch_blank : "-"
507 ch_used = ch_used ? ch_used : "|"
517 cur_scaled = num_scale(cur, max, 1, width)
521 lab ? lab " " : "", \
522 num ? cur "/" max " " : "", \
523 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
525 for (i=1; i<=width; i++) {
526 c = i <= cur_scaled ? ch_used : ch_blank
529 printf "%s\n", ch_right
532 function num_scale(src_cur, src_max, dst_min, dst_max) {
533 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
546 pct = cur / max * 100
547 printf "%s%s %.2f%%", sep, name, pct
556 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
562 if which upower
> /dev
/null
571 / battery/ && device["path"] {
572 device["is_battery"] = 1
576 / percentage:/ && device["is_battery"] {
577 device["battery_percentage"] = $2
578 sub("%$", "", device["battery_percentage"])
583 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
584 print device["battery_percentage"], 100, "batt"
594 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
598 local -r indent_unit
=' '
608 # TODO Bring back seesion and client listing, but per server/socket.
609 printf '%stmux\n' "$indent_unit"
612 # Expecting lines like:
613 # "tmux: server tmux -L pistactl new-session -d -s pistactl"
614 # "tmux: client tmux -L foo"
615 # "tmux: client tmux -Lbar"
616 # "tmux: client tmux"
617 # "tmux: server tmux -L foo -S bar" <-- -S takes precedence
619 # XXX This of course assumes pervasive usage of -L
623 split($0, sides_of_S, "-S")
624 split(sides_of_S[2], words_right_of_S, FS)
626 split($0, sides_of_L, "-L")
627 split(sides_of_L[2], words_right_of_L, FS)
629 if (words_right_of_S[1]) {
630 sock = "path." words_right_of_S[1]
631 } else if (words_right_of_L[1]) {
632 sock = "name." words_right_of_L[1]
643 for (sock in socks) {
644 clients = count["client", sock]
647 printf "<-> %d", clients
655 | indent
"${indent_unit}${indent_unit}"
659 printf '%sprocs by user\n' "${indent_unit}"
668 for (user in count_by_user)
669 print count_by_user[user], total, user
673 | indent
"${indent_unit}${indent_unit}"
679 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
680 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
683 | bar_gauge
-v width
=60 -v pct
=1 \
685 | indent
"$indent_unit"
689 printf '%smem by proc\n' "$indent_unit"
691 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
699 for (proc in by_proc)
700 print by_proc[proc], total, proc
704 | indent
"${indent_unit}${indent_unit}"
708 local _dir temp_input label_file label
710 printf '%sthermal\n' "$indent_unit"
711 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
713 find "$_dir"/ -name 'temp*_input' \
714 |
while read -r temp_input
; do
715 label_file
=${temp_input//_input/_label}
716 if [ -f "$label_file" ]; then
717 label
=$
(< "$label_file")
721 awk -v label
="$label" '{
723 label = sprintf(" (%s)", label)
724 printf("%.2f°C%s\n", $1 / 1000, label)
729 | indent
"$indent_unit"
731 | indent
"${indent_unit}${indent_unit}"
734 #local -r internet_addr=$(internet_addr 0.5)
735 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
737 #echo "${indent_unit}internet"
738 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
739 echo "${indent_unit}if"
740 (ifconfig
; iwconfig
) 2> /dev
/null \
744 sub(":$", "", device)
747 sub("^ESSID:\"", "", _essid)
748 sub("\"$", "", _essid)
749 essid[device] = _essid
754 /^ / && $1 == "inet" {
759 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
760 split($2, lq_parts_eq, "=")
761 split(lq_parts_eq[2], lq_parts_slash, "/")
762 cur = lq_parts_slash[1]
763 max = lq_parts_slash[2]
764 link[device] = cur / max * 100
769 for (device in address)
770 if (device != "lo") {
773 l = l ? sprintf("%.0f%%", l) : "--"
775 print device, address[device], e, l
780 | indent
"${indent_unit}${indent_unit}"
782 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
784 echo "${indent_unit}-->"
786 # TODO Populate pid->cmd dict from `ps -eo pid,comm` and lookup progs there
787 # since netstat -p output comes out truncated.
789 sudo
-n netstat
-tulnp \
790 |
awk -v indent
="${indent_unit}${indent_unit}" '
791 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
794 port = a[split(addr, a, ":")]
795 name = p[split(proc, p, "/")]
797 protocols[protocol] = 1
798 if (!seen[protocol, name, port]++)
799 ports[protocol, name, ++seen[protocol, name]] = port
803 for (protocol in protocols) {
804 printf "%s%s\t", indent, toupper(protocol)
805 for (name in names) {
806 if (n = seen[protocol, name]) {
809 for (i = 1; i <= n; i++) {
810 printf "%s%d", sep, ports[protocol, name, i]
820 echo "${indent_unit}<->"
822 printf '%sTCP\t' "${indent_unit}${indent_unit}"
823 sudo
-n netstat
-tnp \
824 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
825 |
awk '{sub("^[0-9]+/", ""); print}' \
830 # TODO: iptables summary
833 ssh_invalid_by_addr
() {
835 /: Invalid user/ && $5 ~ /^sshd/ {
836 addr=$10 == "port" ? $9 : $10
842 for (addr in by_addr)
843 if ((c = by_addr[addr]) > 1)
844 printf "%d %d %s\n", c, max, addr
848 /var
/log
/auth.log
.1 \
850 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
854 ssh_invalid_by_day
() {
871 /: Invalid user/ && $5 ~ /^sshd/ {
879 if ((c = by_day[day]) > 1)
880 printf "%d %d %s\n", c, max, day
884 /var
/log
/auth.log
.1 \
886 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
890 ssh_invalid_by_user
() {
892 /: Invalid user/ && $5 ~ /^sshd/ {
899 for (user in by_user)
900 if ((c = by_user[user]) > 1)
901 printf "%d %d %s\n", c, max, user
905 /var
/log
/auth.log
.1 \
907 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
915 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
917 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
920 n = split($1, path, "/") # prog may be in path form
928 print count[prog], total, prog
931 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \