X-Git-Url: https://git.xandkar.net/?p=khome.git;a=blobdiff_plain;f=home%2Flib%2Flogin_functions.sh;h=e75468fa5320adcc94145f5e5b65f883f3732789;hp=e006eee32825e21e395e9fea93f8e4d19ec4c85a;hb=0c296cad5e97bbaa0d0c9daa9fba55b411166e4e;hpb=d265cd11ac471b7d2596ac1c41b81e64094b788e diff --git a/home/lib/login_functions.sh b/home/lib/login_functions.sh index e006eee..e75468f 100644 --- a/home/lib/login_functions.sh +++ b/home/lib/login_functions.sh @@ -5,6 +5,103 @@ d() { dict "$word" } +shell_activity_report() { + # TODO: optional concrete number output + # TODO: optional combinations of granularities: hour, weekday, month, year + local group_by="$1" + case "$group_by" in + 'mon') ;; + 'dow') ;; + '') group_by='dow';; + *) + echo "Usage: $0 [mon|dow]" >&2 + kill -INT $$ + esac + history \ + | awk -v group_by="$group_by" ' + function date2dow(y, m, d, _t, _i) { + # Contract: + # y > 1752, 1 <= m <= 12. + # Source: + # Sakamoto`s methods + # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods + _t[ 0] = 0 + _t[ 1] = 3 + _t[ 2] = 2 + _t[ 3] = 5 + _t[ 4] = 0 + _t[ 5] = 3 + _t[ 6] = 5 + _t[ 7] = 1 + _t[ 8] = 4 + _t[ 9] = 6 + _t[10] = 2 + _t[11] = 4 + y -= m < 3 + _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7 + _i = _i == 0 ? 7 : _i # Make Sunday last + return _i + + } + + { + # NOTE: $2 & $3 are specific to oh-my-zsh history output + date = $2 + time = $3 + d_fields = split(date, d, "-") + t_fields = split(time, t, ":") + if (t_fields && d_fields) { + # +0 to coerce number from string + year = d[1] + 0 + month = d[2] + 0 + day = d[3] + 0 + hour = t[1] + 0 + dow = date2dow(year, month, day) + g = group_by == "mon" ? month : dow # dow is default + c = count[g, hour]++ + } + if (c > max) + max = c + } + + END { + w[1] = "Monday" + w[2] = "Tuesday" + w[3] = "Wednesday" + w[4] = "Thursday" + w[5] = "Friday" + w[6] = "Saturday" + w[7] = "Sunday" + + m[ 1] = "January" + m[ 2] = "February" + m[ 3] = "March" + m[ 4] = "April" + m[ 5] = "May" + m[ 6] = "June" + m[ 7] = "July" + m[ 8] = "August" + m[ 9] = "September" + m[10] = "October" + m[11] = "November" + m[12] = "December" + + n = group_by == "mon" ? 12 : 7 # dow is default + + for (gid = 1; gid <= n; gid++) { + group = group_by == "mon" ? m[gid] : w[gid] + printf "%s\n", group; + for (hour=0; hour<24; hour++) { + c = count[gid, hour] + printf " %2d ", hour + for (i = 1; i <= (c * 100) / max; i++) + printf "|" + printf "\n" + } + } + }' +} + top_commands() { history \ | awk ' @@ -43,8 +140,8 @@ top_commands() { # TODO: Consider using numfmt instead of awk tdu() { du "$1" \ - | sort -n -k 1 -r \ - | head -50 \ + | sort -n -k 1 \ + | tail -50 \ | awk ' { size = $1 @@ -59,8 +156,8 @@ tdu() { # Top Disk-Using Files tduf() { find "$1" -type f -printf '%s\t%p\0' \ - | sort -z -n -k 1 -r \ - | head -z -n 50 \ + | sort -z -n -k 1 \ + | tail -z -n 50 \ | gawk -v RS='\0' ' { size = $1 @@ -109,6 +206,16 @@ void_pkgs() { # Colorful man man() { + # mb: begin blink + # md: begin bold + # me: end bold, blink and underline + # + # so: begin standout (reverse video) + # se: end standout + # + # us: begin underline + # ue: end underline + LESS_TERMCAP_md=$'\e[01;31m' \ LESS_TERMCAP_me=$'\e[0m' \ LESS_TERMCAP_se=$'\e[0m' \ @@ -119,7 +226,7 @@ man() { } experiment() { - cd "$(~/bin/experiment $@)" || exit 1 + cd "$(~/bin/experiment $@)" || kill -INT $$ } hump() { @@ -142,7 +249,7 @@ yt() { _yt_dir="${DIR_YOUTUBE}/individual-videos/${_yt_title}--${_yt_id}" mkdir -p "$_yt_dir" - cd "$_yt_dir" || exit 1 + cd "$_yt_dir" || kill -INT $$ echo "$_yt_uri" > 'uri' youtube-dl -c --write-description --write-info-json "$_yt_uri" } @@ -156,7 +263,7 @@ gh_clone() { gh_user_name="$2" gh_dir="${DIR_GITHUB}/${gh_user_name}" mkdir -p "$gh_dir" - cd "$gh_dir" || exit 1 + cd "$gh_dir" || kill -INT $$ gh_fetch_repos "$gh_user_type" "$gh_user_name" \ | jq --raw-output '.[] | select(.fork | not) | .git_url' \ | parallel -j 25 \ @@ -175,7 +282,7 @@ gh_clone_repo() { gh_username=$(echo "$1" | awk -F / '"$1 == "https" && $3 == github.com" {print $4}') gh_dir="${DIR_GITHUB}/${gh_username}" mkdir -p "$gh_dir" - cd "$gh_dir" || exit 1 + cd "$gh_dir" || kill -INT $$ git clone "$1" } @@ -215,7 +322,12 @@ note() { } weather() { - curl "http://wttr.in/$WEATHER_LOCATION" + local _weather_location + case "$1" in + '') _weather_location="$WEATHER_LOCATION";; + *) _weather_location="$1" + esac + curl "http://wttr.in/$_weather_location" } bt_devs_paired() {