Advertisement
lswest

rings

Jun 26th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.50 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.lua
  10.     lua_draw_hook_pre ring_stats
  11.    
  12. Changelog:
  13. + v1.2 -- Added option for the ending angle of the rings (07.10.2009)
  14. + 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)
  15. + v1.0 -- Original release (28.09.2009)
  16. ]]
  17.  
  18. -- Change these settings to affect your background.
  19. -- "corner_r" is the radius, in pixels, of the rounded corners. If you don't want rounded corners, use 0.
  20. corner_r=35
  21. -- Set the colour and transparency (alpha) of your background.
  22. bg_colour=0x000000
  23. bg_alpha=0.2
  24.  
  25. settings_table = {
  26. --[[    {
  27.         -- Edit this table to customise your rings.
  28.         -- You can create more rings simply by adding more elements to settings_table.
  29.         -- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
  30.         name='time',
  31.         -- "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 ''.
  32.         arg='%I.%M',
  33.         -- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
  34.         max=12,
  35.         -- "bg_colour" is the colour of the base ring.
  36.         bg_colour=0xffffff,
  37.         -- "bg_alpha" is the alpha value of the base ring.
  38.         bg_alpha=0.1,
  39.         -- "fg_colour" is the colour of the indicator part of the ring.
  40.         fg_colour=0xffffff,
  41.         -- "fg_alpha" is the alpha value of the indicator part of the ring.
  42.         fg_alpha=0.2,
  43.         -- "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.
  44.         x=165, y=170,
  45.         -- "radius" is the radius of the ring.
  46.         radius=50,
  47.         -- "thickness" is the thickness of the ring, centred around the radius.
  48.         thickness=5,
  49.         -- "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
  50.         start_angle=0,
  51.         -- "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.
  52.         end_angle=360
  53.     }, ]]
  54.     {
  55.         name='time',
  56.         arg='%H',
  57.         max=12,
  58.         bg_colour=0xffffff,
  59.         bg_alpha=0.2,
  60.         fg_colour=0xffffff,
  61.         fg_alpha=1.0,
  62.         x=120, y=140,
  63.         radius=89,
  64.         thickness=7,
  65.         start_angle=0,
  66.         end_angle=360
  67.     },
  68.     {
  69.         name='time',
  70.         arg='%M',
  71.         max=60,
  72.         bg_colour=0xffffff,
  73.         bg_alpha=0.2,
  74.         fg_colour=0xffffff,
  75.         fg_alpha=0.9,
  76.         x=120, y=140,
  77.         radius=79,
  78.         thickness=7,
  79.         start_angle=0,
  80.         end_angle=360
  81.     },
  82.     {
  83.         name='time',
  84.         arg='%S',
  85.         max=60,
  86.         bg_colour=0xffffff,
  87.         bg_alpha=0.2,
  88.         fg_colour=0xffffff,
  89.         fg_alpha=0.5,
  90.         x=120, y=140,
  91.         radius=70,
  92.         thickness=5,
  93.         start_angle=0,
  94.         end_angle=360
  95.     },
  96. }
  97.  
  98. require 'cairo'
  99.  
  100. function rgb_to_r_g_b(colour,alpha)
  101.     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  102. end
  103.  
  104. function draw_ring(cr,t,pt)
  105.     local w,h=conky_window.width,conky_window.height
  106.     local port=pt['max']
  107.     local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
  108.     local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  109.    
  110.     local arc_w = 2*math.pi/port -- Sets width of arc to 1/"max"nth of a circle
  111.     local angle_0 = sa*(2*math.pi/360)-math.pi/2 -- Converts the starting angle to radians & translates to starting from the top
  112.     local angle_f = ea*(2*math.pi/360)-math.pi/2
  113.     local t_arc = angle_0 + t*2*math.pi -- Converts the percentage to a part of 2*pi radians & adds the starting angle
  114.  
  115.     -- Draw background ring
  116.  
  117.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  118.     cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  119.     cairo_set_line_width(cr,ring_w)
  120.     cairo_stroke(cr)
  121.    
  122.     -- Draw indicator ring
  123.     if pt['arg'] == '%S' then cairo_arc(cr, xc, yc, ring_r, angle_0, t_arc+arc_w) end
  124.     cairo_arc(cr, xc, yc, ring_r, t_arc-arc_w, t_arc+arc_w)
  125.     cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  126.     cairo_stroke(cr)       
  127. end
  128. function conky_ring_stats()
  129.     local function setup_rings(cr,pt)
  130.         local str=''
  131.         local value=0
  132.        
  133.         str=string.format('${%s %s}',pt['name'],pt['arg'])
  134.         str=conky_parse(str)
  135.  
  136.  
  137.         value=tonumber(str)
  138.         pct=value/pt['max']
  139.    
  140.         draw_ring(cr,pct,pt)
  141.     end
  142.     local function draw_bg()
  143.         if conky_window==nil then return end
  144.         local w=conky_window.width
  145.         local h=conky_window.height
  146.         local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
  147.         cr=cairo_create(cs)
  148.    
  149.         cairo_move_to(cr,corner_r,0)
  150.         cairo_line_to(cr,w-corner_r,0)
  151.         cairo_curve_to(cr,w,0,w,0,w,corner_r)
  152.         cairo_line_to(cr,w,h-corner_r)
  153.         cairo_curve_to(cr,w,h,w,h,w-corner_r,h)
  154.         cairo_line_to(cr,corner_r,h)
  155.         cairo_curve_to(cr,0,h,0,h,0,h-corner_r)
  156.         cairo_line_to(cr,0,corner_r)
  157.         cairo_curve_to(cr,0,0,0,0,corner_r,0)
  158.         cairo_close_path(cr)
  159.    
  160.         cairo_set_source_rgba(cr,rgb_to_r_g_b(bg_colour,bg_alpha))
  161.         cairo_fill(cr)
  162.     end
  163.  
  164.  
  165.     if conky_window==nil then return end
  166.     local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  167.    
  168.     local cr=cairo_create(cs)  
  169.    
  170.     local updates=conky_parse('${updates}')
  171.     update_num=tonumber(updates)
  172.    
  173.     if update_num>5 then
  174.         for i in pairs(settings_table) do
  175.             draw_bg()
  176.             setup_rings(cr,settings_table[i])
  177.         end
  178.     end
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement