Advertisement
Forge64

conky rings

Dec 16th, 2014
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1. --[[
  2. Clock Rings by Linux Mint (2011) reEdited by despot77
  3.  
  4.  
  5. This script draws percentage meters as rings, and also draws clock hands if you want! It is fully customisable; all options are described in the script. This script is based off a combination of my clock.lua script and my rings.lua script.
  6.  
  7.  
  8. 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.
  9.  
  10.  
  11. To call this script in Conky, use the following (assuming that you save this script to ~/scripts/rings.lua):
  12. lua_load ~/scripts/clock_rings.lua
  13. lua_draw_hook_pre clock_rings
  14.  
  15. Changelog:
  16. + v1.0 -- Original release (30.09.2009)
  17. v1.1p -- Jpope edit londonali1010 (05.10.2009)
  18. *v 2011mint -- reEdit despot77 (18.02.2011)
  19. ]]
  20.  
  21.  
  22. settings_table = {
  23. {
  24. -- Edit this table to customise your rings.
  25. -- You can create more rings simply by adding more elements to settings_table.
  26. -- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
  27. name='time',
  28. -- "arg" is the argument to the stat type, e.g. if in Conky you would write ${cpu cpu0}, 'cpu0' would be the argument. If you would not use an argument in the Conky variable, use ''.
  29. arg='%I.%M',
  30. -- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
  31. max=12,
  32. -- "bg_colour" is the colour of the base ring.
  33. bg_colour=0xffffff,
  34. -- "bg_alpha" is the alpha value of the base ring.
  35. bg_alpha=0.1,
  36. -- "fg_colour" is the colour of the indicator part of the ring.
  37. fg_colour=0x4B9632,
  38. -- "fg_alpha" is the alpha value of the indicator part of the ring.
  39. fg_alpha=0.2,
  40. -- "x" and "y" are the x and y coordinates of the centre of the ring, relative to the top left corner of the Conky window.
  41. x=265, y=90,
  42. -- "radius" is the radius of the ring.
  43. radius=50,
  44. -- "thickness" is the thickness of the ring, centred around the radius.
  45. thickness=5,
  46. -- "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
  47. start_angle=0,
  48. -- "end_angle" is the ending angle of the ring, in degrees, clockwise from top. Value can be either positive or negative, but must be larger than start_angle.
  49. end_angle=360
  50. },
  51. {
  52. name='time',
  53. arg='%M.%S',
  54. max=60,
  55. bg_colour=0xffffff,
  56. bg_alpha=0.1,
  57. fg_colour=0x4B9632,
  58. fg_alpha=0.4,
  59. x=265, y=90,
  60. radius=56,
  61. thickness=5,
  62. start_angle=0,
  63. end_angle=360
  64. },
  65. {
  66. name='time',
  67. arg='%S',
  68. max=60,
  69. bg_colour=0xffffff,
  70. bg_alpha=0.1,
  71. fg_colour=0x4B9632,
  72. fg_alpha=0.6,
  73. x=265, y=90,
  74. radius=62,
  75. thickness=5,
  76. start_angle=0,
  77. end_angle=360
  78. },
  79. {
  80. name='time',
  81. arg='%d',
  82. max=31,
  83. bg_colour=0xffffff,
  84. bg_alpha=0.1,
  85. fg_colour=0x4B9632,
  86. fg_alpha=0.8,
  87. x=265, y=90,
  88. radius=70,
  89. thickness=5,
  90. start_angle=-90,
  91. end_angle=90
  92. },
  93. {
  94. name='time',
  95. arg='%m',
  96. max=12,
  97. bg_colour=0xffffff,
  98. bg_alpha=0.1,
  99. fg_colour=0x4B9632,
  100. fg_alpha=1,
  101. x=265, y=90,
  102. radius=76,
  103. thickness=5,
  104. start_angle=-90,
  105. end_angle=90
  106. },
  107. {
  108. name='cpu',
  109. arg='cpu1',
  110. max=100,
  111. bg_colour=0xffffff,
  112. bg_alpha=0.2,
  113. fg_colour=0x00FF00,
  114. fg_alpha=0.8,
  115. x=280, y=265,
  116. radius=55,
  117. thickness=5,
  118. start_angle=-90,
  119. end_angle=180
  120. },
  121. {
  122. name='cpu',
  123. arg='cpu2',
  124. max=100,
  125. bg_colour=0xffffff,
  126. bg_alpha=0.2,
  127. fg_colour=0x00FF00,
  128. fg_alpha=0.8,
  129. x=280, y=265,
  130. radius=50,
  131. thickness=5,
  132. start_angle=-90,
  133. end_angle=180
  134. },
  135. {
  136. name='cpu',
  137. arg='cpu3',
  138. max=100,
  139. bg_colour=0xffffff,
  140. bg_alpha=0.2,
  141. fg_colour=0x00FF00,
  142. fg_alpha=0.8,
  143. x=280, y=265,
  144. radius=45,
  145. thickness=5,
  146. start_angle=-90,
  147. end_angle=180
  148. },
  149. {
  150. name='cpu',
  151. arg='cpu4',
  152. max=100,
  153. bg_colour=0xffffff,
  154. bg_alpha=0.2,
  155. fg_colour=0x00FF00,
  156. fg_alpha=0.8,
  157. x=280, y=265,
  158. radius=40,
  159. thickness=5,
  160. start_angle=-90,
  161. end_angle=180
  162. },
  163. {
  164. name='cpu',
  165. arg='cpu5',
  166. max=100,
  167. bg_colour=0xffffff,
  168. bg_alpha=0.2,
  169. fg_colour=0x00FF00,
  170. fg_alpha=0.8,
  171. x=280, y=265,
  172. radius=35,
  173. thickness=5,
  174. start_angle=-90,
  175. end_angle=180
  176. },
  177. {
  178. name='cpu',
  179. arg='cpu6',
  180. max=100,
  181. bg_colour=0xffffff,
  182. bg_alpha=0.2,
  183. fg_colour=0x00FF00,
  184. fg_alpha=0.8,
  185. x=280, y=265,
  186. radius=30,
  187. thickness=5,
  188. start_angle=-90,
  189. end_angle=180
  190. },
  191. {
  192. name='cpu',
  193. arg='cpu7',
  194. max=100,
  195. bg_colour=0xffffff,
  196. bg_alpha=0.2,
  197. fg_colour=0x00FF00,
  198. fg_alpha=0.8,
  199. x=280, y=265,
  200. radius=25,
  201. thickness=5,
  202. start_angle=-90,
  203. end_angle=180
  204. },
  205. {
  206. name='cpu',
  207. arg='cpu8',
  208. max=100,
  209. bg_colour=0xffffff,
  210. bg_alpha=0.2,
  211. fg_colour=0x00FF00,
  212. fg_alpha=0.8,
  213. x=280, y=265,
  214. radius=20,
  215. thickness=5,
  216. start_angle=-90,
  217. end_angle=180
  218. },
  219. }
  220. -- Use these settings to define the origin and extent of your clock.
  221.  
  222.  
  223. clock_r=65
  224.  
  225.  
  226. -- "clock_x" and "clock_y" are the coordinates of the centre of the clock, in pixels, from the top left of the Conky window.
  227.  
  228.  
  229. clock_x=265
  230. clock_y=90
  231.  
  232.  
  233. show_seconds=true
  234.  
  235.  
  236. require 'cairo'
  237.  
  238.  
  239. function rgb_to_r_g_b(colour,alpha)
  240. return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  241. end
  242.  
  243.  
  244. function draw_ring(cr,t,pt)
  245. local w,h=conky_window.width,conky_window.height
  246.  
  247. local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
  248. local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  249.  
  250.  
  251. local angle_0=sa*(2*math.pi/360)-math.pi/2
  252. local angle_f=ea*(2*math.pi/360)-math.pi/2
  253. local t_arc=t*(angle_f-angle_0)
  254.  
  255.  
  256. -- Draw background ring
  257.  
  258.  
  259. cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  260. cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  261. cairo_set_line_width(cr,ring_w)
  262. cairo_stroke(cr)
  263.  
  264. -- Draw indicator ring
  265.  
  266.  
  267. cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
  268. cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  269. cairo_stroke(cr)
  270. end
  271.  
  272.  
  273. function draw_clock_hands(cr,xc,yc)
  274. local secs,mins,hours,secs_arc,mins_arc,hours_arc
  275. local xh,yh,xm,ym,xs,ys
  276.  
  277. secs=os.date("%S")
  278. mins=os.date("%M")
  279. hours=os.date("%I")
  280.  
  281. secs_arc=(2*math.pi/60)*secs
  282. mins_arc=(2*math.pi/60)*mins+secs_arc/60
  283. hours_arc=(2*math.pi/12)*hours+mins_arc/12
  284.  
  285. -- Draw hour hand
  286.  
  287. xh=xc+0.7*clock_r*math.sin(hours_arc)
  288. yh=yc-0.7*clock_r*math.cos(hours_arc)
  289. cairo_move_to(cr,xc,yc)
  290. cairo_line_to(cr,xh,yh)
  291.  
  292. cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
  293. cairo_set_line_width(cr,5)
  294. cairo_set_source_rgba(cr,1.0,1.0,1.0,1.0)
  295. cairo_stroke(cr)
  296.  
  297. -- Draw minute hand
  298.  
  299. xm=xc+clock_r*math.sin(mins_arc)
  300. ym=yc-clock_r*math.cos(mins_arc)
  301. cairo_move_to(cr,xc,yc)
  302. cairo_line_to(cr,xm,ym)
  303.  
  304. cairo_set_line_width(cr,3)
  305. cairo_stroke(cr)
  306.  
  307. -- Draw seconds hand
  308.  
  309. if show_seconds then
  310. xs=xc+clock_r*math.sin(secs_arc)
  311. ys=yc-clock_r*math.cos(secs_arc)
  312. cairo_move_to(cr,xc,yc)
  313. cairo_line_to(cr,xs,ys)
  314.  
  315. cairo_set_line_width(cr,1)
  316. cairo_stroke(cr)
  317. end
  318. end
  319.  
  320.  
  321. function conky_clock_rings()
  322. local function setup_rings(cr,pt)
  323. local str=''
  324. local value=0
  325.  
  326. str=string.format('${%s %s}',pt['name'],pt['arg'])
  327. str=conky_parse(str)
  328.  
  329. value=tonumber(str)
  330. pct=value/pt['max']
  331.  
  332. draw_ring(cr,pct,pt)
  333. end
  334.  
  335. -- Check that Conky has been running for at least 5s
  336.  
  337.  
  338. if conky_window==nil then return end
  339. local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  340.  
  341. local cr=cairo_create(cs)
  342.  
  343. local updates=conky_parse('${updates}')
  344. update_num=tonumber(updates)
  345.  
  346. if update_num>5 then
  347. for i in pairs(settings_table) do
  348. setup_rings(cr,settings_table[i])
  349. end
  350. end
  351.  
  352. draw_clock_hands(cr,clock_x,clock_y)
  353. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement