Source

dotfiles / config / herbstluftwm / panel.sh

Full commit
#!/bin/bash

# Check which monitor this is
monitor=${1:-0}
geometry=( $(herbstclient monitor_rect "$monitor") )

if [ -z "$geometry" ]; then
  echo >&2 "Invalid monitor: $monitor"
  exit 1
fi

# Config stuff
x=${geometry[0]}
y=${geometry[1]}
#panel_width=${geometry[2]}
panel_height=14

panel_left_width=600
panel_right_width=$((${geometry[2]} - $panel_left_width))

font="-*-fixed-medium-*-*-*-10-*-*-*-*-*-*-*"
bgcolor=$(herbstclient get frame_border_normal_color)
selbg=$(herbstclient get window_border_active_color)
selfg='#CCC'

####
# true if we are using the svn version of dzen2
dzen2_version=$(dzen2 -v 2>&1 | head -n 1 | cut -d , -f 1|cut -d - -f 2)
if [ -z "$dzen2_version" ] ; then
    dzen2_svn="true"
else
    dzen2_svn=""
fi

function uniq_linebuffered() {
  awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
}

# Begin actual bar implementation
herbstclient pad $monitor $panel_height
{
  #### Emit events here
  # player (mpd)
  (mpc idleloop player) &
  mpc_pid=$!

  # tick
  while true; do echo tick; sleep 20; done &
  tick_pid=$!

  # date
  while true; do
    date +'date ^fg()%H^fg(#909090):^fg()%M^fg(#909090), %Y-%m-^fg()%d'
    sleep 1 || break
  done > >(uniq_linebuffered) &
  date_pid=$!

  # hlwm events
  herbstclient --idle

  # exiting; kill stray event-emitting processes
  kill $date_pid
  kill $mpc_pid
  kill $tick_pid

} 2>/dev/null | {
  # Defaults for the bar-drawing part
  TAGS=( $(herbstclient tag_status $monitor) )
  visible=true

  windowtitle=""

  Default="^fg()--"
  sep="^bg()^fg($selbg)|"

  # fields of the right panel
  date="$Default"
  brightstatus="$Default"
  batstatus="$Default"
  thermstatus="$Default"
  nowplaying="$Default"

  while true; do
    # First, draw tags to left panel
    for i in "${TAGS[@]}"; do
      case ${i:0:1} in
        '#')  echo -n "^bg($selbg)^fg($selfg)"    ;;
        '+')  echo -n "^bg(#9CA668)^fg(#141414)"  ;;
        ':')  echo -n "^bg()^fg(#ffffff)"         ;;
        '!')  echo -n "^bg(#FF0675)^fg(#141414)"  ;;
        *)    echo -n "^bg()^fg(#ababab)"         ;;
      esac

      if [ ! -z "$dzen2_svn" ] ; then
        echo -n "^ca(1,herbstclient focus_monitor $monitor && "'herbstclient use "'${i:1}'") '"${i:1} ^ca()"
      else
        echo -n " ${i:1} "
      fi
    done

    # Then, draw the rest of the left panel
    echo -n "$sep"
    echo "^bg()^fg() ${windowtitle//^/^^}"

    # Right panel is trickier...
  # echo >&3 "$sep $date $sep "
  # S="$sep"
  # echo >&3 "$S^bg() $brightstatus $S $thermstatus $S $batstatus $S $date $S $nowplaying $S "

    echo >&3 -n " $sep $brightstatus"
    echo >&3 -n " $sep $thermstatus"
    echo >&3 -n " $sep $batstatus"
    echo >&3 -n " $sep $nowplaying"
    echo >&3 -n " $sep $date"
    echo >&3    " $sep "

    # Wait for next event
    read line || break
    cmd=( $line )
    # branch on event origin
    case "${cmd[0]}" in
      # First, herbstluft's built-in events...
      tag*)
        #echo "reseting tags" >&2
        TAGS=( $(herbstclient tag_status $monitor) )
        ;;

      quit_panel|reload)
        exit ;;

      togglehidepanel)
        echo "^togglehide()"
        if $visible ; then
            visible=false
            herbstclient pad $monitor 0
        else
            visible=true
            herbstclient pad $monitor $panel_height
        fi
        ;;

      focus_changed|window_title_changed)
        windowtitle="${cmd[@]:2}"
        ;;

      focus_changed|window_title_changed)
        windowtitle="${cmd[@]:2}"
        ;;

      # ...then, custom events
      toggle_runner)
        dmenu_run -fn "$font" -nb "$bgcolor" -nf "#efefef" \
                  -sb "$selbg" -sf "$selfg"
        ;;

      date)
        #echo "reseting date" >&2
        date="${cmd[@]:1}"
        ;;

      brightness)
        brightstatus="$(printf '^fg()%d^fg(#909090)%%' $(backlight.sh '?'))"
        ;;

      tick)
        batstatus="$( t='/sys/class/power_supply/BAT1/'
                      curr=$(cat $t/charge_now)
                      full=$(cat $t/charge_full_design)
                      status=$(cat $t/status)
                      percent=$(printf '100 * %d / %d\n' $curr $full | bc)

                      if [ $status == Full ]; then
                        printf '^fg(#909090)%s' $status
                      else
                        printf '^fg(#909090)%s: ^fg()%d%%' $status $percent
                      fi )"

        thermstatus="$( t='/sys/class/thermal/thermal_zone0'
                        curr=$(cat $t/temp)
                        degrees=$(printf '%d / 1000\n' $curr | bc)
                        printf '^fg()%d^fg(#909090)°C' $degrees )"
        ;;

      mpd_player|player)
      # nowplaying="$(mpc current -f '^fg()[%artist% - ][%title%|%file%][ [%time%]]')"
        nowplaying="$(mpc current -f '^fg()%artist% - %title%')"
        ;;
    esac
  done
} 2>/dev/null \
  1> >(dzen2 -fn "$font" -y $y -h $panel_height -fg '#efefef' -bg "$bgcolor" \
             -ta l  -x $x  -w $panel_left_width) \
  3> >(dzen2 -fn "$font" -y $y -h $panel_height -fg '#efefef' -bg "$bgcolor" \
             -ta r  -x $panel_left_width  -w $panel_right_width)