conkyrings.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. --[[
  2. Ring Meters by londonali1010 (2009)
  3. This script draws percentage meters as rings. It is fully customisable; all options are described in the script.
  4. IMPORTANT: if you are using the 'cpu' function, it will cause a segmentation fault if it tries to draw a ring straight away. The if statement on line 145 uses a delay to make sure that this doesn't happen. It calculates the length of the delay by the number of updates since Conky started. Generally, a value of 5s is long enough, so if you update Conky every 1s, use update_num > 5 in that if statement (the default). If you only update Conky every 2s, you should change it to update_num > 3; conversely if you update Conky every 0.5s, you should use update_num > 10. ALSO, if you change your Conky, is it best to use "killall conky; conky" to update it, otherwise the update_num will not be reset and you will get an error.
  5. Changelog:
  6. + v1.2.1 -- Fixed minor bug that caused script to crash if conky_parse() returns a nil value (20.10.2009)
  7. + v1.2 -- Added option for the ending angle of the rings (07.10.2009)
  8. + v1.1 -- Added options for the starting angle of the rings, and added the "max" variable, to allow for variables that output a numerical value rather than a percentage (29.09.2009)
  9. + v1.0 -- Original release (28.09.2009)
  10. ]]
  11. conky_background_color = 0x151515
  12. conky_background_alpha = 0
  13. ring_background_color = 0x424242
  14. ring_background_alpha = 0.9
  15. ring_foreground_color = 0xefefef
  16. ring_foreground_color2 = 0x1994d1
  17. ring_foreground_color3 = 0x0FC4E3
  18. ring_foreground_alpha = 1
  19. settings_table = {
  20. {
  21. name='time',
  22. arg='%S',
  23. max=60,
  24. bg_colour=ring_background_color,
  25. bg_alpha=ring_background_alpha,
  26. fg_colour=ring_foreground_color2,
  27. fg_alpha=ring_foreground_alpha,
  28. x=100, y=100,
  29. radius=40,
  30. thickness=12,
  31. start_angle=0,
  32. end_angle=360,
  33. fill_l_to_r=false
  34. },
  35. {
  36. name='time',
  37. arg='%I.%M',
  38. max=12,
  39. bg_colour=ring_background_color,
  40. bg_alpha=ring_background_alpha,
  41. fg_colour=ring_foreground_color,
  42. fg_alpha=ring_foreground_alpha,
  43. x=100, y=100,
  44. radius=50,
  45. thickness=5,
  46. start_angle=-140,
  47. end_angle=-30,
  48. fill_l_to_r=false
  49. },
  50. {
  51. name='time',
  52. arg='%M.%S',
  53. max=60,
  54. bg_colour=ring_background_color,
  55. bg_alpha=ring_background_alpha,
  56. fg_colour=ring_foreground_color2,
  57. fg_alpha=ring_foreground_alpha,
  58. x=100, y=100,
  59. radius=50,
  60. thickness=5,
  61. start_angle=30,
  62. end_angle=140,
  63. fill_l_to_r=false
  64. },
  65. {
  66. name='cpu',
  67. arg='cpu0',
  68. max=100,
  69. bg_colour=ring_background_color,
  70. bg_alpha=ring_background_alpha,
  71. fg_colour=ring_foreground_color,
  72. fg_alpha=ring_foreground_alpha,
  73. x=50, y=225,
  74. radius=26,
  75. thickness=5,
  76. start_angle=-180,
  77. end_angle=90,
  78. fill_l_to_r=false
  79. },
  80. {
  81. name='cpu',
  82. arg='cpu1',
  83. max=100,
  84. bg_colour=ring_background_color,
  85. bg_alpha=ring_background_alpha,
  86. fg_colour=ring_foreground_color2,
  87. fg_alpha=ring_foreground_alpha,
  88. x=50, y=225,
  89. radius=20,
  90. thickness=5,
  91. start_angle=-180,
  92. end_angle=90,
  93. fill_l_to_r=false
  94. },
  95. {
  96. name='memperc',
  97. arg='',
  98. max=100,
  99. bg_colour=ring_background_color,
  100. bg_alpha=ring_background_alpha,
  101. fg_colour=ring_foreground_color,
  102. fg_alpha=ring_foreground_alpha,
  103. x=150, y=225,
  104. radius=26,
  105. thickness=5,
  106. start_angle=-90,
  107. end_angle=180,
  108. fill_l_to_r=true
  109. },
  110. {
  111. name='swapperc',
  112. arg='',
  113. max=100,
  114. bg_colour=ring_background_color,
  115. bg_alpha=ring_background_alpha,
  116. fg_colour=ring_foreground_color2,
  117. fg_alpha=ring_foreground_alpha,
  118. x=150, y=225,
  119. radius=20,
  120. thickness=5,
  121. start_angle=-90,
  122. end_angle=180,
  123. fill_l_to_r=true
  124. },
  125. --[[
  126. {
  127. name='mpd_vol',
  128. arg='',
  129. max=100,
  130. bg_colour=ring_background_color,
  131. bg_alpha=ring_background_alpha,
  132. fg_colour=ring_foreground_color3,
  133. fg_alpha=ring_foreground_alpha,
  134. x=750, y=100,
  135. radius=20,
  136. thickness=5,
  137. start_angle=-90,
  138. end_angle=180,
  139. fill_l_to_r=false
  140. },
  141. {
  142. name='if_match "${mpd_status}"=="Playing"}${mpd_percent}${else}${if_match "${mpd_status}"=="Paused"}${mpd_percent}${else}0${endif}${endif',
  143. --name='mpd_percent',
  144. arg='',
  145. max=100,
  146. bg_colour=ring_background_color,
  147. bg_alpha=ring_background_alpha,
  148. fg_colour=ring_foreground_color2,
  149. fg_alpha=ring_foreground_alpha,
  150. x=750, y=100,
  151. radius=26,
  152. thickness=5,
  153. start_angle=-90,
  154. end_angle=180,
  155. fill_l_to_r=false
  156. },
  157. --]]
  158. {
  159. name='upspeedf',
  160. arg='wlp3s0',
  161. max=750,
  162. bg_colour=ring_background_color,
  163. bg_alpha=ring_background_alpha,
  164. fg_colour=ring_foreground_color2,
  165. fg_alpha=ring_foreground_alpha,
  166. x=50, y=320,
  167. radius=20,
  168. thickness=5,
  169. start_angle=-270,
  170. end_angle=0,
  171. fill_l_to_r=true
  172. },
  173. {
  174. name='downspeedf',
  175. arg='wlp3s0',
  176. max=4000,
  177. bg_colour=ring_background_color,
  178. bg_alpha=ring_background_alpha,
  179. fg_colour=ring_foreground_color,
  180. fg_alpha=ring_foreground_alpha,
  181. x=50, y=320,
  182. radius=26,
  183. thickness=5,
  184. start_angle=-270,
  185. end_angle=0,
  186. fill_l_to_r=true
  187. },
  188. {
  189. name='fs_used_perc',
  190. arg='/home',
  191. max=100,
  192. bg_colour=ring_background_color,
  193. bg_alpha=ring_background_alpha,
  194. fg_colour=ring_foreground_color,
  195. fg_alpha=ring_foreground_alpha,
  196. x=150, y=320,
  197. radius=26,
  198. thickness=5,
  199. start_angle=0,
  200. end_angle=270,
  201. fill_l_to_r=false
  202. },
  203. {
  204. name='fs_used_perc',
  205. arg='/',
  206. max=100,
  207. bg_colour=ring_background_color,
  208. bg_alpha=ring_background_alpha,
  209. fg_colour=ring_foreground_color2,
  210. fg_alpha=ring_foreground_alpha,
  211. x=150, y=320,
  212. radius=20,
  213. thickness=5,
  214. start_angle=0,
  215. end_angle=270,
  216. fill_l_to_r=false
  217. },
  218. }
  219. require 'cairo'
  220. function rgb_to_r_g_b(colour,alpha)
  221. return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  222. end
  223. function draw_ring(cr,t,pt)
  224. local w,h=conky_window.width,conky_window.height
  225. local xc,yc,ring_r,ring_w,sa,ea,lr=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle'],pt['fill_l_to_r']
  226. local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  227. local angle_0=sa*(2*math.pi/360)-math.pi/2
  228. local angle_f=ea*(2*math.pi/360)-math.pi/2
  229. local t_arc=t*(angle_f-angle_0)
  230. if math.abs(t_arc) > t*math.abs(angle_f-angle_0) then
  231. t_arc = t*angle_f
  232. end
  233. if lr then
  234. -- Draw background ring
  235. cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  236. cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  237. cairo_set_line_width(cr,ring_w)
  238. cairo_stroke(cr)
  239. -- Draw indicator ring
  240. cairo_arc(cr,xc,yc,ring_r,angle_f-t_arc,angle_f)
  241. cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  242. cairo_stroke(cr)
  243. else
  244. -- Draw background ring
  245. cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  246. cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  247. cairo_set_line_width(cr,ring_w)
  248. cairo_stroke(cr)
  249. -- Draw indicator ring
  250. cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
  251. cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  252. cairo_stroke(cr)
  253. end
  254. end
  255. function conky_ring_stats()
  256. local function setup_rings(cr,pt)
  257. local str=''
  258. local value=0
  259. str=string.format('${%s %s}',pt['name'],pt['arg'])
  260. str=conky_parse(str)
  261. value=tonumber(str)
  262. if value == nil then value = 0 end
  263. pct=value/pt['max']
  264. draw_ring(cr,pct,pt)
  265. end
  266. if conky_window==nil then return end
  267. local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  268. local cr=cairo_create(cs)
  269. local updates=conky_parse('${updates}')
  270. update_num=tonumber(updates)
  271. if update_num>5 then
  272. for i in pairs(settings_table) do
  273. setup_rings(cr,settings_table[i])
  274. end
  275. end
  276. end