Group shell activity by month
[khome.git] / home / lib / login_functions.sh
index 13a2ba2..2287d32 100644 (file)
@@ -1,8 +1,97 @@
+#
+
+d() {
+    local -r word=$(fzf < /usr/share/dict/words)
+    dict "$word"
+}
+
+shell_activity_report() {
+    # TODO: optional concrete number output
+    # TODO: manual weekday calc (since forking date is so expensive)
+    # TODO: optional combinations of granularities: hour, weekday, month, year
+    history \
+    | awk '
+        {
+            # 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
+                month = d[2] + 0
+                hour = t[1] + 0
+                c = count[month, hour]++
+            }
+            if (c > max)
+                max = c
+        }
+
+        END {
+            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"
+            for (month = 1; month <= 12; month++) {
+                printf "%s\n", m[month];
+                for (hour=0; hour<24; hour++) {
+                    c = count[month, hour]
+                    printf "  %2d ", hour
+                    for (i = 1; i <= (c * 100) / max; i++)
+                        printf "|"
+                    printf "\n"
+                }
+            }
+        }'
+}
+
+top_commands() {
+    history \
+    | awk '
+        {
+            count[$4]++
+        }
+
+        END {
+            for (cmd in count)
+                print count[cmd], cmd
+        }' \
+    | sort -n -r -k 1 \
+    | head -50 \
+    | awk '
+        {
+            cmd[NR] = $2
+            c = count[NR] = $1 + 0  # + 0 to coerce number from string
+            if (c > max)
+                max = c
+        }
+
+        END {
+            for (i = 1; i <= NR; i++) {
+                c = count[i]
+                printf "%s %d ", cmd[i], c
+                scaled = (c * 100) / max
+                for (j = 1; j <= scaled; j++)
+                    printf "|"
+                printf "\n"
+            }
+        }' \
+    | column -t
+}
+
 # Top Disk-Using directories
 # TODO: Consider using numfmt instead of awk
 tdu() {
     du "$1" \
-    | sort -n -k 1 -r --parallel="$(nproc)" \
+    | sort -n -k 1 -r \
     | head -50 \
     | awk '
         {
@@ -15,6 +104,21 @@ tdu() {
     | cut -c 1-115
 }
 
+# Top Disk-Using Files
+tduf() {
+    find "$1" -type f -printf '%s\t%p\0' \
+    | sort -z -n -k 1 -r \
+    | head -z -n 50 \
+    | gawk -v RS='\0' '
+        {
+            size = $1
+            path = $0
+            sub("^" $1 "\t+", "", path)
+            gb = size / 1024 / 1024 / 1024
+            printf("%f\t%s\n", gb, path)
+        }'
+}
+
 # Most-recently modified file system objects
 recent() {
     # NOTES:
@@ -67,19 +171,41 @@ experiment() {
 }
 
 hump() {
-    ledit -l $(stty size | awk '{print $2}') ocaml $@
+    ledit -l "$(stty size | awk '{print $2}')" ocaml $@
 }
 
 howto() {
     cat "$(find  ~/Archives/Documents/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
 }
 
+yt() {
+    local _yt_uri
+    local _yt_id
+    local _yt_title
+    local _yt_dir
+
+    _yt_uri="$1"
+    _yt_id=$(youtube-dl --get-id "$_yt_uri")
+    _yt_title=$(youtube-dl --get-title "$_yt_uri")
+    _yt_dir="${DIR_YOUTUBE}/individual-videos/${_yt_title}--${_yt_id}"
+
+    mkdir -p "$_yt_dir"
+    cd "$_yt_dir" || exit 1
+    echo "$_yt_uri" > 'uri'
+    youtube-dl -c --write-description --write-info-json "$_yt_uri"
+}
+
 gh_fetch_repos() {
     curl "https://api.github.com/$1/$2/repos?page=1&per_page=10000"
 }
 
 gh_clone() {
-    gh_fetch_repos "$1" "$2" \
+    gh_user_type="$1"
+    gh_user_name="$2"
+    gh_dir="${DIR_GITHUB}/${gh_user_name}"
+    mkdir -p "$gh_dir"
+    cd "$gh_dir" || exit 1
+    gh_fetch_repos "$gh_user_type" "$gh_user_name" \
     | jq --raw-output '.[] | select(.fork | not) | .git_url' \
     | parallel -j 25 \
     git clone {}
@@ -95,16 +221,15 @@ gh_clone_org() {
 
 gh_clone_repo() {
     gh_username=$(echo "$1" | awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
-    gh_dir="${HOME}/Archives/Software/src/repos/remote/github.com/${gh_username}"
+    gh_dir="${DIR_GITHUB}/${gh_username}"
     mkdir -p "$gh_dir"
     cd "$gh_dir" || exit 1
     git clone "$1"
-    cd - || exit 1
 }
 
 work_log_template() {
 cat << EOF
-$(date +%F)
+$(date '+%F %A')
 ==========
 
 Morning report
@@ -128,10 +253,15 @@ work_log() {
     then
         work_log_template > "$file_work_log_today"
     fi
-    vim "$file_work_log_today"
+    vim -c 'set spell' "$file_work_log_today"
 
 }
 
+note() {
+    mkdir -p "$DIR_NOTES"
+    vim -c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
+}
+
 weather() {
     curl "http://wttr.in/$WEATHER_LOCATION"
 }
@@ -147,3 +277,16 @@ bt_devs() {
     | awk '{print $2}' \
     | xargs bluetoothctl -- info
 }
+
+run() {
+    stderr="$(mktemp)"
+    $@ 2> >(tee "$stderr")
+    code="$?"
+    urgency=''
+    case "$code" in
+        0) urgency='normal';;
+        *) urgency='critical'
+    esac
+    notify-send -u "$urgency" "Job done: $code" "$(cat $stderr)"
+    rm "$stderr"
+}
This page took 0.032974 seconds and 4 git commands to generate.