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"
34 BEGIN {_s = tolower(_s)}
36 # TODO fzf/dmenu select instead of searching:
37 /^[a-zA-Z]/ && tolower($1) ~ _s && NF >= 2 {
50 } # TODO What would NF > 4 mean?
52 printf("%d [O] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
57 printf("%d [C] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
60 printf "%s", p # XXX Intentionally avoiding newline in the result.
67 printf "[ERROR] Found nothing.\n" > "/dev/stderr"
70 # TODO fzf-select which of the records the user (ahem, me) wants.
71 printf "[WARNING] Found more than one record.\n" > "/dev/stderr"
81 ## ws : string -> unit
83 local line search_string0 search_string
86 case "$search_string0" in
88 while read -r line
; do
89 search_string
="${search_string} ${line}"
92 search_string
="$search_string0";;
95 firefox
--search "$search_string"
100 ## d : string -> string list
102 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
106 ## shell_activity_report : (mon | dow) -> string list
107 shell_activity_report
() {
108 # TODO: optional concrete number output
109 # TODO: optional combinations of granularities: hour, weekday, month, year
116 echo "Usage: $0 [mon|dow]" >&2
120 |
awk -v group_by
="$group_by" '
121 function date2dow(y, m, d, _t, _i) {
123 # y > 1752, 1 <= m <= 12.
126 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
140 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
141 _i = _i == 0 ? 7 : _i # Make Sunday last
147 # NOTE: $2 & $3 are specific to oh-my-zsh history output
150 d_fields = split(date, d, "-")
151 t_fields = split(time, t, ":")
152 if (t_fields && d_fields) {
153 # +0 to coerce number from string
158 dow = date2dow(year, month, day)
159 g = group_by == "mon" ? month : dow # dow is default
188 n = group_by == "mon" ? 12 : 7 # dow is default
190 for (gid = 1; gid <= n; gid++) {
191 group = group_by == "mon" ? m[gid] : w[gid]
192 printf "%s\n", group;
193 for (hour=0; hour<24; hour++) {
196 for (i = 1; i <= (c * 100) / max; i++)
204 ## top_commands : unit -> (command:string * count:number * bar:string) list
214 print count[cmd], cmd
221 c = count[NR] = $1 + 0 # + 0 to coerce number from string
227 for (i = 1; i <= NR; i++) {
229 printf "%s %d ", cmd[i], c
230 scaled = (c * 100) / max
231 for (j = 1; j <= scaled; j++)
239 ## Top Disk-Using directories
240 ## TODO: Consider using numfmt instead of awk
241 ## tdu : path-string -> (size:number * directory:path-string) list
243 local -r root_path
="$1"
250 sub("^" $1 "\t+", "", path)
257 for (path in paths) {
259 pct = 100 * (size / max)
260 gb = size / 1024 / 1024
261 printf("%6.2f %3d%% %s\n", gb, pct, path)
268 # A slight optimization: head can exit before traversing the full input.
271 ## Top Disk-Using Files
272 ## tduf : path-string list -> (size:number * file:path-string) list
274 find "$@" -type f
-printf '%s\t%p\0' \
281 sub("^" $1 "\t+", "", path)
282 gb = size / 1024 / 1024 / 1024
283 printf("%f\t%s\n", gb, path)
287 # Most-recently modified file system objects
288 ## recent : ?(path-string list) -> path-string list
291 # - %T+ is a GNU extension;
292 # - gawk is able to split records on \0, while awk cannot.
293 find "$@" -printf '%T@ %T+ %p\0' \
294 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
295 |
sort -z -k 1 -n -r \
296 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
299 sub("^" $1 " +", "") # Remove epoch time
300 sub("+", " ") # Blank-out the default separator
301 sub("\\.[0-9]+", "") # Remove fractional seconds
306 ## recent_dirs : ?(path-string list) -> path-string list
311 ## recent_files : ?(path-string list) -> path-string list
316 ## pa_def_sink : unit -> string
318 pactl info |
awk '/^Default Sink:/ {print $3}'
321 ## void_pkgs : ?(string) -> json
323 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
327 ## man : string -> string
331 # me: end bold, blink and underline
333 # so: begin standout (reverse video)
336 # us: begin underline
339 LESS_TERMCAP_md
=$
'\e[01;30m' \
340 LESS_TERMCAP_me
=$
'\e[0m' \
341 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
342 LESS_TERMCAP_se
=$
'\e[0m' \
343 LESS_TERMCAP_us
=$
'\e[01;33m' \
344 LESS_TERMCAP_ue
=$
'\e[0m' \
349 ## x : string list -> unit
351 cd "$(~/bin/x $@)" ||
kill -INT $$
355 ## hump : unit -> unit
357 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
361 ## howto : unit -> string
363 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
367 local -r base_dir
="$1"
371 local -r yt
=youtube-dl
372 local -r id
=$
("$yt" --get-id "$uri")
373 local -r title
=$
("$yt" --get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
374 local -r dir
="${base_dir}/${title}--${id}"
377 cd "$dir" ||
kill -INT $$
379 "$yt" $opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
384 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
389 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
393 local -r user_type
="$1"
394 local -r user_name
="$2"
396 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
400 local -r gh_user_type
="$1"
401 local -r gh_user_name
="$2"
403 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
405 cd "$gh_dir" ||
kill -INT $$
406 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
407 | jq
--raw-output '.[] | select(.fork | not) | .clone_url' \
413 gh_clone
'users' "$1"
421 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
422 gh_dir
="${DIR_GITHUB}/${gh_username}"
424 cd "$gh_dir" ||
kill -INT $$
428 work_log_template
() {
450 mkdir
-p "$DIR_WORK_LOG"
451 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
452 if [ ! -f "$file_work_log_today" ]
454 work_log_template
> "$file_work_log_today"
456 vim
-c 'set spell' "$file_work_log_today"
461 mkdir
-p "$DIR_NOTES"
462 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
466 # grep's defintion of a line does not include \r, wile awk's does and
467 # which bluetoothctl outputs
468 awk '/^Device +/ {print $2}' \
469 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
470 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
474 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
478 echo 'devices' | bluetoothctl | _bt_devs_infos
482 local -r stderr
="$(mktemp)"
486 $@
2> >(tee "$stderr")
489 0) urgency
='normal';;
490 *) urgency
='critical'
492 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
500 width = width ? width : 80
501 ch_left = ch_left ? ch_left : "["
502 ch_right = ch_right ? ch_right : "]"
503 ch_blank = ch_blank ? ch_blank : "-"
504 ch_used = ch_used ? ch_used : "|"
514 cur_scaled = num_scale(cur, max, 1, width)
518 lab ? lab " " : "", \
519 num ? cur "/" max " " : "", \
520 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
522 for (i=1; i<=width; i++) {
523 c = i <= cur_scaled ? ch_used : ch_blank
526 printf "%s\n", ch_right
529 function num_scale(src_cur, src_max, dst_min, dst_max) {
530 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
543 pct = cur / max * 100
544 printf "%s%s %.2f%%", sep, name, pct
553 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
559 if which upower
> /dev
/null
568 / battery/ && device["path"] {
569 device["is_battery"] = 1
573 / percentage:/ && device["is_battery"] {
574 device["battery_percentage"] = $2
575 sub("%$", "", device["battery_percentage"])
580 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
581 print device["battery_percentage"], 100, "batt"
591 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
595 local -r indent_unit
=' '
605 printf '%stmux\n%ssessions %d, clients %d\n' \
607 "${indent_unit}${indent_unit}" \
608 "$(tmux list-sessions 2> /dev/null | wc -l)" \
609 "$(tmux list-clients 2> /dev/null | wc -l)"
613 printf '%sprocs by user\n' "${indent_unit}"
622 for (user in count_by_user)
623 print count_by_user[user], total, user
627 | indent
"${indent_unit}${indent_unit}"
633 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
634 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
637 | bar_gauge
-v width
=60 -v pct
=1 \
639 | indent
"$indent_unit"
643 printf '%smem by proc\n' "$indent_unit"
645 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
653 for (proc in by_proc)
654 print by_proc[proc], total, proc
658 | indent
"${indent_unit}${indent_unit}"
662 local _dir temp_input label_file label
664 printf '%sthermal\n' "$indent_unit"
665 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
667 find "$_dir"/ -name 'temp*_input' \
668 |
while read -r temp_input
; do
669 label_file
=${temp_input//_input/_label}
670 if [ -f "$label_file" ]; then
671 label
=$
(< "$label_file")
675 awk -v label
="$label" '{
677 label = sprintf(" (%s)", label)
678 printf("%.2f°C%s\n", $1 / 1000, label)
683 | indent
"$indent_unit"
685 | indent
"${indent_unit}${indent_unit}"
688 #local -r internet_addr=$(internet_addr 0.5)
689 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
691 #echo "${indent_unit}internet"
692 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
693 echo "${indent_unit}if"
694 (ifconfig
; iwconfig
) 2> /dev
/null \
698 sub(":$", "", device)
701 sub("^ESSID:\"", "", _essid)
702 sub("\"$", "", _essid)
703 essid[device] = _essid
708 /^ / && $1 == "inet" {
713 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
714 split($2, lq_parts_eq, "=")
715 split(lq_parts_eq[2], lq_parts_slash, "/")
716 cur = lq_parts_slash[1]
717 max = lq_parts_slash[2]
718 link[device] = cur / max * 100
723 for (device in address)
724 if (device != "lo") {
727 l = l ? sprintf("%.0f%%", l) : "--"
729 print device, address[device], e, l
734 | indent
"${indent_unit}${indent_unit}"
736 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
738 echo "${indent_unit}-->"
740 sudo
-n netstat
-tulnp \
741 |
awk -v indent
="${indent_unit}${indent_unit}" '
742 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
745 port = a[split(addr, a, ":")]
746 name = p[split(proc, p, "/")]
748 protocols[protocol] = 1
749 if (!seen[protocol, name, port]++)
750 ports[protocol, name, ++seen[protocol, name]] = port
754 for (protocol in protocols) {
755 printf "%s%s\t", indent, toupper(protocol)
756 for (name in names) {
757 if (n = seen[protocol, name]) {
760 for (i = 1; i <= n; i++) {
761 printf "%s%d", sep, ports[protocol, name, i]
771 echo "${indent_unit}<->"
773 printf '%sTCP\t' "${indent_unit}${indent_unit}"
774 sudo
-n netstat
-tnp \
775 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
776 |
awk '{sub("^[0-9]+/", ""); print}' \
781 # TODO: iptables summary
784 ssh_invalid_by_addr
() {
786 /: Invalid user/ && $5 ~ /^sshd/ {
787 addr=$10 == "port" ? $9 : $10
793 for (addr in by_addr)
794 if ((c = by_addr[addr]) > 1)
795 printf "%d %d %s\n", c, max, addr
799 /var
/log
/auth.log
.1 \
801 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
805 ssh_invalid_by_day
() {
822 /: Invalid user/ && $5 ~ /^sshd/ {
830 if ((c = by_day[day]) > 1)
831 printf "%d %d %s\n", c, max, day
835 /var
/log
/auth.log
.1 \
837 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
841 ssh_invalid_by_user
() {
843 /: Invalid user/ && $5 ~ /^sshd/ {
850 for (user in by_user)
851 if ((c = by_user[user]) > 1)
852 printf "%d %d %s\n", c, max, user
856 /var
/log
/auth.log
.1 \
858 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
866 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
868 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
871 n = split($1, path, "/") # prog may be in path form
879 print count[prog], total, prog
882 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \