i3_lemonbar_parser.sh 18 KB

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