mostly features, some minor bug fixes
[distro-setup] / brc
diff --git a/brc b/brc
index 75a7393eb15c7301d19b22b6a5498fcb9d4b70a1..99fc5b8eb1ddbc87ddd22aef2f026318b04e89c2 100644 (file)
--- a/brc
+++ b/brc
@@ -279,6 +279,16 @@ mysrc() {
 mysrc /a/bin/small-misc-bash/ll-function
 mysrc /a/bin/distro-functions/src/package-manager-abstractions
 
+# things to remember:
+# ALT-C - cd into the selected directory
+# CTRL-T - Paste the selected file path into the command line
+#
+# good guide to some of its basic features is the readme file
+# https://github.com/junegunn/fzf
+
+# if [[ -s /usr/share/doc/fzf/examples/key-bindings.bash ]]; then
+#   source /usr/share/doc/fzf/examples/key-bindings.bash
+# fi
 
 # * functions
 
@@ -949,13 +959,18 @@ digdiff() {
   diff -u /tmp/digdiff <(digsort $s2 "$@")
 }
 
+# date in a format i like reading
 dt() {
   date "+%A, %B %d, %r" "$@"
 }
 dtr() {
   date -R "$@"
 }
-ccomp date dt dtr
+# date with all digits in a format i like
+dtd() {
+  date +%F_%T% "$@"
+}
+ccomp date dt dtr dtd
 
 dus() { # du, sorted, default arg of
   du -sh ${@:-*} | sort -h
@@ -1011,6 +1026,9 @@ etail2() {
 ccomp tail etail etail2
 
 
+showkeys() {
+  ssh "$@" cat .ssh/authorized_keys{,2}
+}
 
 
 # print exim old pids
@@ -1097,6 +1115,20 @@ econfdev() {
   update-exim4.conf -d /tmp/edev/etc/exim4 -o /tmp/edev/e.conf
 }
 
+# exim grep in
+# show important information about incoming mail in the exim log
+egrin() {
+  sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).*T="(.*)" from (<[^ ]+> .*$)/\1 \4\n \3/p' <${1:-/var/log/exim4/mainlog}
+}
+
+# 2nd line is message-id:
+egrinid() {
+  sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).* id=([^ ]+) T="(.*)" from (<[^ ]+> .*$)/\1 \5\n \3\n \4/p' <${1:-/var/log/exim4/mainlog}
+}
+etailin() {
+  tail -F /var/log/exim4/mainlog | sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).*T="(.*)" from (<[^ ]+> .*$)/\1 \4\n \3/p'
+}
+
 
 
 
@@ -1115,12 +1147,74 @@ faf() { # find all files. use -L to follow symlinks
        -o -name .undo-tree-history -prune \) -type f 2>/dev/null
 }
 
-# full path without resolving symlinks
+# usage ffconcat FILES_TO_CONCAT OUTPUT_FILE
+ffconcat() {
+  local tmpf
+  tmpf=$(mktemp)
+  printf "file '%s'\n" "$1" >$tmpf
+  while (( $# > 1 )); do
+    shift
+    printf "file '%s'\n" "$1" >>$tmpf
+  done
+  # https://trac.ffmpeg.org/wiki/Concatenate
+  ffmpeg -f concat -safe 0 -i $tmpf -c copy "$1"
+  rm $tmpf
+}
+ffremux() {
+  local tmpf tmpd
+  if (( $# == 0 )); then
+    echo ffremux error expected args >&2
+    return 1
+  fi
+  tmpd=$(mktemp -d)
+  for f; do
+    tmpf=$tmpd/"${f##*/}"
+    ffmpeg -i "$f" -c:v copy -c:a copy $tmpf
+    cat $tmpf >"$f"
+  done
+  rm -r $tmpd
+}
+
+
+
+# absolute path of file/dir without resolving symlinks.
+#
+# This is what realpath -s does in most cases, but sometimes it
+# actually resolves symlinks, at least when they are in /.
+#
+# Note, if run on a dir, if the final component is relative, it won't
+# resolve that. Use the below fpd for that.
+#
+# note: we could make a variation of this which
+# assigns to a variable name using eval, so that we don't have to do
+# x=$(fp somepath), which might save subshell overhead and look nice,
+# but I'm not going to bother.
 fp() {
-  local dir base
-  base="${1##*/}"
-  dir="${1%$base}"
-  printf "%s/%s\n" $(cd $dir; pwd) "$base"
+  local initial_oldpwd initial_pwd dir base
+  initial_oldpwd="$OLDPWD"
+  initial_pwd="$PWD"
+  if [[ $1 == */* ]]; then
+    dir="${1%/*}"
+    base="/${1##*/}"
+    # CDPATH because having it set will cause cd to possibly print output
+    CDPATH= cd "$dir"
+    printf "%s%s\n" "$PWD" "$base"
+    CDPATH= cd "$initial_pwd"
+    OLDPWD="$initial_oldpwd"
+  else
+    printf "%s/%s\n" "$PWD" "$1"
+  fi
+}
+# full path of directory without resolving symlinks
+fpd() {
+  local initial_oldpwd initial_pwd dir
+  initial_oldpwd="$OLDPWD"
+  initial_pwd="$PWD"
+  dir="$1"
+  CDPATH= cd "$dir"
+  printf "%s%s\n" "$PWD" "$base"
+  cd "$initial_pwd"
+  OLDPWD="$initial_oldpwd"
 }
 
 
@@ -2556,6 +2650,10 @@ if [[ $- == *i* ]]; then
     if [[ $MAIL_HOST && $MAIL_HOST != "$HOSTNAME" ]]; then
       ps_char="@ $ps_char"
     fi
+    jobs_char=
+    if [[ $(jobs -p) ]]; then
+      jobs_char='j\j '
+    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.
@@ -2565,7 +2663,7 @@ if [[ $- == *i* ]]; then
     if [[ ! $HISTFILE ]]; then
       ps_char="NOHIST $ps_char"
     fi
-    PS1="${PS1%"${PS1#*[wW]}"} $psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
+    PS1="${PS1%"${PS1#*[wW]}"} $jobs_char$psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
 
     # set titlebar. instead, using more advanced
     # titelbar below