i3_lemonbar_parser.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/bin/bash
  2. #
  3. # Input parser for i3 bar
  4. # 14 ago 2015 - Electro7
  5. # config
  6. . $(dirname $0)/i3_lemonbar_config
  7. # min init
  8. irc_n_high=0
  9. title="%{F${color_head} B${color_sec_b2}}${sep_right}%{F${color_head} B${color_sec_b2}%{T2} ${icon_prog} %{F${color_sec_b2} B-}${sep_right}%{F- B- T1}"
  10. # parser
  11. while read -r line ; do
  12. case $line in
  13. ### SYS Case ### {{{
  14. SYS*)
  15. ## conky=, 0=wday, 1=mday, 2=month, 3=time, 4=cpu, 5=mem, 6=disk /, 7-8=up/down wlan, 9-10=up/down eth,
  16. ## 11=eth ip, 12=wlan ip, 13=comp temp, 14=battery %, 15=battery stat, 16=wifi %,
  17. ## Things to add: external IP, weather, pianobar, only show eth0/wlan0 if up?
  18. ## Make HD use show free? Check offlineimap unread. Network weechat check?
  19. sys_arr=(${line#???})
  20. ### Time and Date ### {{{
  21. ## Date
  22. if [ ${res_w} -gt 1024 ]; then
  23. date="${sys_arr[0]} ${sys_arr[1]} ${sys_arr[2]}"
  24. else
  25. date="${sys_arr[1]} ${sys_arr[2]}"
  26. fi
  27. date="%{F${color_sec_b1}}${sep_left}%{F${color_icon} B${color_sec_b1}} %{T2}${icon_clock}%{F- T1} ${date}"
  28. ## Time
  29. time="%{F${color_head}}${sep_left}%{F${color_back} B${color_head}} ${sys_arr[3]} %{F- B-}"
  30. ### End Time ### }}}
  31. ### CPU ### {{{
  32. if [ ${sys_arr[4]} -gt ${cpu_alert} ]; then
  33. cpu_cback=${color_cpu}; cpu_cicon=${color_back}; cpu_cfore=${color_back};
  34. else
  35. cpu_cback=${color_sec_b2}; cpu_cicon=${color_icon}; cpu_cfore=${color_fore};
  36. fi
  37. cpu="%{F${cpu_cback}}${sep_left}%{F${cpu_cicon} B${cpu_cback}} %{T2}${icon_cpu}%{F${cpu_cfore} T1} ${sys_arr[4]}%"
  38. ### End CPU ### }}}
  39. ### Memory and Disk ### {{{
  40. ## mem (ram)
  41. mem="%{F${cpu_cicon}}${sep_l_left} %{T2}${icon_mem}%{F${cpu_cfore} T1} ${sys_arr[5]}%"
  42. ## disk /
  43. diskr="%{F${color_sec_b1}}${sep_left}%{F${color_icon} B${color_sec_b1}} %{T2}${icon_hd}%{F- T1} ${sys_arr[6]}%"
  44. ### End Memory ### }}}
  45. ### Net Speed ### {{{
  46. ## made to replace WLAN and Eth sections
  47. ## Will prefer eth0 if up
  48. if [ "${sys_arr[9]}" == "down" ]; then
  49. if [ "${sys_arr[7]}" == "down" ];then
  50. nets_dv="×"; nets_uv="×";
  51. nets_cback=${color_sec_b1}; nets_cicon=${color_disable}; nets_cfore=${color_disable};
  52. else
  53. nets_dv=${sys_arr[7]}K; nets_uv=${sys_arr[8]}K;
  54. if [ ${nets_dv:0:-3} -gt ${net_alert} ] || [ ${nets_uv:0:-3} -gt ${net_alert} ]; then
  55. nets_cback=${color_net}; nets_cicon=${color_back}; nets_cfore=${color_back};
  56. else
  57. nets_cback=${color_sec_b1}; nets_cicon=${color_icon}; nets_cfore=${color_fore};
  58. fi
  59. fi
  60. else
  61. nets_dv=${sys_arr[9]}K; nets_uv=${sys_arr[10]}K;
  62. if [ ${nets_dv:0:-3} -gt ${net_alert} ] || [ ${nets_uv:0:-3} -gt ${net_alert} ]; then
  63. nets_cback=${color_net}; nets_cicon=${color_back}; nets_cfore=${color_back};
  64. else
  65. nets_cback=${color_sec_b1}; nets_cicon=${color_icon}; nets_cfore=${color_fore};
  66. fi
  67. fi
  68. nets_d="%{F${nets_cback}}${sep_left}%{F${nets_cicon} B${nets_cback}} %{T2}${icon_dl}%{F${nets_cfore} T1} ${nets_dv}"
  69. nets_u="%{F${nets_cicon}}${sep_l_left} %{T2}${icon_ul}%{F${nets_cfore} T1} ${nets_uv}"
  70. ### End Net Speed ### }}}
  71. ### Battery ### {{{
  72. ## Icon 0 1 2 3 4
  73. ## Bat >= NA 11 37 63 90
  74. ## Range 0-10 11-36 37-62 63-89 90-100
  75. if [ ${sys_arr[14]} -ge 90 ]; then
  76. bat_icon=${icon_bat4}; bat_cicon=${color_bat_high}; bat_cfore=${color_fore}; bat_cback=${color_sec_b1};
  77. elif [ ${sys_arr[14]} -ge 63 ]; then
  78. bat_icon=${icon_bat3}; bat_cicon=${color_bat_high}; bat_cfore=${color_fore}; bat_cback=${color_sec_b1};
  79. elif [ ${sys_arr[14]} -ge 37 ]; then
  80. bat_icon=${icon_bat2}; bat_cicon=${color_icon}; bat_cfore=${color_fore}; bat_cback=${color_bat_mid};
  81. elif [ ${sys_arr[14]} -ge 11 ]; then
  82. bat_icon=${icon_bat1}; bat_cicon=${color_icon}; bat_cfore=${color_fore}; bat_cback=${color_bat_low};
  83. else
  84. bat_icon=${icon_bat0}; bat_cicon=${color_icon}; bat_cfore=${color_fore}; bat_cback=${color_bat_out};
  85. fi
  86. if [ ${sys_arr[15]} == "C" ] || [ ${sys_arr[15]} == "F" ]; then
  87. bat_icon=${icon_bat_plug}; bat_cicon=${color_icon};
  88. fi
  89. bat="%{F${bat_cback}}${sep_left}%{F${bat_cicon} B${bat_cback}} %{T2}${bat_icon}%{F- T1} ${sys_arr[14]}%"
  90. ### End Battery ### }}}
  91. ### Temperature ### {{{
  92. if [ ${sys_arr[13]} -gt ${temp_alert} ]; then
  93. temp_cback=${color_temp}; temp_cicon=${color_back}; temp_cfore=${color_back};
  94. else
  95. temp_cback=${color_sec_b2}; temp_cicon=${color_icon}; temp_cfore=${color_fore};
  96. fi
  97. temp="%{F${temp_cback}}${sep_left}%{F${temp_cicon} B${temp_cback}} %{T2}${icon_temp}%{F${temp_cfore} T1} ${sys_arr[13]}F"
  98. ### End Temp ### }}}
  99. ### Local IP ### {{{
  100. ## To save space, I don't want to give both eth0 and wlan0 spots
  101. ## So I will make eth0 > wlan0, as if my laptop has eth, I probably want it.
  102. ## eth ${sys_arr[11]} wlan ${sys_arr[12]}
  103. if [ ${sys_arr[11]} != "down" ]; then
  104. lip_select="${sys_arr[11]}"; lip_icon="${icon_local_eth}";
  105. elif [ ${sys_arr[12]} != "down" ]; then
  106. lip_select="${sys_arr[12]}"; lip_icon="${icon_local_wifi}";
  107. else
  108. lip_select="No IP"; lip_icon="${icon_local_out}";
  109. fi
  110. local_ip="%{F${color_sec_b1}}${sep_left}%{F${color_icon} B${color_sec_b1}} %{T2}${lip_icon}%{F- T1} ${lip_select}"
  111. ### End Local IP ### }}}
  112. ### Wifi Percent ### {{{
  113. if [ ${sys_arr[12]} != "down" ]; then
  114. wifi="%{F${color_icon}}${sep_l_left}%{F${color_icon} B${color_sec_b1}} %{T2}${icon_wifi}%{F- T1} ${sys_arr[16]}"
  115. fi
  116. ### End Wifi Percent ### }}}
  117. ;;
  118. ### End SYS Case ### }}}
  119. ### External IP Case ### {{{
  120. EXT*)
  121. # External IP
  122. ext_ip="%{F${color_sec_b2}}${sep_left}%{F${color_icon} B${color_sec_b2}} %{T2}${icon_ext_ip}%{F- T1} ${line#???}"
  123. ;;
  124. ### End External IP Case ### }}}
  125. ### Vol Case ### {{{
  126. VOL*)
  127. # Volume
  128. vol="%{F${color_sec_b2}}${sep_left}%{F${color_icon} B${color_sec_b2}} %{T2}${icon_vol}%{F- T1} ${line#???}"
  129. ;;
  130. ### End Vol Case ### }}}
  131. ### Offlineimap Case ### {{{
  132. EMA*)
  133. email_count="${line#???}"
  134. if [ "${email_count}" != "0" ]; then
  135. mail_cback=${color_mail}; mail_cicon=${color_back}; mail_cfore=${color_back}
  136. else
  137. mail_cback=${color_sec_b2}; mail_cicon=${color_icon}; mail_cfore=${color_fore}
  138. fi
  139. email="%{F${mail_cback}}${sep_left}%{F${mail_cicon} B${mail_cback}} %{T2}${icon_mail}%{F${mail_cfore} T1} ${email_count}"
  140. ;;
  141. ### End Offlineimp Case ### }}}
  142. #### IRC Case ### {{{
  143. #IRC*)
  144. ## IRC highlight (script irc_warn)
  145. #if [ "${line#???}" != "0" ]; then
  146. #((irc_n_high++)); irc_high="${line#???}";
  147. #irc_cback=${color_chat}; irc_cicon=${color_back}; irc_cfore=${color_back}
  148. #else
  149. #irc_n_high=0; [ -z "${irc_high}" ] && irc_high="none";
  150. #irc_cback=${color_sec_b2}; irc_cicon=${color_icon}; irc_cfore=${color_fore}
  151. #fi
  152. #irc="%{F${irc_cback}}${sep_left}%{F${irc_cicon} B${irc_cback}} %{T2}${icon_chat}%{F${irc_cfore} T1} ${irc_n_high} %{F${irc_cicon}}${sep_l_left} %{T2}${icon_contact}%{F${irc_cfore} T1} ${irc_high}"
  153. #;;
  154. #### End IRC Case ### }}}
  155. ### MPD Case ### {{{
  156. MPD*)
  157. # Music
  158. mpd_arr=(${line#???})
  159. if [ -z "${line#???}" ]; then
  160. song="none";
  161. elif [ "${mpd_arr[0]}" == "error:" ]; then
  162. song="mpd off";
  163. else
  164. song="${line#???}";
  165. fi
  166. mpd="%{F${color_sec_b2}}${sep_left}%{B${color_sec_b2}}%{F${color_sec_b1}}${sep_left}%{F${color_icon} B${color_sec_b1}} %{T2}${icon_music}%{F${color_fore} T1} ${song}"
  167. ;;
  168. ### End MPD Case ### }}}
  169. ### Workspace Case ### {{{
  170. WSP*)
  171. ## I3 Workspaces
  172. wsp="%{F${color_back} B${color_head}} %{T2}${icon_wsp}%{T1}"
  173. set -- ${line#???}
  174. while [ $# -gt 0 ] ; do
  175. case $1 in
  176. FOC*)
  177. wsp="${wsp}%{F${color_head} B${color_wsp}}${sep_right}%{F${color_back} B${color_wsp} T1} ${1#???} %{F${color_wsp} B${color_head}}${sep_right}"
  178. ;;
  179. INA*|URG*|ACT*)
  180. wsp="${wsp}%{F${color_disable} T1} ${1#???} "
  181. ;;
  182. esac
  183. shift
  184. done
  185. ;;
  186. ### End Workspace Case ### }}}
  187. ### Window Case ### {{{
  188. WIN*)
  189. # window title
  190. title=$(xprop -id ${line#???} | awk '/_NET_WM_NAME/{$1=$2="";print}' | cut -d'"' -f2)
  191. title="%{F${color_head} B${color_sec_b2}}${sep_right}%{F${color_head} B${color_sec_b2} T2} ${icon_prog} %{F${color_sec_b2} B-}${sep_right}%{F- B- T1} ${title}"
  192. ;;
  193. ### End Window Case ### }}}
  194. esac
  195. # And finally, output
  196. printf "%s\n" "%{l}${wsp}${title} %{r}${mpd}${stab}${email}${stab}${local_ip}${stab}${wifi}${stab}${ext_ip}${stab}${bat}${stab}${cpu}${stab}${mem}${stab}${diskr}${stab}${temp}${stab}${nets_d}${stab}${nets_u}${stab}${vol}${stab}${date}${stab}${time}"
  197. #printf "%s\n" "%{l}${wsp}${title}"
  198. done