8 BEGIN {_s = tolower(_s)}
10 # TODO fzf/dmenu select instead of searching:
11 /^[a-zA-Z]/ && tolower($1) ~ _s && NF >= 2 {
24 } # TODO What would NF > 4 mean?
26 printf("%d [O] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
31 printf("%d [C] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
34 printf "%s", p # XXX Intentionally avoiding newline in the result.
41 printf "[ERROR] Found nothing.\n" > "/dev/stderr"
44 # TODO fzf-select which of the records the user (ahem, me) wants.
45 printf "[WARNING] Found more than one record.\n" > "/dev/stderr"
55 ## ws : string -> unit
57 local line search_string0 search_string
60 case "$search_string0" in
62 while read -r line
; do
63 search_string
="${search_string} ${line}"
66 search_string
="$search_string0";;
69 firefox
--search "$search_string"
74 ## d : string -> string list
76 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
80 ## shell_activity_report : (mon | dow) -> string list
81 shell_activity_report
() {
82 # TODO: optional concrete number output
83 # TODO: optional combinations of granularities: hour, weekday, month, year
90 echo "Usage: $0 [mon|dow]" >&2
94 |
awk -v group_by
="$group_by" '
95 function date2dow(y, m, d, _t, _i) {
97 # y > 1752, 1 <= m <= 12.
100 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
114 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
115 _i = _i == 0 ? 7 : _i # Make Sunday last
121 # NOTE: $2 & $3 are specific to oh-my-zsh history output
124 d_fields = split(date, d, "-")
125 t_fields = split(time, t, ":")
126 if (t_fields && d_fields) {
127 # +0 to coerce number from string
132 dow = date2dow(year, month, day)
133 g = group_by == "mon" ? month : dow # dow is default
162 n = group_by == "mon" ? 12 : 7 # dow is default
164 for (gid = 1; gid <= n; gid++) {
165 group = group_by == "mon" ? m[gid] : w[gid]
166 printf "%s\n", group;
167 for (hour=0; hour<24; hour++) {
170 for (i = 1; i <= (c * 100) / max; i++)
178 ## top_commands : unit -> (command:string * count:number * bar:string) list
188 print count[cmd], cmd
195 c = count[NR] = $1 + 0 # + 0 to coerce number from string
201 for (i = 1; i <= NR; i++) {
203 printf "%s %d ", cmd[i], c
204 scaled = (c * 100) / max
205 for (j = 1; j <= scaled; j++)
213 ## Top Disk-Using directories
214 ## TODO: Consider using numfmt instead of awk
215 ## tdu : path-string -> (size:number * directory:path-string) list
217 local -r root_path
="$1"
224 sub("^" $1 "\t+", "", path)
231 for (path in paths) {
233 pct = 100 * (size / max)
234 gb = size / 1024 / 1024
235 printf("%6.2f %3d%% %s\n", gb, pct, path)
242 # A slight optimization: head can exit before traversing the full input.
245 ## Top Disk-Using Files
246 ## tduf : path-string list -> (size:number * file:path-string) list
248 find "$@" -type f
-printf '%s\t%p\0' \
255 sub("^" $1 "\t+", "", path)
256 gb = size / 1024 / 1024 / 1024
257 printf("%f\t%s\n", gb, path)
261 # Most-recently modified file system objects
262 ## recent : ?(path-string list) -> path-string list
265 # - %T+ is a GNU extension;
266 # - gawk is able to split records on \0, while awk cannot.
267 find "$@" -printf '%T@ %T+ %p\0' \
268 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
269 |
sort -z -k 1 -n -r \
270 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
273 sub("^" $1 " +", "") # Remove epoch time
274 sub("+", " ") # Blank-out the default separator
275 sub("\\.[0-9]+", "") # Remove fractional seconds
280 ## recent_dirs : ?(path-string list) -> path-string list
285 ## recent_files : ?(path-string list) -> path-string list
290 ## pa_def_sink : unit -> string
292 pactl info |
awk '/^Default Sink:/ {print $3}'
295 ## void_pkgs : ?(string) -> json
297 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
301 ## man : string -> string
305 # me: end bold, blink and underline
307 # so: begin standout (reverse video)
310 # us: begin underline
313 LESS_TERMCAP_md
=$
'\e[01;30m' \
314 LESS_TERMCAP_me
=$
'\e[0m' \
315 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
316 LESS_TERMCAP_se
=$
'\e[0m' \
317 LESS_TERMCAP_us
=$
'\e[01;33m' \
318 LESS_TERMCAP_ue
=$
'\e[0m' \
323 ## x : string list -> unit
325 cd "$(~/bin/x $@)" ||
kill -INT $$
329 ## hump : unit -> unit
331 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
335 ## howto : unit -> string
337 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
341 local -r base_dir
="$1"
345 local -r id
=$
(youtube-dlc
--get-id "$uri")
346 local -r title
=$
(youtube-dlc
--get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
347 local -r dir
="${base_dir}/${title}--${id}"
350 cd "$dir" ||
kill -INT $$
352 youtube-dlc
$opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
357 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
362 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
366 local -r user_type
="$1"
367 local -r user_name
="$2"
369 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
373 local -r gh_user_type
="$1"
374 local -r gh_user_name
="$2"
376 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
378 cd "$gh_dir" ||
kill -INT $$
379 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
380 | jq
--raw-output '.[] | select(.fork | not) | .clone_url' \
386 gh_clone
'users' "$1"
394 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
395 gh_dir
="${DIR_GITHUB}/${gh_username}"
397 cd "$gh_dir" ||
kill -INT $$
401 work_log_template
() {
423 mkdir
-p "$DIR_WORK_LOG"
424 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
425 if [ ! -f "$file_work_log_today" ]
427 work_log_template
> "$file_work_log_today"
429 vim
-c 'set spell' "$file_work_log_today"
434 mkdir
-p "$DIR_NOTES"
435 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
439 # grep's defintion of a line does not include \r, wile awk's does and
440 # which bluetoothctl outputs
441 awk '/^Device +/ {print $2}' \
442 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
443 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
447 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
451 echo 'devices' | bluetoothctl | _bt_devs_infos
455 local -r stderr
="$(mktemp)"
459 $@
2> >(tee "$stderr")
462 0) urgency
='normal';;
463 *) urgency
='critical'
465 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
473 width = width ? width : 80
474 ch_left = ch_left ? ch_left : "["
475 ch_right = ch_right ? ch_right : "]"
476 ch_blank = ch_blank ? ch_blank : "-"
477 ch_used = ch_used ? ch_used : "|"
487 cur_scaled = num_scale(cur, max, 1, width)
491 lab ? lab " " : "", \
492 num ? cur "/" max " " : "", \
493 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
495 for (i=1; i<=width; i++) {
496 c = i <= cur_scaled ? ch_used : ch_blank
499 printf "%s\n", ch_right
502 function num_scale(src_cur, src_max, dst_min, dst_max) {
503 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
516 pct = cur / max * 100
517 printf "%s%s %.2f%%", sep, name, pct
526 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
532 if which upower
> /dev
/null
541 / battery/ && device["path"] {
542 device["is_battery"] = 1
546 / percentage:/ && device["is_battery"] {
547 device["battery_percentage"] = $2
548 sub("%$", "", device["battery_percentage"])
553 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
554 print device["battery_percentage"], 100, "batt"
564 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
568 local -r indent_unit
=' '
578 printf '%stmux\n%ssessions %d, clients %d\n' \
580 "${indent_unit}${indent_unit}" \
581 "$(tmux list-sessions 2> /dev/null | wc -l)" \
582 "$(tmux list-clients 2> /dev/null | wc -l)"
586 printf '%sprocs by user\n' "${indent_unit}"
595 for (user in count_by_user)
596 print count_by_user[user], total, user
600 | indent
"${indent_unit}${indent_unit}"
606 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
607 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
610 | bar_gauge
-v width
=60 -v pct
=1 \
612 | indent
"$indent_unit"
616 printf '%smem by proc\n' "$indent_unit"
618 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
626 for (proc in by_proc)
627 print by_proc[proc], total, proc
631 | indent
"${indent_unit}${indent_unit}"
635 local _dir temp_input label_file label
637 printf '%sthermal\n' "$indent_unit"
638 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
640 find "$_dir"/ -name 'temp*_input' \
641 |
while read -r temp_input
; do
642 label_file
=${temp_input//_input/_label}
643 if [ -f "$label_file" ]; then
644 label
=$
(< "$label_file")
648 awk -v label
="$label" '{
650 label = sprintf(" (%s)", label)
651 printf("%.2f°C%s\n", $1 / 1000, label)
656 | indent
"$indent_unit"
658 | indent
"${indent_unit}${indent_unit}"
661 #local -r internet_addr=$(internet_addr 0.5)
662 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
664 #echo "${indent_unit}internet"
665 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
666 echo "${indent_unit}if"
667 (ifconfig
; iwconfig
) 2> /dev
/null \
671 sub(":$", "", device)
674 sub("^ESSID:\"", "", _essid)
675 sub("\"$", "", _essid)
676 essid[device] = _essid
681 /^ / && $1 == "inet" {
686 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
687 split($2, lq_parts_eq, "=")
688 split(lq_parts_eq[2], lq_parts_slash, "/")
689 cur = lq_parts_slash[1]
690 max = lq_parts_slash[2]
691 link[device] = cur / max * 100
696 for (device in address)
697 if (device != "lo") {
700 l = l ? sprintf("%.0f%%", l) : "--"
702 print device, address[device], e, l
707 | indent
"${indent_unit}${indent_unit}"
709 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
711 echo "${indent_unit}-->"
713 sudo
-n netstat
-tulnp \
714 |
awk -v indent
="${indent_unit}${indent_unit}" '
715 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
718 port = a[split(addr, a, ":")]
719 name = p[split(proc, p, "/")]
721 protocols[protocol] = 1
722 if (!seen[protocol, name, port]++)
723 ports[protocol, name, ++seen[protocol, name]] = port
727 for (protocol in protocols) {
728 printf "%s%s\t", indent, toupper(protocol)
729 for (name in names) {
730 if (n = seen[protocol, name]) {
733 for (i = 1; i <= n; i++) {
734 printf "%s%d", sep, ports[protocol, name, i]
744 echo "${indent_unit}<->"
746 printf '%sTCP\t' "${indent_unit}${indent_unit}"
747 sudo
-n netstat
-tnp \
748 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
749 |
awk '{sub("^[0-9]+/", ""); print}' \
754 # TODO: iptables summary
757 ssh_invalid_by_addr
() {
759 /: Invalid user/ && $5 ~ /^sshd/ {
760 addr=$10 == "port" ? $9 : $10
766 for (addr in by_addr)
767 if ((c = by_addr[addr]) > 1)
768 printf "%d %d %s\n", c, max, addr
772 /var
/log
/auth.log
.1 \
774 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
778 ssh_invalid_by_day
() {
795 /: Invalid user/ && $5 ~ /^sshd/ {
803 if ((c = by_day[day]) > 1)
804 printf "%d %d %s\n", c, max, day
808 /var
/log
/auth.log
.1 \
810 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
814 ssh_invalid_by_user
() {
816 /: Invalid user/ && $5 ~ /^sshd/ {
823 for (user in by_user)
824 if ((c = by_user[user]) > 1)
825 printf "%d %d %s\n", c, max, user
829 /var
/log
/auth.log
.1 \
831 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
839 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
841 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
844 n = split($1, path, "/") # prog may be in path form
852 print count[prog], total, prog
855 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \