Advertisement
Guest User

Untitled

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