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"
30 local -r timestamp
="$(date --iso-8601=ns)"
31 local -r dir
="$HOME"/dl
/adhoc
/"$timestamp"
32 local -r url_file_path
="${dir}/url"
35 touch "$url_file_path"
40 ## ws : string -> unit
42 local line search_string0 search_string
45 case "$search_string0" in
47 while read -r line
; do
48 search_string
="${search_string} ${line}"
51 search_string
="$search_string0";;
54 firefox
--search "$search_string"
59 ## d : string -> string list
61 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
65 ## shell_activity_report : (mon | dow) -> string list
66 shell_activity_report
() {
67 # TODO: optional concrete number output
68 # TODO: optional combinations of granularities: hour, weekday, month, year
75 echo "Usage: $0 [mon|dow]" >&2
79 |
awk -v group_by
="$group_by" '
80 function date2dow(y, m, d, _t, _i) {
82 # y > 1752, 1 <= m <= 12.
85 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
99 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
100 _i = _i == 0 ? 7 : _i # Make Sunday last
106 # NOTE: $2 & $3 are specific to oh-my-zsh history output
109 d_fields = split(date, d, "-")
110 t_fields = split(time, t, ":")
111 if (t_fields && d_fields) {
112 # +0 to coerce number from string
117 dow = date2dow(year, month, day)
118 g = group_by == "mon" ? month : dow # dow is default
147 n = group_by == "mon" ? 12 : 7 # dow is default
149 for (gid = 1; gid <= n; gid++) {
150 group = group_by == "mon" ? m[gid] : w[gid]
151 printf "%s\n", group;
152 for (hour=0; hour<24; hour++) {
155 for (i = 1; i <= (c * 100) / max; i++)
163 ## top_commands : unit -> (command:string * count:number * bar:string) list
173 print count[cmd], cmd
180 c = count[NR] = $1 + 0 # + 0 to coerce number from string
186 for (i = 1; i <= NR; i++) {
188 printf "%s %d ", cmd[i], c
189 scaled = (c * 100) / max
190 for (j = 1; j <= scaled; j++)
198 ## Top Disk-Using directories
199 ## tdu : path-string -> (size:number * directory:path-string) list
201 local -r root_path
="$1"
207 # A slight optimization: head can exit before traversing the full input.
210 ## Top Disk-Using Files
211 ## tduf : path-string list -> (size:number * file:path-string) list
213 find "$@" -type f
-printf '%s\t%p\0' \
216 | numfmt
-z --to=iec \
220 # Most-recently modified file system objects
221 ## recent : ?(path-string list) -> path-string list
224 # - %T+ is a GNU extension;
225 # - gawk is able to split records on \0, while awk cannot.
226 find "$@" -printf '%T@ %T+ %p\0' \
227 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
228 |
sort -z -k 1 -n -r \
229 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
232 sub("^" $1 " +", "") # Remove epoch time
233 sub("+", " ") # Blank-out the default separator
234 sub("\\.[0-9]+", "") # Remove fractional seconds
239 ## recent_dirs : ?(path-string list) -> path-string list
244 ## recent_files : ?(path-string list) -> path-string list
249 ## pa_def_sink : unit -> string
251 pactl info |
awk '/^Default Sink:/ {print $3}'
254 ## void_pkgs : ?(string) -> json
256 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
260 ## man : string -> string
264 # me: end bold, blink and underline
266 # so: begin standout (reverse video)
269 # us: begin underline
272 LESS_TERMCAP_md
=$
'\e[01;30m' \
273 LESS_TERMCAP_me
=$
'\e[0m' \
274 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
275 LESS_TERMCAP_se
=$
'\e[0m' \
276 LESS_TERMCAP_us
=$
'\e[01;33m' \
277 LESS_TERMCAP_ue
=$
'\e[0m' \
282 ## x : string list -> unit
284 cd "$(~/bin/x $@)" ||
kill -INT $$
288 ## hump : unit -> unit
290 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
294 ## howto : unit -> string
296 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
300 local -r base_dir
="$1"
304 local -r yt
=youtube-dl
305 local -r id
=$
("$yt" --get-id "$uri")
306 local -r title
=$
("$yt" --get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
307 local -r dir
="${base_dir}/${title}--${id}"
310 cd "$dir" ||
kill -INT $$
312 "$yt" $opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
317 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
322 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
326 local -r user_type
="$1"
327 local -r user_name
="$2"
329 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
333 local -r gh_user_type
="$1"
334 local -r gh_user_name
="$2"
336 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
338 cd "$gh_dir" ||
kill -INT $$
339 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
340 | jq
--raw-output '.[] | select(.fork | not) | .clone_url' \
346 gh_clone
'users' "$1"
354 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
355 gh_dir
="${DIR_GITHUB}/${gh_username}"
357 cd "$gh_dir" ||
kill -INT $$
362 local -r len
="${1:-79}" # 1st arg or 79.
363 local -r char
="${2:--}" # 2nd arg or a dash.
364 for _
in {1..
"$len"}; do
369 daily_todo_file_template
() {
371 ===============================================================================
373 ===============================================================================
375 -------------------------------------------------------------------------------
377 -------------------------------------------------------------------------------
380 -------------------------------------------------------------------------------
382 -------------------------------------------------------------------------------
385 -------------------------------------------------------------------------------
387 -------------------------------------------------------------------------------
390 -------------------------------------------------------------------------------
392 -------------------------------------------------------------------------------
399 local -r dir
="$DIR_TODO/daily"
400 local -r file="$dir/$date.txt"
405 daily_todo_file_template
> "$file"
407 cd "$DIR_TODO" && "$EDITOR" $EDITOR_ARGS "$file"
411 cd "$DIR_TODO" && "$EDITOR" TODO
414 work_log_template
() {
436 mkdir
-p "$DIR_WORK_LOG"
437 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
438 if [ ! -f "$file_work_log_today" ]
440 work_log_template
> "$file_work_log_today"
442 vim
-c 'set spell' "$file_work_log_today"
447 mkdir
-p "$DIR_NOTES"
448 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
452 # grep's defintion of a line does not include \r, wile awk's does and
453 # which bluetoothctl outputs
454 awk '/^Device +/ {print $2}' \
455 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
456 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
460 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
464 echo 'devices' | bluetoothctl | _bt_devs_infos
468 local -r stderr
="$(mktemp)"
472 $@
2> >(tee "$stderr")
475 0) urgency
='normal';;
476 *) urgency
='critical'
478 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
486 width = width ? width : 80
487 ch_left = ch_left ? ch_left : "["
488 ch_right = ch_right ? ch_right : "]"
489 ch_blank = ch_blank ? ch_blank : "-"
490 ch_used = ch_used ? ch_used : "|"
500 cur_scaled = num_scale(cur, max, 1, width)
504 lab ? lab " " : "", \
505 num ? cur "/" max " " : "", \
506 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
508 for (i=1; i<=width; i++) {
509 c = i <= cur_scaled ? ch_used : ch_blank
512 printf "%s\n", ch_right
515 function num_scale(src_cur, src_max, dst_min, dst_max) {
516 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
529 pct = cur / max * 100
530 printf "%s%s %.2f%%", sep, name, pct
539 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
545 if which upower
> /dev
/null
554 / battery/ && device["path"] {
555 device["is_battery"] = 1
559 / percentage:/ && device["is_battery"] {
560 device["battery_percentage"] = $2
561 sub("%$", "", device["battery_percentage"])
566 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
567 print device["battery_percentage"], 100, "batt"
577 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
581 local -r indent_unit
=' '
591 # TODO Bring back seesion and client listing, but per server/socket.
592 printf '%stmux\n' "$indent_unit"
595 # Expecting lines like:
596 # "tmux: server tmux -L pistactl new-session -d -s pistactl"
597 # "tmux: client tmux -L foo"
598 # "tmux: client tmux -Lbar"
599 # "tmux: client tmux"
600 # "tmux: server tmux -L foo -S bar" <-- -S takes precedence
602 # XXX This of course assumes pervasive usage of -L
606 split($0, sides_of_S, "-S")
607 split(sides_of_S[2], words_right_of_S, FS)
609 split($0, sides_of_L, "-L")
610 split(sides_of_L[2], words_right_of_L, FS)
612 if (words_right_of_S[1]) {
613 sock = "path." words_right_of_S[1]
614 } else if (words_right_of_L[1]) {
615 sock = "name." words_right_of_L[1]
626 for (sock in socks) {
627 clients = count["client", sock]
630 printf "<-> %d", clients
638 | indent
"${indent_unit}${indent_unit}"
642 printf '%sprocs by user\n' "${indent_unit}"
651 for (user in count_by_user)
652 print count_by_user[user], total, user
656 | indent
"${indent_unit}${indent_unit}"
662 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
663 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
666 | bar_gauge
-v width
=60 -v pct
=1 \
668 | indent
"$indent_unit"
672 printf '%smem by proc\n' "$indent_unit"
674 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
682 for (proc in by_proc)
683 print by_proc[proc], total, proc
687 | indent
"${indent_unit}${indent_unit}"
691 local _dir temp_input label_file label
693 printf '%sthermal\n' "$indent_unit"
694 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
696 find "$_dir"/ -name 'temp*_input' \
697 |
while read -r temp_input
; do
698 label_file
=${temp_input//_input/_label}
699 if [ -f "$label_file" ]; then
700 label
=$
(< "$label_file")
704 awk -v label
="$label" '{
706 label = sprintf(" (%s)", label)
707 printf("%.2f°C%s\n", $1 / 1000, label)
712 | indent
"$indent_unit"
714 | indent
"${indent_unit}${indent_unit}"
717 #local -r internet_addr=$(internet_addr 0.5)
718 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
720 #echo "${indent_unit}internet"
721 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
722 echo "${indent_unit}if"
723 (ifconfig
; iwconfig
) 2> /dev
/null \
727 sub(":$", "", device)
730 sub("^ESSID:\"", "", _essid)
731 sub("\"$", "", _essid)
732 essid[device] = _essid
737 /^ / && $1 == "inet" {
742 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
743 split($2, lq_parts_eq, "=")
744 split(lq_parts_eq[2], lq_parts_slash, "/")
745 cur = lq_parts_slash[1]
746 max = lq_parts_slash[2]
747 link[device] = cur / max * 100
752 for (device in address)
753 if (device != "lo") {
756 l = l ? sprintf("%.0f%%", l) : "--"
758 print device, address[device], e, l
763 | indent
"${indent_unit}${indent_unit}"
765 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
767 echo "${indent_unit}-->"
769 # TODO Populate pid->cmd dict from `ps -eo pid,comm` and lookup progs there
770 # since netstat -p output comes out truncated.
772 sudo
-n netstat
-tulnp \
773 |
awk -v indent
="${indent_unit}${indent_unit}" '
774 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
777 port = a[split(addr, a, ":")]
778 name = p[split(proc, p, "/")]
780 protocols[protocol] = 1
781 if (!seen[protocol, name, port]++)
782 ports[protocol, name, ++seen[protocol, name]] = port
786 for (protocol in protocols) {
787 printf "%s%s\t", indent, toupper(protocol)
788 for (name in names) {
789 if (n = seen[protocol, name]) {
792 for (i = 1; i <= n; i++) {
793 printf "%s%d", sep, ports[protocol, name, i]
803 echo "${indent_unit}<->"
805 printf '%sTCP\t' "${indent_unit}${indent_unit}"
806 sudo
-n netstat
-tnp \
807 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
808 |
awk '{sub("^[0-9]+/", ""); print}' \
813 # TODO: iptables summary
816 ssh_invalid_by_addr
() {
818 /: Invalid user/ && $5 ~ /^sshd/ {
819 addr=$10 == "port" ? $9 : $10
825 for (addr in by_addr)
826 if ((c = by_addr[addr]) > 1)
827 printf "%d %d %s\n", c, max, addr
831 /var
/log
/auth.log
.1 \
833 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
837 ssh_invalid_by_day
() {
854 /: Invalid user/ && $5 ~ /^sshd/ {
862 if ((c = by_day[day]) > 1)
863 printf "%d %d %s\n", c, max, day
867 /var
/log
/auth.log
.1 \
869 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
873 ssh_invalid_by_user
() {
875 /: Invalid user/ && $5 ~ /^sshd/ {
882 for (user in by_user)
883 if ((c = by_user[user]) > 1)
884 printf "%d %d %s\n", c, max, user
888 /var
/log
/auth.log
.1 \
890 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
898 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
900 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
903 n = split($1, path, "/") # prog may be in path form
911 print count[prog], total, prog
914 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \