6 local line search_string0 search_string
9 case "$search_string0" in
11 while read -r line
; do
12 search_string
="${search_string} ${line}"
15 search_string
="$search_string0";;
18 firefox
--search "$search_string"
23 ## d : string -> string list
25 local -r word
=$
(fzf
< /usr
/share
/dict
/words
)
29 ## shell_activity_report : (mon | dow) -> string list
30 shell_activity_report
() {
31 # TODO: optional concrete number output
32 # TODO: optional combinations of granularities: hour, weekday, month, year
39 echo "Usage: $0 [mon|dow]" >&2
43 |
awk -v group_by
="$group_by" '
44 function date2dow(y, m, d, _t, _i) {
46 # y > 1752, 1 <= m <= 12.
49 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
63 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
64 _i = _i == 0 ? 7 : _i # Make Sunday last
70 # NOTE: $2 & $3 are specific to oh-my-zsh history output
73 d_fields = split(date, d, "-")
74 t_fields = split(time, t, ":")
75 if (t_fields && d_fields) {
76 # +0 to coerce number from string
81 dow = date2dow(year, month, day)
82 g = group_by == "mon" ? month : dow # dow is default
111 n = group_by == "mon" ? 12 : 7 # dow is default
113 for (gid = 1; gid <= n; gid++) {
114 group = group_by == "mon" ? m[gid] : w[gid]
115 printf "%s\n", group;
116 for (hour=0; hour<24; hour++) {
119 for (i = 1; i <= (c * 100) / max; i++)
127 ## top_commands : unit -> (command:string * count:number * bar:string) list
137 print count[cmd], cmd
144 c = count[NR] = $1 + 0 # + 0 to coerce number from string
150 for (i = 1; i <= NR; i++) {
152 printf "%s %d ", cmd[i], c
153 scaled = (c * 100) / max
154 for (j = 1; j <= scaled; j++)
162 ## Top Disk-Using directories
163 ## TODO: Consider using numfmt instead of awk
164 ## tdu : path-string -> (size:number * directory:path-string) list
166 local -r root_path
="$1"
173 sub("^" $1 "\t+", "", path)
180 for (path in paths) {
182 pct = 100 * (size / max)
183 gb = size / 1024 / 1024
184 printf("%6.2f %3d%% %s\n", gb, pct, path)
191 # A slight optimization: head can exit before traversing the full input.
194 ## Top Disk-Using Files
195 ## tduf : path-string list -> (size:number * file:path-string) list
197 find "$@" -type f
-printf '%s\t%p\0' \
204 sub("^" $1 "\t+", "", path)
205 gb = size / 1024 / 1024 / 1024
206 printf("%f\t%s\n", gb, path)
210 # Most-recently modified file system objects
211 ## recent : ?(path-string list) -> path-string list
214 # - %T+ is a GNU extension;
215 # - gawk is able to split records on \0, while awk cannot.
216 find "$@" -printf '%T@ %T+ %p\0' \
217 |
tee >(gawk
-v RS
='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
218 |
sort -z -k 1 -n -r \
219 |
head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
222 sub("^" $1 " +", "") # Remove epoch time
223 sub("+", " ") # Blank-out the default separator
224 sub("\\.[0-9]+", "") # Remove fractional seconds
229 ## recent_dirs : ?(path-string list) -> path-string list
234 ## recent_files : ?(path-string list) -> path-string list
239 ## pa_def_sink : unit -> string
241 pactl info |
awk '/^Default Sink:/ {print $3}'
244 ## void_pkgs : ?(string) -> json
246 curl
"https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq
'.data'
250 ## man : string -> string
254 # me: end bold, blink and underline
256 # so: begin standout (reverse video)
259 # us: begin underline
262 LESS_TERMCAP_md
=$
'\e[01;30m' \
263 LESS_TERMCAP_me
=$
'\e[0m' \
264 LESS_TERMCAP_so
=$
'\e[01;44;33m' \
265 LESS_TERMCAP_se
=$
'\e[0m' \
266 LESS_TERMCAP_us
=$
'\e[01;33m' \
267 LESS_TERMCAP_ue
=$
'\e[0m' \
272 ## x : string list -> unit
274 cd "$(~/bin/x $@)" ||
kill -INT $$
278 ## hump : unit -> unit
280 ledit
-l "$(stty size | awk '{print $2}')" ocaml $@
284 ## howto : unit -> string
286 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
290 local -r base_dir
="$1"
294 local -r id
=$
(youtube-dlc
--get-id "$uri")
295 local -r title
=$
(youtube-dlc
--get-title "$uri" |
sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g')
296 local -r dir
="${base_dir}/${title}--${id}"
299 cd "$dir" ||
kill -INT $$
301 youtube-dlc
$opts -c --write-description --write-info-json "$uri"
306 _yt
"${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
311 _yt
"${DIR_YOUTUBE_VIDEO}/individual" "$uri"
315 local -r user_type
="$1"
316 local -r user_name
="$2"
318 curl
"https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000"
322 local -r gh_user_type
="$1"
323 local -r gh_user_name
="$2"
325 local -r gh_dir
="${DIR_GITHUB}/${gh_user_name}"
327 cd "$gh_dir" ||
kill -INT $$
328 gh_fetch_repos
"$gh_user_type" "$gh_user_name" \
329 | jq
--raw-output '.[] | select(.fork | not) | .git_url' \
335 gh_clone
'users' "$1"
343 gh_username
=$
(echo "$1" |
awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
344 gh_dir
="${DIR_GITHUB}/${gh_username}"
346 cd "$gh_dir" ||
kill -INT $$
350 work_log_template
() {
372 mkdir
-p "$DIR_WORK_LOG"
373 local -r file_work_log_today
="${DIR_WORK_LOG}/daily-$(date +%F).md"
374 if [ ! -f "$file_work_log_today" ]
376 work_log_template
> "$file_work_log_today"
378 vim
-c 'set spell' "$file_work_log_today"
383 mkdir
-p "$DIR_NOTES"
384 vim
-c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
388 local _weather_location
390 '') _weather_location
="$WEATHER_LOCATION";;
391 *) _weather_location
="$1"
393 curl
"http://wttr.in/$_weather_location?format=v2"
397 # grep's defintion of a line does not include \r, wile awk's does and
398 # which bluetoothctl outputs
399 awk '/^Device +/ {print $2}' \
400 |
xargs -I% sh
-c 'echo info % | bluetoothctl' \
401 |
awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
405 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
409 echo 'devices' | bluetoothctl | _bt_devs_infos
413 local -r stderr
="$(mktemp)"
417 $@
2> >(tee "$stderr")
420 0) urgency
='normal';;
421 *) urgency
='critical'
423 notify-send
-u "$urgency" "Job done: $code" "$(cat $stderr)"
431 width = width ? width : 80
432 ch_left = ch_left ? ch_left : "["
433 ch_right = ch_right ? ch_right : "]"
434 ch_blank = ch_blank ? ch_blank : "-"
435 ch_used = ch_used ? ch_used : "|"
445 cur_scaled = num_scale(cur, max, 1, width)
449 lab ? lab " " : "", \
450 num ? cur "/" max " " : "", \
451 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
453 for (i=1; i<=width; i++) {
454 c = i <= cur_scaled ? ch_used : ch_blank
457 printf "%s\n", ch_right
460 function num_scale(src_cur, src_max, dst_min, dst_max) {
461 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
474 pct = cur / max * 100
475 printf "%s%s %.2f%%", sep, name, pct
484 curl
--silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
490 if which upower
> /dev
/null
499 / battery/ && device["path"] {
500 device["is_battery"] = 1
504 / percentage:/ && device["is_battery"] {
505 device["battery_percentage"] = $2
506 sub("%$", "", device["battery_percentage"])
511 if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice")
512 print device["battery_percentage"], 100, "batt"
522 awk -v unit
="$1" '{printf "%s%s\n", unit, $0}'
526 local -r indent_unit
=' '
536 printf '%stmux\n%ssessions %d, clients %d\n' \
538 "${indent_unit}${indent_unit}" \
539 "$(tmux list-sessions 2> /dev/null | wc -l)" \
540 "$(tmux list-clients 2> /dev/null | wc -l)"
544 printf '%sprocs by user\n' "${indent_unit}"
553 for (user in count_by_user)
554 print count_by_user[user], total, user
558 | indent
"${indent_unit}${indent_unit}"
564 free |
awk '$1 == "Mem:" {print $3, $2, "mem"}'
565 df ~ |
awk 'NR == 2 {print $3, $3 + $4, "disk"}'
568 | bar_gauge
-v width
=60 -v pct
=1 \
570 | indent
"$indent_unit"
574 printf '%smem by proc\n' "$indent_unit"
576 |
awk -v total
="$(free | awk '$1 == "Mem
:" {print $2; exit}')" '
584 for (proc in by_proc)
585 print by_proc[proc], total, proc
589 | indent
"${indent_unit}${indent_unit}"
593 local _dir temp_input label_file label
595 printf '%sthermal\n' "$indent_unit"
596 for _dir
in /sys
/class
/hwmon
/hwmon
*; do
598 find "$_dir"/ -name 'temp*_input' \
599 |
while read -r temp_input
; do
600 label_file
=${temp_input//_input/_label}
601 if [ -f "$label_file" ]; then
602 label
=$
(< "$label_file")
606 awk -v label
="$label" '{
608 label = sprintf(" (%s)", label)
609 printf("%.2f°C%s\n", $1 / 1000, label)
614 | indent
"$indent_unit"
616 | indent
"${indent_unit}${indent_unit}"
619 #local -r internet_addr=$(internet_addr 0.5)
620 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
622 #echo "${indent_unit}internet"
623 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
624 echo "${indent_unit}if"
625 (ifconfig
; iwconfig
) 2> /dev
/null \
629 sub(":$", "", device)
632 sub("^ESSID:\"", "", _essid)
633 sub("\"$", "", _essid)
634 essid[device] = _essid
639 /^ / && $1 == "inet" {
644 /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ {
645 split($2, lq_parts_eq, "=")
646 split(lq_parts_eq[2], lq_parts_slash, "/")
647 cur = lq_parts_slash[1]
648 max = lq_parts_slash[2]
649 link[device] = cur / max * 100
654 for (device in address)
655 if (device != "lo") {
658 l = l ? sprintf("%.0f%%", l) : "--"
660 print device, address[device], e, l
665 | indent
"${indent_unit}${indent_unit}"
667 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
669 echo "${indent_unit}-->"
671 sudo
-n netstat
-tulnp \
672 |
awk -v indent
="${indent_unit}${indent_unit}" '
673 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
676 port = a[split(addr, a, ":")]
677 name = p[split(proc, p, "/")]
679 protocols[protocol] = 1
680 if (!seen[protocol, name, port]++)
681 ports[protocol, name, ++seen[protocol, name]] = port
685 for (protocol in protocols) {
686 printf "%s%s\t", indent, toupper(protocol)
687 for (name in names) {
688 if (n = seen[protocol, name]) {
691 for (i = 1; i <= n; i++) {
692 printf "%s%d", sep, ports[protocol, name, i]
702 echo "${indent_unit}<->"
704 printf '%sTCP\t' "${indent_unit}${indent_unit}"
705 sudo
-n netstat
-tnp \
706 |
awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
707 |
awk '{sub("^[0-9]+/", ""); print}' \
712 # TODO: iptables summary
715 ssh_invalid_by_addr
() {
717 /: Invalid user/ && $5 ~ /^sshd/ {
718 addr=$10 == "port" ? $9 : $10
724 for (addr in by_addr)
725 if ((c = by_addr[addr]) > 1)
726 printf "%d %d %s\n", c, max, addr
730 /var
/log
/auth.log
.1 \
732 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
736 ssh_invalid_by_day
() {
753 /: Invalid user/ && $5 ~ /^sshd/ {
761 if ((c = by_day[day]) > 1)
762 printf "%d %d %s\n", c, max, day
766 /var
/log
/auth.log
.1 \
768 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
772 ssh_invalid_by_user
() {
774 /: Invalid user/ && $5 ~ /^sshd/ {
781 for (user in by_user)
782 if ((c = by_user[user]) > 1)
783 printf "%d %d %s\n", c, max, user
787 /var
/log
/auth.log
.1 \
789 | bar_gauge
-v width
="$(stty size | awk '{print $2}')" -v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \
797 sub(":$", "", prog[1]) # if there were no [], than : will is left behind
799 }' /var
/log
/syslog
/var
/log
/syslog
.1 \
802 n = split($1, path, "/") # prog may be in path form
810 print count[prog], total, prog
813 | bar_gauge
-v num
=1 -v ch_right
=' ' -v ch_left
=' ' -v ch_blank
=' ' \