daily pastebin goal
85%
SHARE
TWEET

Untitled

a guest Nov 7th, 2010 15 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. conky_background_color = 0x000000
  19. conky_background_alpha = 0.5
  20.  
  21. ring_background_color = 0xffffff
  22. ring_background_alpha = 0.15
  23. ring_foreground_color = 0xffffff
  24. ring_foreground_alpha = 0.5
  25.  
  26. settings_table = {
  27.     {
  28.         -- Edit this table to customise your rings.
  29.         -- You can create more rings simply by adding more elements to settings_table.
  30.         -- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
  31.         name='time',
  32.         -- "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 ''.
  33.         arg='%I.%M',
  34.         -- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
  35.         max=12,
  36.         -- "bg_colour" is the colour of the base ring.
  37.         bg_colour=ring_background_color,
  38.         -- "bg_alpha" is the alpha value of the base ring.
  39.         bg_alpha=ring_background_alpha,
  40.         -- "fg_colour" is the colour of the indicator part of the ring.
  41.         fg_colour=ring_foreground_color,
  42.         -- "fg_alpha" is the alpha value of the indicator part of the ring.
  43.         fg_alpha=ring_foreground_alpha,
  44.         -- "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.
  45.         x=26, y=25,
  46.         -- "radius" is the radius of the ring.
  47.         radius=10,
  48.         -- "thickness" is the thickness of the ring, centred around the radius.
  49.         thickness=6,
  50.         -- "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
  51.         start_angle=0,
  52.         -- "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 (e.g. more clockwise) than start_angle.
  53.         end_angle=360
  54.     },
  55.     {
  56.         name='time',
  57.         arg='%M.%S',
  58.         max=60,
  59.         bg_colour=ring_background_color,
  60.         bg_alpha=ring_background_alpha,
  61.         fg_colour=ring_foreground_color,
  62.         fg_alpha=ring_foreground_alpha,
  63.         x=26, y=25,
  64.         radius=16,
  65.         thickness=4,
  66.         start_angle=0,
  67.         end_angle=360
  68.     },
  69.     {
  70.         name='time',
  71.         arg='%S',
  72.         max=60,
  73.         bg_colour=ring_background_color,
  74.         bg_alpha=ring_background_alpha,
  75.         fg_colour=ring_foreground_color,
  76.         fg_alpha=ring_foreground_alpha,
  77.         x=26, y=25,
  78.         radius=20,
  79.         thickness=2,
  80.         start_angle=0,
  81.         end_angle=360
  82.     },
  83.     {
  84.         name='cpu',
  85.         arg='cpu1',
  86.         max=100,
  87.         bg_colour=ring_background_color,
  88.         bg_alpha=ring_background_alpha,
  89.         fg_colour=ring_foreground_color,
  90.         fg_alpha=ring_foreground_alpha,
  91.         x=26, y=75,
  92.         radius=19,
  93.         thickness=3,
  94.         start_angle=-90,
  95.         end_angle=210
  96.     },
  97.     {
  98.         name='memperc',
  99.         arg='',
  100.         max=100,
  101.         bg_colour=ring_background_color,
  102.         bg_alpha=ring_background_alpha,
  103.         fg_colour=ring_foreground_color,
  104.         fg_alpha=ring_foreground_alpha,
  105.         x=26, y=125,
  106.         radius=19,
  107.         thickness=5,
  108.         start_angle=-90,
  109.         end_angle=200
  110.     },
  111.     {
  112.         name='swapperc',
  113.         arg='',
  114.         max=100,
  115.         bg_colour=ring_background_color,
  116.         bg_alpha=ring_background_alpha,
  117.         fg_colour=ring_foreground_color,
  118.         fg_alpha=ring_foreground_alpha,
  119.         x=26, y=125,
  120.         radius=13,
  121.         thickness=5,
  122.         start_angle=-90,
  123.         end_angle=180
  124.     },
  125.     {
  126.         name='upspeedf',
  127.         arg='eth0',
  128.         max=32,
  129.         bg_colour=ring_background_color,
  130.         bg_alpha=ring_background_alpha,
  131.         fg_colour=ring_foreground_color,
  132.         fg_alpha=ring_foreground_alpha,
  133.         x=26, y=175,
  134.         radius=13,
  135.         thickness=5,
  136.         start_angle=-90,
  137.         end_angle=180
  138.     },
  139.     {
  140.         name='downspeedf',
  141.         arg='eth0',
  142.         max=135,
  143.         bg_colour=ring_background_color,
  144.         bg_alpha=ring_background_alpha,
  145.         fg_colour=ring_foreground_color,
  146.         fg_alpha=ring_foreground_alpha,
  147.         x=26, y=175,
  148.         radius=19,
  149.         thickness=5,
  150.         start_angle=-90,
  151.         end_angle=200
  152.     }
  153. }
  154.  
  155. require 'cairo'
  156.  
  157. function rgb_to_r_g_b(colour,alpha)
  158.     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  159. end
  160.  
  161. function conky_round_rect(cr, x0, y0, w, h, r)
  162.     if (w == 0) or (h == 0) then return end
  163.     local x1 = x0 + w
  164.     local y1 = y0 + h
  165.     if w/2 < r then
  166.         if h/2 < r then
  167.             cairo_move_to  (cr, x0, (y0 + y1)/2)
  168.             cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0)
  169.             cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2)
  170.             cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1)
  171.             cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2)
  172.         else
  173.             cairo_move_to  (cr, x0, y0 + r)
  174.             cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0)
  175.             cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + r)
  176.             cairo_line_to (cr, x1 , y1 - r)
  177.             cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1)
  178.             cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- r)
  179.         end
  180.     else
  181.         if h/2 < r then
  182.             cairo_move_to  (cr, x0, (y0 + y1)/2)
  183.             cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + r, y0)
  184.             cairo_line_to (cr, x1 - r, y0)
  185.             cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2)
  186.             cairo_curve_to (cr, x1, y1, x1, y1, x1 - r, y1)
  187.             cairo_line_to (cr, x0 + r, y1)
  188.             cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2)
  189.         else
  190.             cairo_move_to  (cr, x0, y0 + r)
  191.             cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + r, y0)
  192.             cairo_line_to (cr, x1 - r, y0)
  193.             cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + r)
  194.             cairo_line_to (cr, x1 , y1 - r)
  195.             cairo_curve_to (cr, x1, y1, x1, y1, x1 - r, y1)
  196.             cairo_line_to (cr, x0 + r, y1)
  197.             cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- r)
  198.         end
  199.     end
  200.     cairo_close_path (cr)
  201. end
  202.  
  203.  
  204. function draw_ring(cr,t,pt)
  205.     local w,h=conky_window.width,conky_window.height
  206.  
  207.     local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
  208.     local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  209.  
  210.     local angle_0=sa*(2*math.pi/360)-math.pi/2
  211.     local angle_f=ea*(2*math.pi/360)-math.pi/2
  212.     local t_arc=t*(angle_f-angle_0)
  213.  
  214.     -- Draw background ring
  215.  
  216.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  217.     cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  218.     cairo_set_line_width(cr,ring_w)
  219.     cairo_stroke(cr)
  220.  
  221.     -- Draw indicator ring
  222.  
  223.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
  224.     cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  225.     cairo_stroke(cr)        
  226. end
  227.  
  228. cs, cr = nil -- initialize our cairo surface and context to nil
  229. function conky_ring_stats()
  230.     local function setup_rings(cr,pt)
  231.         local str=''
  232.         local value=0
  233.  
  234.         str=string.format('${%s %s}',pt['name'],pt['arg'])
  235.         str=conky_parse(str)
  236.  
  237.         value=tonumber(str)
  238.         if value == nil then value = 0 end
  239.         pct=value/pt['max']
  240.  
  241.         draw_ring(cr,pct,pt)
  242.     end
  243.  
  244.     if conky_window==nil then return end
  245.     --local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  246.  
  247.     --local cr=cairo_create(cs)
  248.  
  249.     if cs == nil or cairo_xlib_surface_get_width(cs) ~= conky_window.width or cairo_xlib_surface_get_height(cs) ~= conky_window.height then
  250.         if cs then cairo_surface_destroy(cs) end
  251.         cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  252.     end
  253.     if cr then cairo_destroy(cr) end
  254.     cr = cairo_create(cs)
  255.  
  256.  
  257.     local updates=conky_parse('${updates}')
  258.     update_num=tonumber(updates)
  259.  
  260.     if update_num>5 then
  261.         conky_round_rect(cr, 3, 0, conky_window.width-6, conky_window.height-3, 15)
  262.         cairo_set_source_rgba(cr, rgb_to_r_g_b(conky_background_color, conky_background_alpha))
  263.         cairo_fill(cr)
  264.         for i in pairs(settings_table) do
  265.             setup_rings(cr,settings_table[i])
  266.         end
  267.     end
  268.     cairo_destroy(cr)
  269.     cr = nil
  270. end
  271.  
  272. function conky_cairo_cleanup()
  273.     cairo_surface_destroy(cs)
  274.     cs = nil
  275. end
RAW Paste Data
Top