minor fixes and improvements
[distro-setup] / brc
diff --git a/brc b/brc
index a29651bb6589bc0a38446415d3b06212cdec1791..048fa5131688e744e27f66aa2c463a746fefa548 100644 (file)
--- a/brc
+++ b/brc
@@ -15,8 +15,8 @@ else
   # bleh shellcheck can't handle disabling in an elif, so nesting this if.
   # shellcheck disable=SC2154 # set in .bashrc
   if [[ -s $bashrc_dir/err ]]; then
-  # shellcheck source=/a/bin/errhandle/err
-  source $bashrc_dir/err
+    # shellcheck source=/a/bin/errhandle/err
+    source $bashrc_dir/err
   fi
 fi
 
@@ -242,6 +242,7 @@ export SL_FILES_DIR=/b/ds/sl/.iank
 export SL_INFO_DIR=/p/sshinfo
 
 
+
 # * include files
 
 if [[ -s $bashrc_dir/path-add-function ]]; then
@@ -300,6 +301,16 @@ mysrc /a/bin/distro-functions/src/package-manager-abstractions
 
 # * functions
 
+
+# temporary functions
+y() {
+  m "${@//spring/fall}"
+}
+h() {
+  e "${@//spring/fall}"
+}
+
+
 ### begin FSF section ###
 
 # Comments before functions are meant to be good useful
@@ -307,10 +318,12 @@ mysrc /a/bin/distro-functions/src/package-manager-abstractions
 # note.
 
 ## copy bash completion
-# Usage: ORIGINAL_COMMAND TARGET_COMMAND...
 #
 # It copies how the bash completion works from one command to other
-# commands.
+# commands. Generally just use within a .bashrc.
+#
+# Usage: ORIGINAL_COMMAND TARGET_COMMAND...
+#
 ccomp() {
   local c src
   src=$1
@@ -325,17 +338,21 @@ ccomp() {
   eval $c $*
 }
 
-## directory history tracking and navigation.
+## BEGIN functions to change directory better than cd ##
+#
+# The functions:
 #
-# cd becomes a function, also aliased to c. b to go back, f to go
-# forward, cl to list recent directories and choose one.
+# c: acts like cd, but stores directory history: you could alias to cd if you wanted.
+# b: go back
+# f: go forward
+# cl: list recent directories and optionally choose one.
 #
-# The finer details you may want to skip:
+# Finer details you may want to skip:
 #
-# We also define bl to print the list of back and forward directories.
+# bl: print the list of back and forward directories.
 #
-# We keep 2 stacks, forward and back. Unlike with a web browser, the
-# forward stack is not erased when going somewhere new.
+# We keep 2 stacks of directories, forward and back. Unlike with a web
+# browser, the forward stack is not erased when going somewhere new.
 #
 # Recent directories are stored in ~/.cdirs.
 #
@@ -437,7 +454,7 @@ f() {
   #   printf "%s\n" "${_dir_forward[-1]}"
   # fi
 }
-# cd list
+# cl = cd list
 cl() {
   local i line input start
   local -A buttondirs alines
@@ -467,7 +484,9 @@ cl() {
     alines[$line]=t
     buttondirs[${buttons[i]}]="$line"
     printf "%s %s\n" ${buttons[i]} "$line"
-    if (( i == ${#buttons[@]} - 1 )); then
+    # the LINES bit is for when we have a short terminal, just dont print all
+    # the directories. alternative would be to do something like less the list.
+    if (( i == ${#buttons[@]} - 1 )) || { [[ $LINES ]] && (( i == LINES - 3 )); }; then
       break
     fi
     i=$(( i + 1 ))
@@ -482,7 +501,8 @@ cl() {
     c "${buttondirs[$input]}"
   fi
 }
-# back list
+# bl = back list. lists the back and forward directories. i tend to
+# forget this exists and use cl instead.
 bl() {
   local start i j max
   max=10
@@ -523,6 +543,7 @@ bl() {
     fi
   done
 }
+## END functions to change directory better than cd ##
 
 # pee do. run args as a command with output copied to syslog.
 #
@@ -671,21 +692,6 @@ khcopy() {
   ssh-copy-id $1
 }
 
-# ya, hacky hardcoded hostnames in 2023. we could do better
-hssh-update() {
-  local -a failed_hosts
-  for host in kd x3.office.fsf.org syw; do
-    e $host
-    if ! scp /b/fai/fai/config/files/usr/local/bin/hssh/IANK root@$host:/usr/local/bin/hssh; then
-      failed_hosts+=($host)
-    fi
-  done
-  if (( ${#failed_hosts[@]} >= 1 )); then
-    echo failed_hosts=${failed_hosts[*]}
-    return 1
-  fi
-}
-
 a() {
   local x
   x=$(readlink -nf "${1:-$PWD}")
@@ -700,7 +706,7 @@ for field in {1..20}; do
 done
 # h1 = head -n1
 for num in {1..9}; do
-  eval h$num"() { head -n$num; }"
+  eval h$num"() { head -n$num || [[ \$? == 141 ]]; }"
 done
 
 
@@ -1578,14 +1584,16 @@ pst() {
   pstree -apnA
 }
 
-jtail() {
-  journalctl -n 10000 -f "$@"
-}
-jr() { journalctl "$@" ; }
-jrf() { journalctl -f "$@" ; }
+# journalctl with times in the format the --since= and --until= options accept
+jrt() { journalctl -e -n100000 -o short-full "$@"; }
+jr() { journalctl -e -n100000 "$@" ; }
+jrf() { journalctl -n1000 -f "$@" ; }
 jru() {
-  journalctl -u exim4 _SYSTEMD_INVOCATION_ID="$(systemctl show -p InvocationID --value $1)"
+  # the invocation id is "assigned each time the unit changes from an inactive
+  # state into an activating or active state" man systemd.exec
+  journalctl -e --no-tail -u exim4 _SYSTEMD_INVOCATION_ID="$(systemctl show -p InvocationID --value $1)"
 }
+ccomp journalctl jr jrf jru
 
 
 
@@ -1694,6 +1702,11 @@ histrm() {
   history -w
 }
 
+# history without the date
+histplain() {
+  history "$@" | cut -d' ' -f 7-
+}
+
 ccomp grep k ks ksu histrm
 
 
@@ -1732,12 +1745,28 @@ mkdir() { command mkdir -p "$@"; }
 
 nags() {
   # https://github.com/HenriWahl/Nagstamon/issues/357
-  if ! pgrep -f /usr/lib/notification-daemon/notification-daemon >/dev/null; then
-    /usr/lib/notification-daemon/notification-daemon &
+  if ! pgrep -f /usr/bin/dunst >/dev/null; then
+    /usr/bin/dunst &
   fi
   /usr/bin/nagstamon &
 }
 
+# profanity screen
+profsrc() {
+  screen -RD -S profanity
+}
+
+# i dont want to wait for konsole to exit...
+prof() {
+  command prof &>/dev/null &
+}
+# self chat
+sc() {
+  while read -r l; do
+    printf '\033[1A\033[K'; printf "%s\n" "$l"| ts "%F %T" | tee -a /p/self-chat.log
+  done
+}
+
 nmt() {
   # cant use s because sudo -i doesnt work for passwordless sudo command
   case $EUID in
@@ -1789,15 +1818,15 @@ pkx() { # package extract
   c "$(mktemp -d)"
   pkg=$1
   # shellcheck disable=SC2012
-  cached=$(ls -t /var/cache/apt/archives/$pkg* | tail -n1 2>/dev/null) ||:
+  cached=$(ls -t /var/cache/apt/archives/${pkg}_* | tail -n1 2>/dev/null) ||:
   if [[ $cached ]]; then
-    cp $cached .
+    cp $cached .
   else
-    aptitude download $pkg || return 1
+    aptitude download $pkg || return 1
   fi
   tmp=(*); f=${tmp[0]} # only 1 expected
-  ex $f
-  rm -f $f
+  ex $f
+  rm -f $f
 }
 
 # pgrep and kill
@@ -2103,19 +2132,32 @@ sgu() {
 
 
 sk() {
-
-
   # disable a warning with:
   # shellcheck disable=SC2206 # reasoning
 
   # see bash-template/style-guide.md for justifications
 
   local quotes others
-  quotes=2048,2068,2086,2206
-  others=2029,2033,2054,2164
+  quotes=2048,2068,2086,2206,2254
+  others=2029,2032,2033,2054,2164,
   shellcheck -W 999 -x -e $quotes,$others "$@" || return $?
 }
+# sk with quotes. For checking scripts that we expect to take untrusted
+# input in order to verify we quoted vars.
+skq() {
+  local others
+  others=2029,2033,2054,2164
+  shellcheck -W 999 -x -e $others "$@" || return $?
+}
 
+skgit() {
+  local f
+  for f in $(i s | awk '$1 == "modified:" {print $2}'); do
+    if [[ $(head -n1 "$f") == '#!/bin/bash'* ]]; then
+      sk $f ||:
+    fi
+  done
+}
 
 # sl: ssh, but firsh rsync our bashrc and related files to a special
 # directory on the remote host if needed.
@@ -2678,6 +2720,74 @@ spark()
 
 pdfwc() { local f; for f; do echo "$f" "$(pdfinfo "$f" | awk '/^Pages:/ {print $2}')"; done }
 
+
+# nvm install script appended this to my .bashrc. I dont want to run it all the time,
+# so put it in a function.
+nvm-init() {
+  export NVM_DIR="$HOME/.nvm"
+  # shellcheck disable=SC1091 # may not exist, & third party
+  [ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh"  # This loads nvm
+  # shellcheck disable=SC1091 # may not exist, & third party
+  [ -s "$NVM_DIR/bash_completion" ] && source "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
+}
+
+
+leap-year() {
+  if date -d 'february 29' &>/dev/null; then
+    year_days=366
+  else
+    year_days=365
+  fi
+  echo $year_days
+}
+
+# on-battery
+on-bat() {
+  if [[ -e /sys/class/power_supply/AC/online && $(</sys/class/power_supply/AC/online) == 0 ]]; then
+    return 1
+  fi
+}
+
+# make vim work with my light colortheme terminal.
+vim() {
+  if [[ -e ~/.vimrc ]]; then
+    command vim "$@"
+  else
+    command vim -c ':colorscheme peachpuff' "$@"
+  fi
+}
+
+# ls count. usage: pass a directory, get the number of files.
+# https://unix.stackexchange.com/questions/90106/whats-the-most-resource-efficient-way-to-count-how-many-files-are-in-a-director
+lsc() {
+  ls -Uq "$@"|wc -l
+}
+
+# run then notify. close notification after the next prompt.
+rn() {
+  "$@"
+  dunstify -u critical "$*"
+  _psrun=(dunstctl close-all)
+}
+n() {
+  dunstify -u critical n
+  _psrun=(dunstctl close-all)
+}
+
+catnew() {
+  local dir file
+  dir="$1"
+  inotifywait -m "$dir" -e create -e moved_to | while read -r _ _ file; do
+    hr
+    cat "$dir/$file"
+  done
+}
+# cat mail
+cm() {
+  catnew /m/md/$1/new
+}
+
+
 # * misc stuff
 
 
@@ -2768,6 +2878,26 @@ if [[ $- == *i* ]]; then
     if [[ $(jobs -p) ]]; then
       jobs_char='j\j '
     fi
+
+
+    # allow a function to specify a command to run after we run the next
+    # command. Use case: a function makes a persistent notification. If
+    # we happen to be using that terminal, we can just keep working by
+    # entering our next command, even a noop in order to dismiss the
+    # notification, instead of having to explicitly dismiss it.
+    if [[ ${_psrun[@]} ]]; then
+      if (( _psrun_count >= 1 )); then
+
+        "${_psrun[@]}" ||:
+        _psrun_count=0
+        unset _psrun
+      else
+        _psrun_count=$(( _psrun_count + 1 ))
+      fi
+    else
+      _psrun_count=0
+    fi
+
     # We could test if sudo is active with sudo -nv
     # but then we get an email and log of lots of failed sudo commands.
     # We could turn those off, but seems better not to.
@@ -2779,6 +2909,15 @@ if [[ $- == *i* ]]; then
     fi
     PS1="${PS1%"${PS1#*[wW]}"} $jobs_char$psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
 
+    # copy of what is automatically added by guix.
+    # adds [env] to PS1 if GUIX_ENVIRONMENT is set and PS1 contains '$';
+    if [ -n "$GUIX_ENVIRONMENT" ]; then
+      if [[ $PS1 =~ (.*)"\\$" ]]; then
+        PS1="${BASH_REMATCH[1]} [env]\\\$ "
+      fi
+    fi
+
+
     # set titlebar. instead, using more advanced
     # titelbar below
     #echo -ne "$_title_escape $HOSTNAME ${PWD/#$HOME/~}  \007"