2012-05-15

Using Conky's Template[x]

Conky's neat, but doesn't appear to allow user variables, and the documentation leaves a lot to be desired when it comes to templates.  I found myself desperate for some way to clean up a .conkyrc when using Teo's weather scripts. (They work well, but are insane to edit by hand --- and the gui program (conky companion) appears to require using a mouse to drag. every. single. element. to one's configuration.)

Here's the stock USA Image Accuweather script from TEO:
Screenshot of Teo's Accuweather Conky Script
Stock Accuweather Conky

TEXT
${font Arial:size=12}${color ffffe5e59595}WEATHER ${font}
${hr 2}$color${texeci 500 bash $HOME/Accuweather_Conky_USA_Images/acc_usa_images}
${image $HOME/Accuweather_Conky_USA_Images/cc.png -p 0,70 -s 180x108}
${font Arial:size=10}${execpi 600 sed -n '3p' $HOME/Accuweather_Conky_USA_Images/curr_cond}${font}
${goto 200}${color ffe595}TEMP:$color${alignr}${execpi 600 sed -n '4p' $HOME/Accuweather_Conky_USA_Images/curr_cond}°F (${execpi 600 sed -n '5p' $HOME/Accuweather_Conky_USA_Images/curr_cond}°F)
${goto 200}${color ffe595}WIND:$color${alignr}${execpi 600 sed -n '6p' $HOME/Accuweather_Conky_USA_Images/curr_cond} ${execpi 600 sed -n '14p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}HUM:$color${alignr}${execpi 600 sed -n '7p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}PRESS:$color${alignr}${execpi 600 sed -n '8p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}CLOUD COVER:$color${alignr}${execpi 600 sed -n '9p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}UV INDEX:$color${alignr 10}${execpi 600 sed -n '10p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}DEW POINT:$color${alignr}${execpi 600 sed -n '11p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}AM. OF PRECIP.(1hr):$color${alignr}${execpi 600 sed -n '12p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}VISIB.:$color${alignr}${execpi 600 sed -n '13p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}SUNRISE:$color${alignr}${execpi 600 sed -n '15p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${goto 200}${color ffe595}SUNSET:$color${alignr}${execpi 600 sed -n '16p' $HOME/Accuweather_Conky_USA_Images/curr_cond}
${hr 1}
${goto 50}${font Arial:size=10}${color ffe595}TODAY$color$font${goto 230}${font Arial:size=10}${color ffe595}TONIGHT$color$font${image $HOME/Accuweather_Conky_USA_Images/today.png -p 0,230 -s 120x72}${image $HOME/Accuweather_Conky_USA_Images/tonight.png -p 190,230 -s 120x72}
${font Arial:size=8}${execpi 600 sed -n '1p' $HOME/Accuweather_Conky_USA_Images/messages_curr}${goto 195}${execpi 600 sed -n '3p' $HOME/Accuweather_Conky_USA_Images/messages_curr}$font
${font Arial:size=8}${execpi 600 sed -n '2p' $HOME/Accuweather_Conky_USA_Images/messages_curr}${goto 195}${execpi 600 sed -n '4p' $HOME/Accuweather_Conky_USA_Images/messages_curr}$font
${color ffe595}TEMP: $color${execpi 600 sed -n '28p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 195}${color ffe595}TEMP: $color${execpi 600 sed -n '32p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F
${color ffe595}REAL FEEL: $color${execpi 600 sed -n '29p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 195}${color ffe595}REAL FEEL: $color${execpi 600 sed -n '33p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F
${hr 1}
${font Arial:size=9}${goto 40}${color ffe595}${execpi 600 sed -n '6p' $HOME/Accuweather_Conky_USA_Images/tod_ton}${goto 155}${execpi 600 sed -n '11p' $HOME/Accuweather_Conky_USA_Images/tod_ton}${goto 270}${execpi 600 sed -n '16p' $HOME/Accuweather_Conky_USA_Images/tod_ton}$color${font}
${image $HOME/Accuweather_Conky_USA_Images/7.png -p 0,390 -s 90x54}
${font Arial:size=8}${goto 100}H:${execpi 600 sed -n '9p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 210}H:${execpi 600 sed -n '14p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 330}H:${execpi 600 sed -n '19p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F
${font Arial:size=8}${goto 100}L:${execpi 600 sed -n '10p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 210}L:${execpi 600 sed -n '15p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 330}L:${execpi 600 sed -n '20p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F
${execpi 600 sed -n '1p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 128}${execpi 600 sed -n '3p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 255}${execpi 600 sed -n '5p' $HOME/Accuweather_Conky_USA_Images/messages}
${execpi 600 sed -n '2p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 128}${execpi 600 sed -n '4p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 255}${execpi 600 sed -n '6p' $HOME/Accuweather_Conky_USA_Images/messages}
${image $HOME/Accuweather_Conky_USA_Images/12.png -p 115,390 -s 90x54}${image $HOME/Accuweather_Conky_USA_Images/17.png -p 230,390 -s 90x54}
${font Arial:size=9}${goto 40}${color ffe595}${execpi 600 sed -n '21p' $HOME/Accuweather_Conky_USA_Images/tod_ton}${goto 155}${execpi 600 sed -n '1p' $HOME/Accuweather_Conky_USA_Images/last_days}${goto 270}${execpi 600 sed -n '6p' $HOME/Accuweather_Conky_USA_Images/last_days}$color${font}
${image $HOME/Accuweather_Conky_USA_Images/22.png -p 0,505 -s 90x54}
${font Arial:size=8}${goto 100}H:${execpi 600 sed -n '24p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 210}H:${execpi 600 sed -n '4p' $HOME/Accuweather_Conky_USA_Images/last_days}°F${goto 330}H:${execpi 600 sed -n '9p' $HOME/Accuweather_Conky_USA_Images/last_days}°F
${font Arial:size=8}${goto 100}L:${execpi 600 sed -n '25p' $HOME/Accuweather_Conky_USA_Images/tod_ton}°F${goto 210}L:${execpi 600 sed -n '5p' $HOME/Accuweather_Conky_USA_Images/last_days}°F${goto 330}L:${execpi 600 sed -n '10p' $HOME/Accuweather_Conky_USA_Images/last_days}°F
${execpi 600 sed -n '7p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 128}${execpi 600 sed -n '9p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 255}${execpi 600 sed -n '11p' $HOME/Accuweather_Conky_USA_Images/messages}
${execpi 600 sed -n '8p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 128}${execpi 600 sed -n '10p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 255}${execpi 600 sed -n '12p' $HOME/Accuweather_Conky_USA_Images/messages}
${image $HOME/Accuweather_Conky_USA_Images/N2.png -p 115,505 -s 90x54}${image $HOME/Accuweather_Conky_USA_Images/N7.png -p 230,505 -s 90x54}
${font Arial:size=9}${goto 40}${color ffe595}${execpi 600 sed -n '11p' $HOME/Accuweather_Conky_USA_Images/last_days}${goto 155}${execpi 600 sed -n '16p' $HOME/Accuweather_Conky_USA_Images/last_days}${goto 270}${execpi 600 sed -n '21p' $HOME/Accuweather_Conky_USA_Images/last_days}$color${font}
${image $HOME/Accuweather_Conky_USA_Images/N12.png -p 0,620 -s 90x54}
${font Arial:size=8}${goto 100}H:${execpi 600 sed -n '14p' $HOME/Accuweather_Conky_USA_Images/last_days}°F${goto 210}H:${execpi 600 sed -n '19p' $HOME/Accuweather_Conky_USA_Images/last_days}°F${goto 330}H:${execpi 600 sed -n '24p' $HOME/Accuweather_Conky_USA_Images/last_days}°F
${font Arial:size=8}${goto 100}L:${execpi 600 sed -n '15p' $HOME/Accuweather_Conky_USA_Images/last_days}°F${goto 210}L:${execpi 600 sed -n '20p' $HOME/Accuweather_Conky_USA_Images/last_days}°F${goto 330}L:${execpi 600 sed -n '25p' $HOME/Accuweather_Conky_USA_Images/last_days}°F
${execpi 600 sed -n '13p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 128}${execpi 600 sed -n '15p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 255}${execpi 600 sed -n '17p' $HOME/Accuweather_Conky_USA_Images/messages}
${execpi 600 sed -n '14p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 128}${execpi 600 sed -n '16p' $HOME/Accuweather_Conky_USA_Images/messages}${goto 255}${execpi 600 sed -n '18p' $HOME/Accuweather_Conky_USA_Images/messages}
${image $HOME/Accuweather_Conky_USA_Images/N17.png -p 115,620 -s 90x54}${image $HOME/Accuweather_Conky_USA_Images/N22.png -p 230,620 -s 90x54}

That just shouts "There's got to be a better way.". Here's my use of Conky templates.  I was also playing with fonts, so yes, it's most likely pretty putrid (the screenshot and colors), but the point is more editable (ie, hand tweakable) code which is functionally equivalent to the above:
Screenshot of Greyproc's Accuweather Conky
My Modified Version
#custom colors
color0 85e5ef
color1 42a5AA
color2 75dfd5
# Current Conditions
template0 ${execpi 3600 sed -n '\1' $HOME/.Accuweather_Conky_USA_Images/\2}
# Formatting boilerplate
template1 ${goto 285}${color0}\1:$color${alignr \2}
# Image boilplate (get path here to make cleaner) : assumes all images are pngs
template2 ${image $HOME/.Accuweather_Conky_USA_Images/\1.png -p \2 -s \3}
# Temp/Feels Like
template3 ${color0}${goto 75}\1: $color${template0 \2 \3}°F${goto 265}${color0}\1: $color${template0 \4 \3}°F
# template4 : Day + Day + Day label (ie, "Tomorrow Thursday Friday")
template4 ${font Caston:size=10}${color0}${alignc 100}${template0 \1 \2}${goto 210}${template0 \3 \4}${goto 355}${template0 \5 \4}$color$font
# template5 : High/Low temperature prediction text
template5 ${font Terminus:style=bold:size=10}${goto 125}${color0}\1:\ ${color}${template0 \2 \3}°F${goto 285}${color0}\1:\ $color${template0 \4 \5}°F${goto 420}${color0}\1:\ $color${template0 \6 \5}°F
# template6 : message aka weather description
template6 ${font Caston:size=9}${goto 40}${template0 \1 messages}${goto 195}${template0 \2 messages}${goto 345}${template0 \3 messages}
TEXT
${font Metal Lord:style=bold:size=20}${color1}W E A T H E R  ${font}$color${hr 2}$color${texeci 3600 bash $HOME/.Accuweather_Conky_USA_Images/acc_usa_images}
${template2 cc 50,70 180x108}
${offset 50}${font Terminus:style=bold:size=10}${template0 '3p' curr_cond} 
${template1 TEMP}${template0 '4p' curr_cond}°F (${template0 '5p' curr_cond}°F)
${template1 WIND}${template0 '6p' curr_cond} ${template0 '14p' curr_cond}
${template1 HUMIDITY}${template0 '7p' curr_cond}
${template1 PRESSURE}${template0 '8p' curr_cond}
${template1 CLOUD\ COVER}${template0 '9p' curr_cond}
${template1 UV\ INDEX 10}${template0 '10p' curr_cond}
${template1 DEW\ POINT}${template0 '11p' curr_cond}
${template1 PRECIPITATION}${template0 '12p' curr_cond}
${template1 VISIBILITY}${template0 '13p' curr_cond}
${template1 SUNRISE}${template0 '15p' curr_cond}
${template1 SUNSET}${template0 '16p' curr_cond}
${hr 1}
${goto 85}${color1}${font Venus Rising:style=bold:size=10}TODAY${goto 265}TONIGHT$color$font
${template2 today 40,250 120x72}${template2 tonight 225,250 120x72}
${alignc 95}${voffset 60}${template0 '1p' messages_curr}${goto 270}${template0 '3p' messages_curr}
${template0 '2p' messages_curr}${goto 265}${template0 '4p' messages_curr}
${template3 TEMP '28p' tod_ton '32p'}
${template3 FEELS\ LIKE '29p' tod_ton '33p'}
${hr 1}
# row 1
${voffset 5}
${template4 '6p' tod_ton '11p' tod_ton '16p'}
${template2 7 15,435 90x54} ${template2 12 175,435 90x54} ${template2 17 310,435 90x54}
${template5 H '9p' tod_ton '14p' tod_ton '19p'}
${template5 L '10p' tod_ton '15p' tod_ton '20p'}
${voffset 15}
${template6 '1p' '3p' '5p'}
${template6 '2p' '4p' '6p'}
# row 2
${voffset 5}
${template4 '21p' tod_ton '1p' last_days '6p'}
${template2 22 15,565 90x54}${template2 N2 175,565 90x54}${template2 N7 310,565 90x54}
${template5 H '24p' tod_ton '4p' last_days '9p'}
${template5 L '25p' tod_ton '5p' last_days '10p'}
${voffset 15}
${template6 '7p' '9p' '11p'}
${template6 '8p' '10p' '12p'}
# row 3
${voffset 5}
${template4 '11p' last_days '16p' last_days '21p'}
${template2 N12 15,700 90x54}${template2 N17 175,700 90x54}${template2 N22 310,700 90x54}
${template5 H '14p' last_days '19p' last_days '24p'}
${template5 L '15p' last_days '20p' last_days '25p'}
${voffset 15}
${template6 '13p' '15p' '17p'}

While I'm hardly an expert with conky, or weather data scraping (I'd likely not be using someone else's script, if I were), I'd say the template way works quite a bit better for hand editing; after converting, it became trivial for me to quickly change the positioning of elements.

Reference:
Teo's Conky Weather Scripts
Conky - A light-weight system monitor

No comments: