bunch of new stuff, a few fixes
[distro-setup] / brc
diff --git a/brc b/brc
index b2df5c28bcd8aa102fc4eef73ca43b69b379610e..4274d6e968ea3523e8fbb2569abf78bb9eabc8e2 100644 (file)
--- a/brc
+++ b/brc
@@ -242,6 +242,25 @@ export SL_FILES_DIR=/b/ds/sl/.iank
 export SL_INFO_DIR=/p/sshinfo
 
 
+### begin pyenv ###
+
+# this is adapted from things printed to term after install
+# pyenv. commented for now since I'm not actually using pyenv.
+
+# export PYENV_ROOT="$HOME/.pyenv"
+# command -v pyenv &>/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
+# command -v pyenv &>/dev/null && eval "$(pyenv init -)"
+
+
+# output showed this example for pyenv-virtualenv, which i have no idea
+# what it is, but leaving it as a comment in case I end up doing python
+# dev.
+
+#eval "$(pyenv virtualenv-init -)"
+### end begin pyenv ###
+
+
+
 # * include files
 
 if [[ -s $bashrc_dir/path-add-function ]]; then
@@ -300,6 +319,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 +336,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 +356,21 @@ ccomp() {
   eval $c $*
 }
 
-## directory history tracking and navigation.
+## BEGIN functions to change directory better than cd ##
 #
-# cd becomes a function, also aliased to c. b to go back, f to go
-# forward, cl to list recent directories and choose one.
+# The functions:
 #
-# The finer details you may want to skip:
+# 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.
 #
-# We also define bl to print the list of back and forward directories.
+# Finer details you may want to skip:
 #
-# We keep 2 stacks, forward and back. Unlike with a web browser, the
-# forward stack is not erased when going somewhere new.
+# bl: print the list of back and forward directories.
+#
+# 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 +472,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 +502,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 +519,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 +561,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,33 +710,6 @@ khcopy() {
   ssh-copy-id $1
 }
 
-# ya, hacky hardcoded hostnames in 2023. we could do better
-hssh-update() {
-  local -a failed_hosts hosts
-  case $HOSTNAME in
-    sy|kd)
-      hosts=(
-        kd x3.office.fsf.org syw
-      )
-      ;;
-    x3)
-      hosts=(
-        b8.nz sywg.b8.nz
-      )
-      ;;
-  esac
-  for host in ${hosts[@]}; 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}")
@@ -804,12 +816,17 @@ cf() {
   done
 }
 caf() {
-  # shellcheck disable=SC2033
-  find -L "$@" -type f -not \( -name .svn -prune -o -name .git -prune \
-       -o -name .hg -prune -o -name .editor-backups -prune \
-       -o -name .undo-tree-history -prune \) \
-       -exec bash -c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
 
+  local file
+find -L "$@" -type f -not \( -name .svn -prune -o -name .git -prune \
+       -o -name .hg -prune -o -name .editor-backups -prune \
+       -o -name .undo-tree-history -prune \) -printf '%h\0%d\0%p\n' | sort -t '\0' -n \
+  | awk -F '\0' '{print $3}' 2>/dev/null | while read -r file; do
+    hr
+    printf "%s\n" "$file"
+    hr
+    cat "$file"
+done
 }
 ccomp cat cf caf
 
@@ -821,6 +838,10 @@ clc() {
   echo "scale=3; $x" | bc -l
 }
 
+cx() {
+  chmod +X "$@"
+  }
+
 cam() {
   git commit -am "$*"
 }
@@ -1369,9 +1390,6 @@ and works in older versions of git which did not have that."
 
 g() {
 
-  # todo: patch emacs so it will look elsewhere. this is kinda sad:
-  # https://emacs.stackexchange.com/questions/4253/how-to-start-emacs-with-a-custom-user-emacs-directory
-
   local args gdb=false
 
   if [[ $EMACSDIR ]]; then
@@ -1394,6 +1412,10 @@ g() {
     fi
   fi
   if [[ $EMACSDIR ]]; then
+
+    # todo: we don't have to alter HOME since emacs 29+, we can set
+    # user-emacs-directory with the flag --init-directory
+
     # Alter the path here, otherwise the nfs mount gets triggered on the
     # first path lookup when emacs is not being used.
     # shellcheck disable=SC2098 disable=SC2097 # false positive
@@ -1766,8 +1788,12 @@ profsrc() {
 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
@@ -1820,15 +1846,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
@@ -2141,7 +2167,7 @@ sk() {
 
   local quotes others
   quotes=2048,2068,2086,2206,2254
-  others=2029,2033,2054,2164
+  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
@@ -2750,10 +2776,59 @@ on-bat() {
   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() {
+  # shellcheck disable=SC2790 # intentional
+  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
 
 
 if $use_color && type -p tput &>/dev/null; then
+  # this is nice for a dark background terminal:
+  # https://github.com/trapd00r/LS_COLORS
+  # I would like if there was something similar for light.
+
+  # the default bold green is too light.
+  # this explains the codes: https://gist.github.com/thomd/7667642
+  export LS_COLORS=ex=1
+
   term_bold="$(tput bold)"
   term_red="$(tput setaf 1)"
   term_green="$(tput setaf 2)"
@@ -2840,6 +2915,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.
@@ -2851,6 +2946,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"