My personal dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

panel.sh 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env bash
  2. hc() { "${herbstclient_command[@]:-herbstclient}" "$@" ;}
  3. monitor=${1:-0}
  4. geometry=( $(herbstclient monitor_rect "$monitor") )
  5. if [ -z "$geometry" ] ;then
  6. echo "Invalid monitor $monitor"
  7. exit 1
  8. fi
  9. # geometry has the format W H X Y
  10. x=${geometry[0]}
  11. y=${geometry[1]}
  12. panel_width=${geometry[2]}
  13. panel_height=16
  14. font="-*-fixed-medium-*-*-*-12-*-*-*-*-*-*-*"
  15. bgcolor=$(hc get frame_border_normal_color)
  16. selbg=$(hc get window_border_active_color)
  17. selfg='#101010'
  18. ####
  19. # Try to find textwidth binary.
  20. # In e.g. Ubuntu, this is named dzen2-textwidth.
  21. if which textwidth &> /dev/null ; then
  22. textwidth="textwidth";
  23. elif which dzen2-textwidth &> /dev/null ; then
  24. textwidth="dzen2-textwidth";
  25. else
  26. echo "This script requires the textwidth tool of the dzen2 project."
  27. exit 1
  28. fi
  29. ####
  30. # true if we are using the svn version of dzen2
  31. # depending on version/distribution, this seems to have version strings like
  32. # "dzen-" or "dzen-x.x.x-svn"
  33. if dzen2 -v 2>&1 | head -n 1 | grep -q '^dzen-\([^,]*-svn\|\),'; then
  34. dzen2_svn="true"
  35. else
  36. dzen2_svn=""
  37. fi
  38. if awk -Wv 2>/dev/null | head -1 | grep -q '^mawk'; then
  39. # mawk needs "-W interactive" to line-buffer stdout correctly
  40. # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=593504
  41. uniq_linebuffered() {
  42. awk -W interactive '$0 != l { print ; l=$0 ; fflush(); }' "$@"
  43. }
  44. else
  45. # other awk versions (e.g. gawk) issue a warning with "-W interactive", so
  46. # we don't want to use it there.
  47. uniq_linebuffered() {
  48. awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
  49. }
  50. fi
  51. hc pad $monitor $panel_height
  52. {
  53. ### Event generator ###
  54. # based on different input data (mpc, date, hlwm hooks, ...) this generates events, formed like this:
  55. # <eventname>\t<data> [...]
  56. # e.g.
  57. # date ^fg(#efefef)18:33^fg(#909090), 2013-10-^fg(#efefef)29
  58. #mpc idleloop player &
  59. while true ; do
  60. # "date" output is checked once a second, but an event is only
  61. # generated if the output changed compared to the previous run.
  62. date +$'date\t^fg(#efefef)%H:%M^fg(#909090), %Y-%m-^fg(#efefef)%d'
  63. sleep 1 || break
  64. done > >(uniq_linebuffered) &
  65. childpid=$!
  66. hc --idle
  67. kill $childpid
  68. } 2> /dev/null | {
  69. IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
  70. visible=true
  71. date=""
  72. windowtitle=""
  73. while true ; do
  74. ### Output ###
  75. # This part prints dzen data based on the _previous_ data handling run,
  76. # and then waits for the next event to happen.
  77. bordercolor="#26221C"
  78. separator="^bg()^fg($selbg)|"
  79. # draw tags
  80. for i in "${tags[@]}" ; do
  81. case ${i:0:1} in
  82. '#')
  83. echo -n "^bg($selbg)^fg($selfg)"
  84. ;;
  85. '+')
  86. echo -n "^bg(#9CA668)^fg(#141414)"
  87. ;;
  88. ':')
  89. echo -n "^bg()^fg(#ffffff)"
  90. ;;
  91. '!')
  92. echo -n "^bg(#FF0675)^fg(#141414)"
  93. ;;
  94. *)
  95. echo -n "^bg()^fg(#ababab)"
  96. ;;
  97. esac
  98. if [ ! -z "$dzen2_svn" ] ; then
  99. # clickable tags if using SVN dzen
  100. echo -n "^ca(1,\"${herbstclient_command[@]:-herbstclient}\" "
  101. echo -n "focus_monitor \"$monitor\" && "
  102. echo -n "\"${herbstclient_command[@]:-herbstclient}\" "
  103. echo -n "use \"${i:1}\") ${i:1} ^ca()"
  104. else
  105. # non-clickable tags if using older dzen
  106. echo -n " ${i:1} "
  107. fi
  108. done
  109. echo -n "$separator"
  110. echo -n "^bg()^fg() ${windowtitle//^/^^}"
  111. # small adjustments
  112. right="$separator^bg() $date $separator"
  113. right_text_only=$(echo -n "$right" | sed 's.\^[^(]*([^)]*)..g')
  114. # get width of right aligned text.. and add some space..
  115. width=$($textwidth "$font" "$right_text_only ")
  116. echo -n "^pa($(($panel_width - $width)))$right"
  117. echo
  118. ### Data handling ###
  119. # This part handles the events generated in the event loop, and sets
  120. # internal variables based on them. The event and its arguments are
  121. # read into the array cmd, then action is taken depending on the event
  122. # name.
  123. # "Special" events (quit_panel/togglehidepanel/reload) are also handled
  124. # here.
  125. # wait for next event
  126. IFS=$'\t' read -ra cmd || break
  127. # find out event origin
  128. case "${cmd[0]}" in
  129. tag*)
  130. #echo "resetting tags" >&2
  131. IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
  132. ;;
  133. date)
  134. #echo "resetting date" >&2
  135. date="${cmd[@]:1}"
  136. ;;
  137. quit_panel)
  138. exit
  139. ;;
  140. togglehidepanel)
  141. currentmonidx=$(hc list_monitors | sed -n '/\[FOCUS\]$/s/:.*//p')
  142. if [ "${cmd[1]}" -ne "$monitor" ] ; then
  143. continue
  144. fi
  145. if [ "${cmd[1]}" = "current" ] && [ "$currentmonidx" -ne "$monitor" ] ; then
  146. continue
  147. fi
  148. echo "^togglehide()"
  149. if $visible ; then
  150. visible=false
  151. hc pad $monitor 0
  152. else
  153. visible=true
  154. hc pad $monitor $panel_height
  155. fi
  156. ;;
  157. reload)
  158. exit
  159. ;;
  160. focus_changed|window_title_changed)
  161. windowtitle="${cmd[@]:2}"
  162. ;;
  163. #player)
  164. # ;;
  165. esac
  166. done
  167. ### dzen2 ###
  168. # After the data is gathered and processed, the output of the previous block
  169. # gets piped to dzen2.
  170. } 2> /dev/null | dzen2 -w $panel_width -x $x -y $y -fn "$font" -h $panel_height \
  171. -e 'button3=;button4=exec:herbstclient use_index -1;button5=exec:herbstclient use_index +1' \
  172. -ta l -bg "$bgcolor" -fg '#efefef'