8 BEGIN {_s = tolower(_s)}
10 /^[a-zA-Z]/ && tolower($1) ~ _s && NF >= 2 {
22 } # TODO What would NF > 4 mean?
23 printf("s:\"%s\", e:\"%s\", u:\"%s\"\n", s, e, u) > "/dev/stderr"
24 printf "%s", p # XXX Intentionally avoiding newline in the result.
32 ## ws : string -> unit
34 local line search_string0 search_string
37 case "$search_string0" in
39 while read -r line
; do
40 search_string
="${search_string} ${line}"
43 search_string
="$search_string0";;
46 firefox
--search "$search_string"
51 ## d : string -> string list
53 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
57 ## shell_activity_report : (mon | dow) -> string list
58 shell_activity_report
() {
59 # TODO: optional concrete number output
60 # TODO: optional combinations of granularities: hour, weekday, month, year
67 echo "Usage: $0 [mon|dow]" >&2
71 |
awk -v group_by
="$group_by" '
72 function date2dow(y, m, d, _t, _i) {
74 # y > 1752, 1 <= m <= 12.
77 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
91 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
92 _i = _i == 0 ? 7 : _i # Make Sunday last
98 # NOTE: $2 & $3 are specific to oh-my-zsh history output
101 d_fields = split(date, d, "-")
102 t_fields = split(time, t, ":")
103 if (t_fields && d_fields) {
104 # +0 to coerce number from string
109 dow = date2dow(year, month, day)
110 g = group_by == "mon" ? month : dow # dow is default
139 n = group_by == "mon" ? 12 : 7 # dow is default
141 for (gid = 1; gid <= n; gid++) {
142 group = group_by == "mon" ? m[gid] : w[gid]
143 printf "%s\n", group;
144 for (hour=0; hour<24; hour++) {
147 for (i = 1; i <= (c * 100) / max; i++)
155 ## top_commands : unit -> (command:string * count:number * bar:string) list
165 print count[cmd], cmd
172 c = count[NR] = $1 + 0 # + 0 to coerce number from string
178 for (i = 1; i <= NR; i++) {
180 printf "%s %d ", cmd[i], c
181 scaled = (c * 100) / max
182 for (j = 1; j <= scaled; j++)
190 ## Top Disk-Using directories
191 ## TODO: Consider using numfmt instead of awk
192 ## tdu : path-string -> (size:number * directory:path-string) list
194 local -r root_path
="$1"
201 sub("^" $1 "\t+", "", path)
208 for (path in paths) {
210 pct = 100 * (size / max)
211 gb = size / 1024 / 1024
212 printf("%6.2f %3d%% %s\n", gb, pct, path)
219 # A slight optimization: head can exit before traversing the full input.
222 ## Top Disk-Using Files
223 ## tduf : path-string list -> (size:number * file:path-string) list
225 find "$@" -type f
-printf '%s\t%p\0' \
232 sub("^" $1 "\t+", "", path)
233 gb = size / 1024 / 1024 / 1024
234 printf("%f\t%s\n", gb, path)
238 # Most-recently modified file system objects
239 ## recent : ?(path-string list) -> path-string list
242 # - %T+ is a GNU extension;
243 # - gawk is able to split records on \0, while awk cannot.
244 find "$@" -printf '%T@ %T+ %p\0' \
245 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
246 |
sort -z -k 1 -n -r \
247 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
250 sub("^" $1 " +", "") # Remove epoch time
251 sub("+", " ") # Blank-out the default separator
252 sub("\\.[0-9]+", "") # Remove fractional seconds
257 ## recent_dirs : ?(path-string list) -> path-string list
262 ## recent_files : ?(path-string list) -> path-string list
267 ## pa_def_sink : unit -> string
269 pactl info |
awk '/^Default Sink:/ {print $3}'
272 ## void_pkgs : ?(string) -> json
274 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
278 ## man : string -> string
282 # me: end bold, blink and underline
284 # so: begin standout (reverse video)
287 # us: begin underline
290 LESS_TERMCAP_md
=$
'\e[01;30m' \
291 LESS_TERMCAP_me
=$
'\e[0m' \
292 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
293 LESS_TERMCAP_se
=$
'\e[0m' \
294 LESS_TERMCAP_us
=$
'\e[01;33m' \
295 LESS_TERMCAP_ue
=$
'\e[0m' \
300 ## x : string list -> unit
302 cd "$(~/bin/x $@)" ||
kill -INT $$
306 ## hump : unit -> unit
308 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
312 ## howto : unit -> string
314 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
318 local -r base_dir
="$1"
322 local -r id
=$
(youtube-dlc
--get-id "$uri")
323 local -r title
=$
(youtube-dlc
--get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
324 local -r dir
="${base_dir}/${title}--${id}"
327 cd "$dir" ||
kill -INT $$
329 youtube-dlc
$opts -c --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) | .git_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 $$
378 work_log_template
() {
400 mkdir
-p "$DIR_WORK_LOG"
401 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
402 if [ ! -f "$file_work_log_today" ]
404 work_log_template
> "$file_work_log_today"
406 vim
-c 'set spell' "$file_work_log_today"
411 mkdir
-p "$DIR_NOTES"
412 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
416 # grep's defintion of a line does not include \r, wile awk's does and
417 # which bluetoothctl outputs
418 awk '/^Device +/ {print $2}' \
419 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
420 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
424 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
428 echo 'devices' | bluetoothctl | _bt_devs_infos
432 local -r stderr
="$(mktemp)"
436 $@
2> >(tee "$stderr")
439 0) urgency
='normal';;
440 *) urgency
='critical'
442 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
450 width = width ? width : 80
451 ch_left = ch_left ? ch_left : "["
452 ch_right = ch_right ? ch_right : "]"
453 ch_blank = ch_blank ? ch_blank : "-"
454 ch_used = ch_used ? ch_used : "|"
464 cur_scaled = num_scale(cur, max, 1, width)
468 lab ? lab " " : "", \
469 num ? cur "/" max " " : "", \
470 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
472 for (i=1; i<=width; i++) {
473 c = i <= cur_scaled ? ch_used : ch_blank
476 printf "%s\n", ch_right
479 function num_scale(src_cur, src_max, dst_min, dst_max) {
480 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
493 pct = cur / max * 100
494 printf "%s%s %.2f%%", sep, name, pct
503 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
509 if which upower
> /dev
/null
518 / battery/ && device["path"] {
519 device["is_battery"] = 1
523 / percentage:/ && device["is_battery"] {
524 device["battery_percentage"] = $2
525 sub("%$", "", device["battery_percentage"])
530 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
531 print device["battery_percentage"], 100, "batt"
541 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
545 local -r indent_unit
=' '
555 printf '%stmux\n%ssessions %d, clients %d\n' \
557 "${indent_unit}${indent_unit}" \
558 "$(tmux list-sessions 2> /dev/null | wc -l)" \
559 "$(tmux list-clients 2> /dev/null | wc -l)"
563 printf '%sprocs by user\n' "${indent_unit}"
572 for (user in count_by_user)
573 print count_by_user[user], total, user
577 | indent
"${indent_unit}${indent_unit}"
583 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
584 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
587 | bar_gauge
-v width
=60 -v pct
=1 \
589 | indent
"$indent_unit"
593 printf '%smem by proc\n' "$indent_unit"
595 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
603 for (proc in by_proc)
604 print by_proc[proc], total, proc
608 | indent
"${indent_unit}${indent_unit}"
612 local _dir temp_input label_file label
614 printf '%sthermal\n' "$indent_unit"
615 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
617 find "$_dir"/ -name 'temp*_input' \
618 |
while read -r temp_input
; do
619 label_file
=${temp_input//_input/_label}
620 if [ -f "$label_file" ]; then
621 label
=$
(< "$label_file")
625 awk -v label
="$label" '{
627 label = sprintf(" (%s)", label)
628 printf("%.2f°C%s\n", $1 / 1000, label)
633 | indent
"$indent_unit"
635 | indent
"${indent_unit}${indent_unit}"
638 #local -r internet_addr=$(internet_addr 0.5)
639 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
641 #echo "${indent_unit}internet"
642 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
643 echo "${indent_unit}if"
644 (ifconfig
; iwconfig
) 2> /dev
/null \
648 sub(":$", "", device)
651 sub("^ESSID:\"", "", _essid)
652 sub("\"$", "", _essid)
653 essid[device] = _essid
658 /^ / && $1 == "inet" {
663 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
664 split($2, lq_parts_eq, "=")
665 split(lq_parts_eq[2], lq_parts_slash, "/")
666 cur = lq_parts_slash[1]
667 max = lq_parts_slash[2]
668 link[device] = cur / max * 100
673 for (device in address)
674 if (device != "lo") {
677 l = l ? sprintf("%.0f%%", l) : "--"
679 print device, address[device], e, l
684 | indent
"${indent_unit}${indent_unit}"
686 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
688 echo "${indent_unit}-->"
690 sudo
-n netstat
-tulnp \
691 |
awk -v indent
="${indent_unit}${indent_unit}" '
692 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
695 port = a[split(addr, a, ":")]
696 name = p[split(proc, p, "/")]
698 protocols[protocol] = 1
699 if (!seen[protocol, name, port]++)
700 ports[protocol, name, ++seen[protocol, name]] = port
704 for (protocol in protocols) {
705 printf "%s%s\t", indent, toupper(protocol)
706 for (name in names) {
707 if (n = seen[protocol, name]) {
710 for (i = 1; i <= n; i++) {
711 printf "%s%d", sep, ports[protocol, name, i]
721 echo "${indent_unit}<->"
723 printf '%sTCP\t' "${indent_unit}${indent_unit}"
724 sudo
-n netstat
-tnp \
725 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
726 |
awk '{sub("^[0-9]+/", ""); print}' \
731 # TODO: iptables summary
734 ssh_invalid_by_addr
() {
736 /: Invalid user/ && $5 ~ /^sshd/ {
737 addr=$10 == "port" ? $9 : $10
743 for (addr in by_addr)
744 if ((c = by_addr[addr]) > 1)
745 printf "%d %d %s\n", c, max, addr
749 /var
/log
/auth.log
.1 \
751 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
755 ssh_invalid_by_day
() {
772 /: Invalid user/ && $5 ~ /^sshd/ {
780 if ((c = by_day[day]) > 1)
781 printf "%d %d %s\n", c, max, day
785 /var
/log
/auth.log
.1 \
787 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
791 ssh_invalid_by_user
() {
793 /: Invalid user/ && $5 ~ /^sshd/ {
800 for (user in by_user)
801 if ((c = by_user[user]) > 1)
802 printf "%d %d %s\n", c, max, user
806 /var
/log
/auth.log
.1 \
808 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
816 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
818 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
821 n = split($1, path, "/") # prog may be in path form
829 print count[prog], total, prog
832 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \