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"
31 local -r usage
='Usage: p [FILE] NAME'
53 BEGIN {_s = tolower(_s)}
55 # TODO fzf/dmenu select instead of searching:
56 /^[a-zA-Z]/ && tolower($1) ~ _s && NF >= 2 {
69 } # TODO What would NF > 4 mean?
71 printf("%d [O] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
76 printf("%d [C] s:\"%s\", e:\"%s\", u:\"%s\"\n", n, s, e, u) > "/dev/stderr"
82 printf "%s", ps[n] # XXX Intentionally avoiding newline in the result.
86 printf "[ERROR] Found nothing.\n" > "/dev/stderr"
89 # TODO fzf-select which of the records the user (ahem, me) wants.
90 printf "[WARNING] Found more than one record. Selecting the last one.\n" > "/dev/stderr"
100 ## ws : string -> unit
102 local line search_string0 search_string
105 case "$search_string0" in
107 while read -r line
; do
108 search_string
="${search_string} ${line}"
111 search_string
="$search_string0";;
114 firefox
--search "$search_string"
119 ## d : string -> string list
121 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
125 ## shell_activity_report : (mon | dow) -> string list
126 shell_activity_report
() {
127 # TODO: optional concrete number output
128 # TODO: optional combinations of granularities: hour, weekday, month, year
135 echo "Usage: $0 [mon|dow]" >&2
139 |
awk -v group_by
="$group_by" '
140 function date2dow(y, m, d, _t, _i) {
142 # y > 1752, 1 <= m <= 12.
145 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
159 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
160 _i = _i == 0 ? 7 : _i # Make Sunday last
166 # NOTE: $2 & $3 are specific to oh-my-zsh history output
169 d_fields = split(date, d, "-")
170 t_fields = split(time, t, ":")
171 if (t_fields && d_fields) {
172 # +0 to coerce number from string
177 dow = date2dow(year, month, day)
178 g = group_by == "mon" ? month : dow # dow is default
207 n = group_by == "mon" ? 12 : 7 # dow is default
209 for (gid = 1; gid <= n; gid++) {
210 group = group_by == "mon" ? m[gid] : w[gid]
211 printf "%s\n", group;
212 for (hour=0; hour<24; hour++) {
215 for (i = 1; i <= (c * 100) / max; i++)
223 ## top_commands : unit -> (command:string * count:number * bar:string) list
233 print count[cmd], cmd
240 c = count[NR] = $1 + 0 # + 0 to coerce number from string
246 for (i = 1; i <= NR; i++) {
248 printf "%s %d ", cmd[i], c
249 scaled = (c * 100) / max
250 for (j = 1; j <= scaled; j++)
258 ## Top Disk-Using directories
259 ## TODO: Consider using numfmt instead of awk
260 ## tdu : path-string -> (size:number * directory:path-string) list
262 local -r root_path
="$1"
269 sub("^" $1 "\t+", "", path)
276 for (path in paths) {
278 pct = 100 * (size / max)
279 gb = size / 1024 / 1024
280 printf("%6.2f %3d%% %s\n", gb, pct, path)
287 # A slight optimization: head can exit before traversing the full input.
290 ## Top Disk-Using Files
291 ## tduf : path-string list -> (size:number * file:path-string) list
293 find "$@" -type f
-printf '%s\t%p\0' \
300 sub("^" $1 "\t+", "", path)
301 gb = size / 1024 / 1024 / 1024
302 printf("%f\t%s\n", gb, path)
306 # Most-recently modified file system objects
307 ## recent : ?(path-string list) -> path-string list
310 # - %T+ is a GNU extension;
311 # - gawk is able to split records on \0, while awk cannot.
312 find "$@" -printf '%T@ %T+ %p\0' \
313 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
314 |
sort -z -k 1 -n -r \
315 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
318 sub("^" $1 " +", "") # Remove epoch time
319 sub("+", " ") # Blank-out the default separator
320 sub("\\.[0-9]+", "") # Remove fractional seconds
325 ## recent_dirs : ?(path-string list) -> path-string list
330 ## recent_files : ?(path-string list) -> path-string list
335 ## pa_def_sink : unit -> string
337 pactl info |
awk '/^Default Sink:/ {print $3}'
340 ## void_pkgs : ?(string) -> json
342 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
346 ## man : string -> string
350 # me: end bold, blink and underline
352 # so: begin standout (reverse video)
355 # us: begin underline
358 LESS_TERMCAP_md
=$
'\e[01;30m' \
359 LESS_TERMCAP_me
=$
'\e[0m' \
360 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
361 LESS_TERMCAP_se
=$
'\e[0m' \
362 LESS_TERMCAP_us
=$
'\e[01;33m' \
363 LESS_TERMCAP_ue
=$
'\e[0m' \
368 ## x : string list -> unit
370 cd "$(~/bin/x $@)" ||
kill -INT $$
374 ## hump : unit -> unit
376 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
380 ## howto : unit -> string
382 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
386 local -r base_dir
="$1"
390 local -r yt
=youtube-dl
391 local -r id
=$
("$yt" --get-id "$uri")
392 local -r title
=$
("$yt" --get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
393 local -r dir
="${base_dir}/${title}--${id}"
396 cd "$dir" ||
kill -INT $$
398 "$yt" $opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
403 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
408 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
412 local -r user_type
="$1"
413 local -r user_name
="$2"
415 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
419 local -r gh_user_type
="$1"
420 local -r gh_user_name
="$2"
422 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
424 cd "$gh_dir" ||
kill -INT $$
425 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
426 | jq
--raw-output '.[] | select(.fork | not) | .clone_url' \
432 gh_clone
'users' "$1"
440 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
441 gh_dir
="${DIR_GITHUB}/${gh_username}"
443 cd "$gh_dir" ||
kill -INT $$
448 local -r len
="${1:-79}" # 1st arg or 79.
449 local -r char
="${2:--}" # 2nd arg or a dash.
450 for _
in {1..
"$len"}; do
455 daily_todo_file_template
() {
457 ===============================================================================
459 ===============================================================================
461 -------------------------------------------------------------------------------
463 -------------------------------------------------------------------------------
466 -------------------------------------------------------------------------------
468 -------------------------------------------------------------------------------
471 -------------------------------------------------------------------------------
473 -------------------------------------------------------------------------------
476 -------------------------------------------------------------------------------
478 -------------------------------------------------------------------------------
485 local -r dir
="$DIR_TODO/daily"
486 local -r file="$dir/$date.txt"
491 daily_todo_file_template
> "$file"
493 cd "$DIR_TODO" && "$EDITOR" $EDITOR_ARGS "$file"
497 cd "$DIR_TODO" && "$EDITOR" TODO
500 work_log_template
() {
522 mkdir
-p "$DIR_WORK_LOG"
523 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
524 if [ ! -f "$file_work_log_today" ]
526 work_log_template
> "$file_work_log_today"
528 vim
-c 'set spell' "$file_work_log_today"
533 mkdir
-p "$DIR_NOTES"
534 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
538 # grep's defintion of a line does not include \r, wile awk's does and
539 # which bluetoothctl outputs
540 awk '/^Device +/ {print $2}' \
541 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
542 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
546 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
550 echo 'devices' | bluetoothctl | _bt_devs_infos
554 local -r stderr
="$(mktemp)"
558 $@
2> >(tee "$stderr")
561 0) urgency
='normal';;
562 *) urgency
='critical'
564 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
572 width = width ? width : 80
573 ch_left = ch_left ? ch_left : "["
574 ch_right = ch_right ? ch_right : "]"
575 ch_blank = ch_blank ? ch_blank : "-"
576 ch_used = ch_used ? ch_used : "|"
586 cur_scaled = num_scale(cur, max, 1, width)
590 lab ? lab " " : "", \
591 num ? cur "/" max " " : "", \
592 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
594 for (i=1; i<=width; i++) {
595 c = i <= cur_scaled ? ch_used : ch_blank
598 printf "%s\n", ch_right
601 function num_scale(src_cur, src_max, dst_min, dst_max) {
602 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
615 pct = cur / max * 100
616 printf "%s%s %.2f%%", sep, name, pct
625 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
631 if which upower
> /dev
/null
640 / battery/ && device["path"] {
641 device["is_battery"] = 1
645 / percentage:/ && device["is_battery"] {
646 device["battery_percentage"] = $2
647 sub("%$", "", device["battery_percentage"])
652 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
653 print device["battery_percentage"], 100, "batt"
663 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
667 local -r indent_unit
=' '
677 printf '%stmux\n%ssessions %d, clients %d\n' \
679 "${indent_unit}${indent_unit}" \
680 "$(tmux list-sessions 2> /dev/null | wc -l)" \
681 "$(tmux list-clients 2> /dev/null | wc -l)"
685 printf '%sprocs by user\n' "${indent_unit}"
694 for (user in count_by_user)
695 print count_by_user[user], total, user
699 | indent
"${indent_unit}${indent_unit}"
705 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
706 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
709 | bar_gauge
-v width
=60 -v pct
=1 \
711 | indent
"$indent_unit"
715 printf '%smem by proc\n' "$indent_unit"
717 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
725 for (proc in by_proc)
726 print by_proc[proc], total, proc
730 | indent
"${indent_unit}${indent_unit}"
734 local _dir temp_input label_file label
736 printf '%sthermal\n' "$indent_unit"
737 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
739 find "$_dir"/ -name 'temp*_input' \
740 |
while read -r temp_input
; do
741 label_file
=${temp_input//_input/_label}
742 if [ -f "$label_file" ]; then
743 label
=$
(< "$label_file")
747 awk -v label
="$label" '{
749 label = sprintf(" (%s)", label)
750 printf("%.2f°C%s\n", $1 / 1000, label)
755 | indent
"$indent_unit"
757 | indent
"${indent_unit}${indent_unit}"
760 #local -r internet_addr=$(internet_addr 0.5)
761 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
763 #echo "${indent_unit}internet"
764 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
765 echo "${indent_unit}if"
766 (ifconfig
; iwconfig
) 2> /dev
/null \
770 sub(":$", "", device)
773 sub("^ESSID:\"", "", _essid)
774 sub("\"$", "", _essid)
775 essid[device] = _essid
780 /^ / && $1 == "inet" {
785 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
786 split($2, lq_parts_eq, "=")
787 split(lq_parts_eq[2], lq_parts_slash, "/")
788 cur = lq_parts_slash[1]
789 max = lq_parts_slash[2]
790 link[device] = cur / max * 100
795 for (device in address)
796 if (device != "lo") {
799 l = l ? sprintf("%.0f%%", l) : "--"
801 print device, address[device], e, l
806 | indent
"${indent_unit}${indent_unit}"
808 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
810 echo "${indent_unit}-->"
812 sudo
-n netstat
-tulnp \
813 |
awk -v indent
="${indent_unit}${indent_unit}" '
814 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
817 port = a[split(addr, a, ":")]
818 name = p[split(proc, p, "/")]
820 protocols[protocol] = 1
821 if (!seen[protocol, name, port]++)
822 ports[protocol, name, ++seen[protocol, name]] = port
826 for (protocol in protocols) {
827 printf "%s%s\t", indent, toupper(protocol)
828 for (name in names) {
829 if (n = seen[protocol, name]) {
832 for (i = 1; i <= n; i++) {
833 printf "%s%d", sep, ports[protocol, name, i]
843 echo "${indent_unit}<->"
845 printf '%sTCP\t' "${indent_unit}${indent_unit}"
846 sudo
-n netstat
-tnp \
847 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
848 |
awk '{sub("^[0-9]+/", ""); print}' \
853 # TODO: iptables summary
856 ssh_invalid_by_addr
() {
858 /: Invalid user/ && $5 ~ /^sshd/ {
859 addr=$10 == "port" ? $9 : $10
865 for (addr in by_addr)
866 if ((c = by_addr[addr]) > 1)
867 printf "%d %d %s\n", c, max, addr
871 /var
/log
/auth.log
.1 \
873 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
877 ssh_invalid_by_day
() {
894 /: Invalid user/ && $5 ~ /^sshd/ {
902 if ((c = by_day[day]) > 1)
903 printf "%d %d %s\n", c, max, day
907 /var
/log
/auth.log
.1 \
909 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
913 ssh_invalid_by_user
() {
915 /: Invalid user/ && $5 ~ /^sshd/ {
922 for (user in by_user)
923 if ((c = by_user[user]) > 1)
924 printf "%d %d %s\n", c, max, user
928 /var
/log
/auth.log
.1 \
930 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
938 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
940 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
943 n = split($1, path, "/") # prog may be in path form
951 print count[prog], total, prog
954 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \