sb

shitty bar for dwm
git clone git://git.ckyln.com/~cem/sb.git
Log | Files | Refs | README | LICENSE

sb (2458B)


      1 #!/bin/sh
      2 # See the LICENSE file for copyright and license details.
      3 # shellcheck disable=1090
      4 
      5 FUNCPATH="./func"
      6 VERSION="git"
      7 
      8 pidfile="/tmp/sb-$USER.pid"
      9 
     10 out() { printf '%s\n' "$@" >&2 ;}
     11 
     12 usage() {
     13     out "${0##*/} [-dnvx]" "" \
     14         " Options:" \
     15         " -d        Use the default configuration" \
     16         " -n        Print to stdout instead of the bar" \
     17         " -v        Print version number and exit" \
     18         " -x        Kill existing sb instance" "" \
     19         "${0##*/}-$VERSION" \
     20         "2019-2020 (c) Cem Keylan"
     21     exit 1
     22 }
     23 
     24 cleanup() { rm -f "$pidfile" ; exit 0;}
     25 
     26 while [ "$1" ] ; do
     27     case "$1" in
     28         -d) noconfig=1 ; shift ;;
     29         -v) printf '%s\n' "${0##*/}-$VERSION"; exit 0 ;;
     30         -n) nobar=1 ; shift ;;
     31         -x) [ -e "/tmp/sb-$USER.pid" ] || {
     32                 out "There is no running instance of sb"
     33                 exit 1
     34             }
     35             read -r pid < "$pidfile"
     36             kill "$pid"
     37             rm "$pidfile"
     38             exit 0
     39             ;;
     40         *) usage ;;
     41     esac
     42 done
     43 
     44 
     45 [ "$noconfig" != 1 ] && [ -e "${XDG_CONFIG_HOME:=$HOME/.config}/sbrc" ] && . "$XDG_CONFIG_HOME/sbrc"
     46 
     47 # Load Functions
     48 for func in "$FUNCPATH/"* ; do [ -f "$func" ] && . "$func"; done
     49 for func in "${XDG_CONFIG_HOME:=$HOME/.config}/sb-func/"* ; do [ -f "$func" ] && . "$func" ; done
     50 
     51 # Unless running from the command line, check for
     52 # an existing instance
     53 [ "$nobar" ] || {
     54     [ -e "/tmp/sb-$USER.pid" ] && {
     55         [ "$KILLEXISTING" != 1 ] && {
     56             out "Error: sb is already running." \
     57                 "If you think that this is not true, run 'sb -x'."
     58             exit 1
     59         }
     60         read -r pid < "/tmp/sb-$USER.pid"
     61 	kill "$pid" 2>/dev/null
     62     }
     63     printf '%s\n' "$$" > "/tmp/sb-$USER.pid"
     64 }
     65 
     66 : "${DELIMITER:=|}"
     67 
     68 # If the bar command is not defined, set a default bar.
     69 command -v bar >/dev/null 2>&1 ||
     70     bar() { storage ; ram ; gethostname ; datetime ;}
     71 
     72 trap cleanup EXIT INT QUIT HUP
     73 
     74 if [ "$nobar" = 1 ]; then
     75     while :; do
     76         # If stdout belongs to sb, clear the screen each time the bar is being
     77         # printed. If stdout doesn't belong to us, that means sb is being piped
     78         # and we are most likely not dealing with a terminal. In such a case we
     79         # should not clear the screen.
     80         [ -t 1 ] && clear
     81         printf ' %s\n' "$(bar)"
     82         sleep "${SLEEPTIME:=1}"
     83     done
     84 else
     85     while :; do xsetroot -name " $(bar)"; sleep "${SLEEPTIME:=1}"; done
     86 fi