Advertisement
mukasoneca

clockringslua

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