daily pastebin goal
57%
SHARE
TWEET

clock_rings.lua

a guest May 5th, 2011 226 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 2011mint -- 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=0x0066FF,
  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=0x0066FF,
  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=0x0066FF,
  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=0x0066FF,
  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=0x0066FF,
  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=0x0066FF,
  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='memperc',
  119.         arg='',
  120.         max=100,
  121.         bg_colour=0xffffff,
  122.         bg_alpha=0.2,
  123.         fg_colour=0x0066FF,
  124.         fg_alpha=0.8,
  125.         x=75, y=350,
  126.         radius=25,
  127.         thickness=5,
  128.         start_angle=-90,
  129.         end_angle=180
  130.     },
  131.     {
  132.         name='swapperc',
  133.         arg='',
  134.         max=100,
  135.         bg_colour=0xffffff,
  136.         bg_alpha=0.2,
  137.         fg_colour=0x0066FF,
  138.         fg_alpha=0.8,
  139.         x=100, y=400,
  140.         radius=25,
  141.         thickness=5,
  142.         start_angle=-90,
  143.         end_angle=180
  144.     },
  145.     {
  146.         name='fs_used_perc',
  147.         arg='/',
  148.         max=100,
  149.         bg_colour=0xffffff,
  150.         bg_alpha=0.2,
  151.         fg_colour=0x0066FF,
  152.         fg_alpha=0.8,
  153.         x=125, y=450,
  154.         radius=25,
  155.         thickness=5,
  156.         start_angle=-90,
  157.         end_angle=180
  158.     },
  159.         {
  160.         name='downspeedf',
  161.         arg='eth0',
  162.         max=100,
  163.         bg_colour=0xffffff,
  164.         bg_alpha=0.2,
  165.         fg_colour=0x0066FF,
  166.         fg_alpha=0.8,
  167.         x=150, y=500,
  168.         radius=25,
  169.         thickness=4,
  170.         start_angle=-90,
  171.         end_angle=180
  172.     },
  173.         {
  174.         name='upspeedf',
  175.         arg='eth0',
  176.         max=100,
  177.         bg_colour=0xffffff,
  178.         bg_alpha=0.2,
  179.         fg_colour=0xff3300,
  180.         fg_alpha=0.8,
  181.         x=150, y=500,
  182.         radius=20,
  183.         thickness=4,
  184.         start_angle=-90,
  185.         end_angle=180
  186.     },
  187. }
  188.  
  189. -- Use these settings to define the origin and extent of your clock.
  190.  
  191. clock_r=65
  192.  
  193. -- "clock_x" and "clock_y" are the coordinates of the centre of the clock, in pixels, from the top left of the Conky window.
  194.  
  195. clock_x=100
  196. clock_y=150
  197.  
  198. show_seconds=true
  199.  
  200. require 'cairo'
  201.  
  202. function rgb_to_r_g_b(colour,alpha)
  203.     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  204. end
  205.  
  206. function draw_ring(cr,t,pt)
  207.     local w,h=conky_window.width,conky_window.height
  208.    
  209.     local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
  210.     local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  211.  
  212.     local angle_0=sa*(2*math.pi/360)-math.pi/2
  213.     local angle_f=ea*(2*math.pi/360)-math.pi/2
  214.     local t_arc=t*(angle_f-angle_0)
  215.  
  216.     -- Draw background ring
  217.  
  218.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  219.     cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  220.     cairo_set_line_width(cr,ring_w)
  221.     cairo_stroke(cr)
  222.    
  223.     -- Draw indicator ring
  224.  
  225.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
  226.     cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  227.     cairo_stroke(cr)        
  228. end
  229.  
  230. function draw_clock_hands(cr,xc,yc)
  231.     local secs,mins,hours,secs_arc,mins_arc,hours_arc
  232.     local xh,yh,xm,ym,xs,ys
  233.    
  234.     secs=os.date("%S")    
  235.     mins=os.date("%M")
  236.     hours=os.date("%I")
  237.        
  238.     secs_arc=(2*math.pi/60)*secs
  239.     mins_arc=(2*math.pi/60)*mins+secs_arc/60
  240.     hours_arc=(2*math.pi/12)*hours+mins_arc/12
  241.        
  242.     -- Draw hour hand
  243.    
  244.     xh=xc+0.7*clock_r*math.sin(hours_arc)
  245.     yh=yc-0.7*clock_r*math.cos(hours_arc)
  246.     cairo_move_to(cr,xc,yc)
  247.     cairo_line_to(cr,xh,yh)
  248.    
  249.     cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
  250.     cairo_set_line_width(cr,5)
  251.     cairo_set_source_rgba(cr,1.0,1.0,1.0,1.0)
  252.     cairo_stroke(cr)
  253.    
  254.     -- Draw minute hand
  255.    
  256.     xm=xc+clock_r*math.sin(mins_arc)
  257.     ym=yc-clock_r*math.cos(mins_arc)
  258.     cairo_move_to(cr,xc,yc)
  259.     cairo_line_to(cr,xm,ym)
  260.    
  261.     cairo_set_line_width(cr,3)
  262.     cairo_stroke(cr)
  263.    
  264.     -- Draw seconds hand
  265.    
  266.     if show_seconds then
  267.         xs=xc+clock_r*math.sin(secs_arc)
  268.         ys=yc-clock_r*math.cos(secs_arc)
  269.         cairo_move_to(cr,xc,yc)
  270.         cairo_line_to(cr,xs,ys)
  271.    
  272.         cairo_set_line_width(cr,1)
  273.         cairo_stroke(cr)
  274.     end
  275. end
  276.  
  277. function conky_clock_rings()
  278.     local function setup_rings(cr,pt)
  279.         local str=''
  280.         local value=0
  281.        
  282.         str=string.format('${%s %s}',pt['name'],pt['arg'])
  283.         str=conky_parse(str)
  284.        
  285.         value=tonumber(str)
  286.         pct=value/pt['max']
  287.        
  288.         draw_ring(cr,pct,pt)
  289.     end
  290.    
  291.     -- Check that Conky has been running for at least 5s
  292.  
  293.     if conky_window==nil then return end
  294.     local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  295.    
  296.     local cr=cairo_create(cs)    
  297.    
  298.     local updates=conky_parse('${updates}')
  299.     update_num=tonumber(updates)
  300.    
  301.     if update_num>5 then
  302.         for i in pairs(settings_table) do
  303.             setup_rings(cr,settings_table[i])
  304.         end
  305.     end
  306.    
  307.     draw_clock_hands(cr,clock_x,clock_y)
  308. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top