X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=home%2Flib%2Flogin_functions.sh;h=006902863fad638cadaa0255d4452f6ce5015960;hb=22f1641ffee69609f220a143c67640d402811dc7;hp=45460f4aa487a1393c01936b74bbee32dc62adb8;hpb=86b746627f089d4634b48f023823dd3320a35610;p=khome.git diff --git a/home/lib/login_functions.sh b/home/lib/login_functions.sh index 45460f4..0069028 100644 --- a/home/lib/login_functions.sh +++ b/home/lib/login_functions.sh @@ -267,9 +267,10 @@ gh_fetch_repos() { } gh_clone() { - gh_user_type="$1" - gh_user_name="$2" - gh_dir="${DIR_GITHUB}/${gh_user_name}" + local -r gh_user_type="$1" + local -r gh_user_name="$2" + + local -r gh_dir="${DIR_GITHUB}/${gh_user_name}" mkdir -p "$gh_dir" cd "$gh_dir" || kill -INT $$ gh_fetch_repos "$gh_user_type" "$gh_user_name" \ @@ -317,7 +318,7 @@ EOF work_log() { mkdir -p "$DIR_WORK_LOG" - file_work_log_today="${DIR_WORK_LOG}/$(date +%F).md" + local -r file_work_log_today="${DIR_WORK_LOG}/$(date +%F).md" if [ ! -f "$file_work_log_today" ] then work_log_template > "$file_work_log_today" @@ -353,10 +354,12 @@ bt_devs() { } run() { - stderr="$(mktemp)" + local -r stderr="$(mktemp)" + + local code urgency + $@ 2> >(tee "$stderr") code="$?" - urgency='' case "$code" in 0) urgency='normal';; *) urgency='critical' @@ -364,3 +367,164 @@ run() { notify-send -u "$urgency" "Job done: $code" "$(cat $stderr)" rm "$stderr" } + +bar_gauge() { + local -r width="$1" + + awk -v width="$width" ' + { + used = $1 + total = $2 + + u = num_scale(used, total, 1, width) + + printf "[" + for (i=1; i<=width; i++) { + c = i <= u ? "|" : "-" + printf "%s", c + } + printf "]\n" + } + + function num_scale(src_cur, src_max, dst_min, dst_max) { + return dst_min + ((src_cur * (dst_max - dst_min)) / src_max) + } + ' +} + +motd_mem() { + local -r bar_width="$1" + + printf 'mem ' + free \ + | awk '$1 == "Mem:" {total=$2; used=$3; print used, total}' \ + | bar_gauge "$bar_width" +} + +motd_disk() { + local -r bar_width="$1" + + printf 'disk ' + df ~ \ + | awk 'NR == 2 {used=$3; avail=$4; total=used+avail; print used, total}' \ + | bar_gauge "$bar_width" +} + +motd_batt() { + local -r bar_width="$1" + + case "$(uname)" in + 'Linux') + printf 'batt ' + upower --dump \ + | awk ' + /^Device:[ \t]+/ { + device["path"] = $2 + next + } + + / battery/ && device["path"] { + device["is_battery"] = 1 + next + } + + / percentage:/ && device["is_battery"] { + device["battery_percentage"] = $2 + sub("%$", "", device["battery_percentage"]) + next + } + + /^$/ { + if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice") + print device["battery_percentage"], 100 + delete device + } + ' \ + | bar_gauge "$bar_width" + ;; + esac +} + +indent() { + awk -v unit="$1" '{printf "%s%s\n", unit, $0}' +} + +motd() { + local -r bar_width='60' + local -r indent_unit=' ' + + uname -srvmo + hostname | figlet + uptime + + echo + + printf 'tmux sessions: %d\n' "$(tmux ls 2> /dev/null | wc -l)" + + echo + + echo 'Resources' + ( + motd_mem "$bar_width" + motd_disk "$bar_width" + motd_batt "$bar_width" + ) \ + | column -t \ + | indent "$indent_unit" + + echo + + echo 'Network' + echo "${indent_unit}interfaces:" + (ifconfig; iwconfig) 2> /dev/null \ + | awk ' + /^[^ ]/ { + device = $1 + sub(":$", "", device) + if ($4 ~ "ESSID:") { + _essid = $4 + sub("^ESSID:\"", "", _essid) + sub("\"$", "", _essid) + essid[device] = _essid + } + next + } + + /^ / && $1 == "inet" { + address[device] = $2 + next + } + + /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ { + split($2, lq_parts_eq, "=") + split(lq_parts_eq[2], lq_parts_slash, "/") + cur = lq_parts_slash[1] + max = lq_parts_slash[2] + link[device] = cur / max * 100 + next + } + + END { + for (device in address) + if (device != "lo") { + l = link[device] + e = essid[device] + l = l ? sprintf("%.0f%%", l) : "--" + e = e ? e : "--" + print device, address[device], e, l + } + } + ' \ + | column -t \ + | indent "${indent_unit}${indent_unit}" + + echo "${indent_unit}servers:" + # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat + sudo -n netstat -tlnp \ + | awk 'NR > 2 {print $7}' \ + | awk -F/ '{print $2}' \ + | sort -u \ + | xargs \ + | column -t \ + | indent "${indent_unit}${indent_unit}" +}