i3_lemonbar_parser.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #!/bin/bash
  2. ## Input parser for i3 bar
  3. ##
  4. ## Based on Electro7's work
  5. ## Highly Modded by demure
  6. ## config
  7. . $(dirname $0)/i3_lemonbar_config
  8. ## min init
  9. irc_n_high=0
  10. 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}"
  11. ## parser
  12. while read -r line ; do
  13. case ${line} in
  14. ### SYS Case ### {{{
  15. SYS*)
  16. ## conky=, 0-1=up/down wlan, 2-3=up/down eth, 5=cpu
  17. sys_arr=(${line#???})
  18. ### CPU ### {{{
  19. if [ ${sys_arr[4]} -gt ${cpu_alert} ]; then
  20. cpu_set_back=${color_cpu}; cpu_cicon=${color_back}; cpu_cfore=${color_back};
  21. else
  22. cpu_set_back=${cpu_cback}; cpu_cicon=${color_icon}; cpu_cfore=${color_fore};
  23. fi
  24. cpu="%{F${cpu_set_back}}${sep_left}%{F${cpu_cicon} B${cpu_set_back}} %{T2}${icon_cpu}%{F${cpu_cfore} T1} ${sys_arr[4]}%"
  25. ### End CPU ### }}}
  26. ### Net Speed ### {{{
  27. ## made to replace WLAN and Eth sections
  28. ## Will prefer eth0 if up
  29. if [ "${sys_arr[2]}" == "down" ]; then
  30. if [ "${sys_arr[0]}" == "down" ];then
  31. nets_dv="×"; nets_uv="×";
  32. nets_cback=${color_sec_b1}; nets_cicon=${color_disable}; nets_cfore=${color_disable};
  33. else
  34. nets_dv=${sys_arr[0]}K; nets_uv=${sys_arr[1]}K;
  35. if [ ${nets_dv:0:-3} -gt ${net_alert} ] || [ ${nets_uv:0:-3} -gt ${net_alert} ]; then
  36. nets_cback=${color_net}; nets_cicon=${color_back}; nets_cfore=${color_back};
  37. else
  38. nets_cback=${color_sec_b1}; nets_cicon=${color_icon}; nets_cfore=${color_fore};
  39. fi
  40. fi
  41. else
  42. nets_dv=${sys_arr[2]}K; nets_uv=${sys_arr[3]}K;
  43. if [ ${nets_dv:0:-3} -gt ${net_alert} ] || [ ${nets_uv:0:-3} -gt ${net_alert} ]; then
  44. nets_cback=${color_net}; nets_cicon=${color_back}; nets_cfore=${color_back};
  45. else
  46. nets_cback=${color_sec_b1}; nets_cicon=${color_icon}; nets_cfore=${color_fore};
  47. fi
  48. fi
  49. nets_d="%{F${nets_cback}}${sep_left}%{F${nets_cicon} B${nets_cback}} %{T2}${icon_dl}%{F${nets_cfore} T1} ${nets_dv}"
  50. nets_u="%{F${nets_cicon}}${sep_l_left} %{T2}${icon_ul}%{F${nets_cfore} T1} ${nets_uv}"
  51. ### End Net Speed ### }}}
  52. ;;
  53. ### End SYS Case ### }}}
  54. ### Time and Date Case ### {{{
  55. TIM*)
  56. ## Display Ram use
  57. ## Seems to need a hand to be treated as an array...
  58. read -r -a time_arr <<< "${line#???}"
  59. #time_arr="${line#???}"
  60. ## Date
  61. if [ ${res_w} -gt 1024 ]; then
  62. date="${time_arr[0]} ${time_arr[1]} ${time_arr[2]}"
  63. else
  64. date="${time_arr[1]} ${time_arr[2]}"
  65. fi
  66. date="%{F${date_cback}}${sep_left}%{F${color_icon} B${date_cback}} %{T2}${icon_time}%{F- T1} ${date}"
  67. ## Time
  68. time="%{F${color_head}}${sep_left}%{F${color_back} B${color_head}} ${time_arr[3]} %{F- B-}"
  69. ;;
  70. ### End MEM Case ### }}}
  71. ### Disk Usage Case ### {{{
  72. DIC*)
  73. ## Display Disk use
  74. disk_arr="${line#???}"
  75. diskr="%{F${disk_cback}}${sep_left}%{F${color_icon} B${disk_cback}} %{T2}${icon_disk}%{F- T1} ${disk_arr}"
  76. ;;
  77. ### End Disk Usage Case ### }}}
  78. ### MEM Usage Case ### {{{
  79. MEM*)
  80. ## Display Ram use
  81. mem_arr="${line#???}"
  82. mem="%{F${cpu_cicon}}${sep_l_left} %{T2}${icon_mem}%{F${cpu_cfore} T1} ${mem_arr}"
  83. ;;
  84. ### End MEM Usage Case ### }}}
  85. ### Network Case ### {{{
  86. NET*)
  87. ## Receives either eth or wireless ip, and interface, and signal.
  88. ## Unfortunately, this will likely default to the last valid entry.
  89. net_arr_ip=$(echo ${line#???} | cut -f1 -d\ )
  90. net_arr_inter=$(echo ${line#???} | cut -f2 -d\ )
  91. net_arr_ipv6=$(echo ${line#???} | cut -f3 -d\ )
  92. net_arr_signal=$(echo ${line#???} | cut -f4 -d\ )
  93. net_arr_vpn=$(echo ${line#???} | cut -f5 -d\ )
  94. ## Local IP
  95. if [ "${net_arr_ip}" != "none" ]; then
  96. if [[ ${net_arr_inter} =~ eth ]]; then
  97. net_icon="${icon_local_eth}";
  98. else
  99. net_icon="${icon_local_wifi}";
  100. fi
  101. else
  102. net_arr_ip="No IP"; net_icon="${icon_local_out}";
  103. fi
  104. #local_ip_cback=${color_sec_b1}
  105. local_ip="%{F${local_ip_cback}}${sep_left}%{F${color_icon} B${local_ip_cback}} %{T2}${net_icon}%{F- T1} ${net_arr_ip}"
  106. ## Wifi Signal Strength
  107. if [ "${net_arr_signal}" != "none" ]; then
  108. wifi="%{F${color_icon}}${sep_l_left}%{F${color_icon} B${local_ip_cback}} %{T2}${icon_wifi}%{F- T1} ${net_arr_signal}%"
  109. else
  110. wifi=""
  111. fi
  112. ## External IP clean up
  113. ## This stops an external IP from displaying, if on local IP.
  114. ## This is in this section, as it run much much more often.
  115. if [ "${net_arr_ip}" = "No IP" ]; then
  116. ext_ip="%{F${ext_ip_cback}}${sep_left}%{F${color_icon} B${ext_it_cback}} %{T2}${icon_ext_ip}%{F- T1} No IP"
  117. fi
  118. ## External IP icon to VPN icon
  119. ## This will swap the normal icon to show that a VPN is up.
  120. ## This is in this section, as it run much much more often.
  121. if [ "${net_arr_vpn}" = "VPN" ]; then
  122. vpn="%{F${color_icon}}${sep_l_left}%{B${ext_ip_cback}}%{F${color_icon} B${ext_ip_cback}} %{T2}${icon_vpn}"
  123. else
  124. vpn=""
  125. fi
  126. ;;
  127. ### End Network Case ### }}}
  128. ### External IP Case ### {{{
  129. EXT*)
  130. ## External IP
  131. ext_arr="${line#???}"
  132. if [ "${net_arr_ip}" = "${ext_arr}" ]; then
  133. ext_ip_select="Same"
  134. else
  135. ext_ip_select="${ext_arr}"
  136. fi
  137. ext_ip="%{F${ext_ip_cback}}${sep_left}%{F${color_icon} B${ext_ip_cback}} %{T2}${icon_ext_ip}%{F- T1} ${ext_ip_select}"
  138. ;;
  139. ### End External IP Case ### }}}
  140. ### Volume Case ### {{{
  141. VOL*)
  142. ## Volume
  143. vol_arr="${line#???}"
  144. vol="%{F${vol_cback}}${sep_left}%{F${color_icon} B${vol_cback}} %{T2}${icon_vol}%{F- T1} ${vol_arr}"
  145. ;;
  146. ### End Volume Case ### }}}
  147. ### Brightness Case ### {{{
  148. BRI*)
  149. ## Brightness
  150. bright_arr="${line#???}"
  151. ## Don't show brightness if there is no battery.
  152. ## Most desktops don't software adjust brightness.
  153. ## I suppose there is a small use case of a laptop with no battery...
  154. if [ "${bright_arr}" != "none" ]; then
  155. bri="%{F${color_icon}}${sep_l_left}%{F${color_icon} B${color_sec_b2}} %{T2}${icon_bri}%{F- T1} ${bright_arr}%"
  156. else
  157. bri=""
  158. fi
  159. ;;
  160. ### End Brightness Case ### }}}
  161. ### Temperature Case ### {{{
  162. TMP*)
  163. ## Temperature
  164. temp_arr_val=$(echo ${line#???} | cut -f1 -d\ )
  165. temp_arr_unit=$(echo ${line#???} | cut -f2 -d\ )
  166. if [ "${temp_arr_val}" != "none" ]; then
  167. ## Select alert for measurement system
  168. if [ ${temp_arr_unit} == "C" ]; then
  169. temp_check=$(awk -v x=${temp_arr_val} -v y=${temp_alert_c} 'BEGIN {if (x<y){print 0} else {print 1}}')
  170. else
  171. temp_check=$(awk -v x=${temp_arr_val} -v y=${temp_alert_f} 'BEGIN {if (x<y){print 0} else {print 1}}')
  172. fi
  173. ## Set alert color
  174. if [ ${temp_check} -eq 1 ]; then
  175. temp_set_back=${color_temp}; temp_cicon=${color_back}; temp_cfore=${color_back};
  176. else
  177. temp_set_back=${temp_cback}; temp_cicon=${color_icon}; temp_cfore=${color_fore};
  178. fi
  179. temp="%{F${temp_set_back}}${sep_left}%{F${temp_cicon} B${temp_set_back}} %{T2}${icon_temp}%{F${temp_cfore} T1} ${temp_arr_val}${temp_arr_unit}"
  180. else
  181. ## Set if temp fifo missing
  182. temp="%{F${temp_cback}}${sep_left}%{F${color_disable} B${temp_cback}} %{T2}${icon_temp}%{F${color_disable} T1} ×"
  183. fi
  184. ;;
  185. ### End Temperature Case ### }}}
  186. ### Offlineimap Case ### {{{
  187. EMA*)
  188. email_arr="${line#???}"
  189. if [ "${email_arr}" != "0" ]; then
  190. mail_set_back=${color_mail}; mail_cicon=${color_sec_b2}; mail_cfore=${color_sec_b2}; mail_icon=${icon_mail}; mail_num=${email_arr}; mail_post_sep=${sep_left}
  191. else
  192. mail_set_back=${mail_cback}; mail_cicon=${color_icon}; mail_cfore=${color_fore}; mail_icon=${icon_mail_read}; mail_num=""; mail_post_sep=${sep_l_left}
  193. fi
  194. email="%{F${mail_set_back}}${sep_left}%{F${mail_cicon} B${mail_set_back}} %{T2}${mail_icon}%{F${mail_cfore} T1} ${mail_num}"
  195. ;;
  196. ### End Offlineimp Case ### }}}
  197. ### GPG Case ### {{{
  198. GPG*)
  199. gpg_arr=(${line#???})
  200. if [ "${gpg_arr}" != "none" ]; then
  201. if [ "${gpg_arr}" = "1" ]; then
  202. lock="${icon_gpg_unlocked}"
  203. else
  204. lock="${icon_gpg_locked}"
  205. fi
  206. gpg="%{F${gpg_cback}}${sep_left}%{F${color_icon} B${gpg_cback}} %{T2}${icon_gpg}%{F${color_fore} T1} ${lock}"
  207. fi
  208. ;;
  209. ### End GPG Case ### }}}
  210. ### RSS Case ### {{{
  211. RSS*)
  212. rss_arr=(${line#???})
  213. if [ -n "${rss_arr}" ]; then
  214. rss="%{F${rss_cback}}${sep_left}%{F${color_icon} B${rss_cback}} %{T2}${icon_rss}%{F${color_fore} T1} ${rss_arr}"
  215. fi
  216. ;;
  217. ### End RSS Case ### }}}
  218. ### Thinkpad Multi Battery ### {{{
  219. ## Icon 0 1 2 3 4
  220. ## Bat >= NA 11 37 63 90
  221. ## Range 0-10 11-36 37-62 63-89 90-100
  222. TMB*)
  223. ## Now that I have >12 hours of battery, I don't feel the need for as drastic color thresholds.
  224. ## I will have colors only at much lower percentages.
  225. tmb_arr_perc=$(echo ${line#???} | cut -f1 -d\ )
  226. tmb_arr_stat=$(echo ${line#???} | cut -f2 -d\ )
  227. tmb_arr_time=$(echo ${line#???} | cut -f3 -d\ )
  228. ## This means it will not show up on desktop computers
  229. if [ "${tmb_arr_perc}" != "none" ]; then
  230. ## Set icon only
  231. ## This is 'hard' coded, instead of in the conf, due to icon font.
  232. ## It will take user intervention if they have a different number of icons
  233. if [ ${tmb_arr_perc} -ge 90 ]; then
  234. bat_icon=${icon_bat4};
  235. elif [ ${tmb_arr_perc} -ge 63 ]; then
  236. bat_icon=${icon_bat3};
  237. elif [ ${tmb_arr_perc} -ge 37 ]; then
  238. bat_icon=${icon_bat2};
  239. elif [ ${tmb_arr_perc} -ge 11 ]; then
  240. bat_icon=${icon_bat1};
  241. else
  242. bat_icon=${icon_bat0};
  243. fi
  244. ## Set Colors
  245. if [ ${tmb_arr_perc} -le ${bat_alert_low} ]; then
  246. bat_cicon=${color_icon}; bat_cfore=${color_fore}; bat_set_back=${color_bat_low};
  247. elif [ ${tmb_arr_perc} -le ${bat_alert_mid} ]; then
  248. bat_cicon=${color_icon}; bat_cfore=${color_fore}; bat_set_back=${color_bat_mid};
  249. elif [ ${tmb_arr_perc} -le ${bat_alert_high} ]; then
  250. bat_cicon=${color_bat_high}; bat_cfore=${color_fore}; bat_set_back=${bat_cback};
  251. else
  252. bat_cicon=${color_icon}; bat_cfore=${color_fore}; bat_set_back=${bat_cback};
  253. fi
  254. ## Set charging icon
  255. if [ "${tmb_arr_stat}" == "C" ]; then
  256. bat_icon=${icon_bat_plug}; bat_cicon=${color_bat_plug};
  257. fi
  258. bat="%{F${bat_set_back}}${sep_left}%{F${bat_cicon} B${bat_set_back}} %{T2}${bat_icon}%{F- T1} ${tmb_arr_perc}%"
  259. if [ "${tmb_arr_time}" != "none" ]; then
  260. bat_time="%{F${color_icon}}${sep_l_left}%{F${color_icon} B${bat_cback}} %{T2}${icon_bat_time}%{F- T1} ${tmb_arr_time}"
  261. else
  262. bat_time=""
  263. fi
  264. else
  265. ## If a desktop, show a plug icon. This stops the ugly segment merge that that would happen.
  266. bat="%{F${bat_cback}}${sep_left}%{F${color_icon} B${bat_cback}} %{T2}${icon_bat_plug}%{F- T1}"
  267. fi
  268. ;;
  269. ### End Thinkpad Multi Battery ### }}}
  270. #### IRC Case ### {{{
  271. #IRC*)
  272. ## IRC highlight (script irc_warn)
  273. #if [ "${line#???}" != "0" ]; then
  274. #((irc_n_high++)); irc_high="${line#???}";
  275. #irc_cback=${color_chat}; irc_cicon=${color_back}; irc_cfore=${color_back}
  276. #else
  277. #irc_n_high=0; [ -z "${irc_high}" ] && irc_high="none";
  278. #irc_cback=${color_sec_b2}; irc_cicon=${color_icon}; irc_cfore=${color_fore}
  279. #fi
  280. #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}"
  281. #;;
  282. #### End IRC Case ### }}}
  283. ### Mutli Music Player Display ### {{{
  284. MMP*)
  285. ## Music
  286. mmpd_arr="${line#???}"
  287. if [ -z "${mmpd_arr}" ] || [ "${mmpd_arr}" == "none" ]; then
  288. ## This can deal with odd issues?
  289. mmpd_song="×";
  290. else
  291. ## $music_limit will help stop the music from taking over the workspace display on the left.
  292. ## Note: you should read http://tldp.org/LDP/abs/html/parameter-substitution.html
  293. mmpd_song="${mmpd_arr:0:${music_limit}}";
  294. fi
  295. mmpd="%{F${mmpd_alt_cback}}${sep_left}%{B${mmpd_alt_cback}}%{F${mmpd_cback}}${sep_left}%{F${color_icon} B${mmpd_cback}} %{T2}${icon_music}%{F${color_fore} T1} ${mmpd_song}"
  296. ;;
  297. ### End Multi Music Player Display ### }}}
  298. ### Workspace Case ### {{{
  299. WSP*)
  300. ## I3 Workspaces
  301. wsp="%{F${color_back} B${color_head}} %{T2}${icon_wsp}%{T1}"
  302. set -- ${line#???}
  303. while [ $# -gt 0 ] ; do
  304. case $1 in
  305. FOC*)
  306. 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}"
  307. ;;
  308. INA*|URG*|ACT*)
  309. wsp="${wsp}%{F${color_disable} T1} ${1#???} "
  310. ;;
  311. esac
  312. shift
  313. done
  314. ;;
  315. ### End Workspace Case ### }}}
  316. ### Window Case ### {{{
  317. WIN*)
  318. ## window title
  319. title=$(xprop -id ${line#???} | awk '/_NET_WM_NAME/{$1=$2="";print}' | cut -d'"' -f2)
  320. 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}"
  321. ;;
  322. ### End Window Case ### }}}
  323. esac
  324. ### Network Display Toggle ### {{{
  325. # This is toggled by, preferably, a binding in your ~/.i3/config
  326. ## You can find the awk command in my config, or this bar's readme
  327. ext_toggle="$(cat /tmp/i3_lemonbar_ip_${USER} 2>/dev/null)"
  328. if [ "${ext_toggle}" = 2 ]; then
  329. local_ip="%{F${local_ip_cback}}${sep_left}%{F${color_icon} B${local_ip_cback}} %{T2}${net_icon}%{F- T1} ${scrub_ip}"
  330. filler="%{F${ext_ip_cback}}${sep_left}%{F${color_icon} B${ext_ip_cback}} %{T2}${icon_ext_ip}%{F- T1}"
  331. mast_net="${local_ip}${stab}${wifi}${stab}${filler}${stab}${vpn}${stab}"
  332. elif [ "${ext_toggle}" = 1 ]; then
  333. if [ "${net_arr_ipv6}" = "none" ]; then
  334. net_arr_ipv6="No IPv6"
  335. fi
  336. filler="%{F${local_ip_cback}}${sep_left}%{F${color_icon} B${local_ip_cback}} %{T2}${net_icon}%{F- T1}"
  337. ipv6_ip="%{F${ext_ip_cback}}${sep_left}%{F${color_icon} B${ext_ip_cback}} %{T2}${icon_ext_ip}%{F- T1} ${net_arr_ipv6}"
  338. mast_net="${filler}${stab}${ipv6_ip}"
  339. else
  340. mast_net="${local_ip}${stab}${wifi}${stab}${ext_ip}${stab}${vpn}${stab}"
  341. fi
  342. ### End Network Display Toggle ### }}}
  343. ## And finally, output
  344. ## Broken into multiple lines to make more readable.
  345. ## You can rearrange them or remove them as desired.
  346. ## Notice that all but the first and last have a ${stab} for spacing.
  347. ## NOTE: At this moment, there is not a dead simple way to adjust a
  348. # segment between the three background colors, you have to manually
  349. # find the CASE and edit.
  350. printf "%s\n" "%{l}${wsp}${title} %{r}\
  351. ${mmpd}${stab}\
  352. ${rss}${stab}\
  353. ${email}${stab}\
  354. ${gpg}${stab}\
  355. ${mast_net}${stab}\
  356. ${bat}${stab}\
  357. ${bat_time}${stab}\
  358. ${cpu}${stab}\
  359. ${mem}${stab}\
  360. ${diskr}${stab}\
  361. ${temp}${stab}\
  362. ${nets_d}${stab}\
  363. ${nets_u}${stab}\
  364. ${vol}${stab}\
  365. ${bri}${stab}\
  366. ${date}${stab}\
  367. ${time}"
  368. done