Advertisement
Guest User

conky.lua

a guest
Feb 4th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. --[[
  2. Ring Meters by londonali1010 (2009)
  3.  
  4. This script draws percentage meters as rings. It is fully customisable; all options are described in the script.
  5.  
  6. 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.
  7.  
  8. To call this script in Conky, use the following (assuming that you save this script to ~/scripts/rings.lua):
  9. lua_load ~/scripts/rings-v1.2.1.lua
  10. lua_draw_hook_pre ring_stats
  11.  
  12. Changelog:
  13. + v1.2.1 -- Fixed minor bug that caused script to crash if conky_parse() returns a nil value (20.10.2009)
  14. + v1.2 -- Added option for the ending angle of the rings (07.10.2009)
  15. + 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)
  16. + v1.0 -- Original release (28.09.2009)
  17. ]]
  18.  
  19. conky_background_color = 0x151515
  20. conky_background_alpha = 0
  21.  
  22. ring_background_color = 0x424242
  23. ring_background_alpha = 0.9
  24. ring_foreground_color = 0x86c113
  25. ring_foreground_color2 = 0x1994d1
  26. ring_foreground_color3 = 0xaa0000
  27. ring_foreground_alpha = 1
  28.  
  29. settings_table = {
  30. {
  31. name='time',
  32. arg='%S',
  33. max=60,
  34. bg_colour=ring_background_color,
  35. bg_alpha=ring_background_alpha,
  36. fg_colour=ring_foreground_color,
  37. fg_alpha=ring_foreground_alpha,
  38. x=191, y=145,
  39. radius=40,
  40. thickness=12,
  41. start_angle=0,
  42. end_angle=360
  43. },
  44. {
  45. name='time',
  46. arg='%I.%M',
  47. max=12,
  48. bg_colour=ring_background_color,
  49. bg_alpha=ring_background_alpha,
  50. fg_colour=ring_foreground_color2,
  51. fg_alpha=ring_foreground_alpha,
  52. x=191, y=145,
  53. radius=50,
  54. thickness=5,
  55. start_angle=-140,
  56. end_angle=-30
  57. },
  58. {
  59. name='time',
  60. arg='%M.%S',
  61. max=60,
  62. bg_colour=ring_background_color,
  63. bg_alpha=ring_background_alpha,
  64. fg_colour=ring_foreground_color,
  65. fg_alpha=ring_foreground_alpha,
  66. x=191, y=145,
  67. radius=50,
  68. thickness=5,
  69. start_angle=30,
  70. end_angle=140
  71. },
  72. {
  73. name='cpu',
  74. arg='cpu0',
  75. max=100,
  76. bg_colour=ring_background_color,
  77. bg_alpha=ring_background_alpha,
  78. fg_colour=ring_foreground_color,
  79. fg_alpha=ring_foreground_alpha,
  80. x=140, y=300,
  81. radius=26,
  82. thickness=5,
  83. start_angle=-90,
  84. end_angle=180
  85. },
  86. {
  87. name='memperc',
  88. arg='',
  89. max=100,
  90. bg_colour=ring_background_color,
  91. bg_alpha=ring_background_alpha,
  92. fg_colour=ring_foreground_color,
  93. fg_alpha=ring_foreground_alpha,
  94. x=170, y=350,
  95. radius=26,
  96. thickness=5,
  97. start_angle=-90,
  98. end_angle=180
  99. },
  100. {
  101. name='swapperc',
  102. arg='',
  103. max=100,
  104. bg_colour=ring_background_color,
  105. bg_alpha=ring_background_alpha,
  106. fg_colour=ring_foreground_color2,
  107. fg_alpha=ring_foreground_alpha,
  108. x=170, y=350,
  109. radius=20,
  110. thickness=5,
  111. start_angle=-90,
  112. end_angle=180
  113. },
  114. {
  115. name='fs_used_perc',
  116. arg='/',
  117. max=100,
  118. bg_colour=ring_background_color,
  119. bg_alpha=ring_background_alpha,
  120. fg_colour=ring_foreground_color2,
  121. fg_alpha=ring_foreground_alpha,
  122. x=260, y=503,
  123. radius=20,
  124. thickness=5,
  125. start_angle=-90,
  126. end_angle=180
  127. },
  128. {
  129. name='fs_used_perc',
  130. arg='/media/Bagy/',
  131. max=100,
  132. bg_colour=ring_background_color,
  133. bg_alpha=ring_background_alpha,
  134. fg_colour=ring_foreground_color,
  135. fg_alpha=ring_foreground_alpha,
  136. x=260, y=503,
  137. radius=26,
  138. thickness=5,
  139. start_angle=-90,
  140. end_angle=180
  141. },
  142. {
  143. name='upspeedf',
  144. arg='eth0',
  145. max=200,
  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=230, y=452,
  151. radius=20,
  152. thickness=5,
  153. start_angle=-90,
  154. end_angle=180
  155. },
  156. {
  157. name='downspeedf',
  158. arg='eth0',
  159. max=800,
  160. bg_colour=ring_background_color,
  161. bg_alpha=ring_background_alpha,
  162. fg_colour=ring_foreground_color,
  163. fg_alpha=ring_foreground_alpha,
  164. x=230, y=452,
  165. radius=26,
  166. thickness=5,
  167. start_angle=-90,
  168. end_angle=180
  169. },
  170. {
  171. name='hwmon temp 1',
  172. arg='',
  173. max=100,
  174. bg_colour=ring_background_color,
  175. bg_alpha=ring_background_alpha,
  176. fg_colour=ring_foreground_color3,
  177. fg_alpha=ring_foreground_alpha,
  178. x=200, y=401,
  179. radius=26,
  180. thickness=6,
  181. start_angle=-90,
  182. end_angle=180
  183. },
  184. }
  185.  
  186. require 'cairo'
  187.  
  188. function rgb_to_r_g_b(colour,alpha)
  189. return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  190. end
  191.  
  192. function draw_ring(cr,t,pt)
  193. local w,h=conky_window.width,conky_window.height
  194.  
  195. local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
  196. local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  197.  
  198. local angle_0=sa*(2*math.pi/360)-math.pi/2
  199. local angle_f=ea*(2*math.pi/360)-math.pi/2
  200. local t_arc=t*(angle_f-angle_0)
  201.  
  202. -- Draw background ring
  203.  
  204. cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  205. cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  206. cairo_set_line_width(cr,ring_w)
  207. cairo_stroke(cr)
  208.  
  209. -- Draw indicator ring
  210.  
  211. cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
  212. cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  213. cairo_stroke(cr)
  214. end
  215.  
  216. function conky_ring_stats()
  217. local function setup_rings(cr,pt)
  218. local str=''
  219. local value=0
  220.  
  221. str=string.format('${%s %s}',pt['name'],pt['arg'])
  222. str=conky_parse(str)
  223.  
  224. value=tonumber(str)
  225. if value == nil then value = 0 end
  226. pct=value/pt['max']
  227.  
  228. draw_ring(cr,pct,pt)
  229. end
  230.  
  231. if conky_window==nil then return end
  232. local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  233.  
  234. local cr=cairo_create(cs)
  235.  
  236. local updates=conky_parse('${updates}')
  237. update_num=tonumber(updates)
  238.  
  239. if update_num>5 then
  240. for i in pairs(settings_table) do
  241. setup_rings(cr,settings_table[i])
  242. end
  243. end
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement