Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

rings.lua

By: freieschaf on Nov 16th, 2011  |  syntax: None  |  size: 9.19 KB  |  hits: 274  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.  
  19. settings_table = {
  20.     {
  21.         -- Edit this table to customise your rings.
  22.         -- You can create more rings simply by adding more elements to settings_table.
  23.         -- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
  24.         name='time',
  25.         -- "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 ''.
  26.         arg='%d',
  27.         -- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
  28.         max=31,
  29.         -- "bg_colour" is the colour of the base ring.
  30.         bg_colour=0xcccccc,
  31.         -- "bg_alpha" is the alpha value of the base ring.
  32.         bg_alpha=0.0,
  33.         -- "fg_colour" is the colour of the indicator part of the ring.
  34.         fg_colour=0xe43526,
  35.         -- "fg_alpha" is the alpha value of the indicator part of the ring.
  36.         fg_alpha=0.8,
  37.         -- "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.
  38.         x=170, y=170,
  39.         -- "radius" is the radius of the ring.
  40.         radius=120,
  41.         -- "thickness" is the thickness of the ring, centred around the radius.
  42.         thickness=1,
  43.         -- "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
  44.         start_angle=0,
  45.         -- "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.
  46.         end_angle=360,
  47.         sectors = 31,
  48.         gap_sectors=1
  49.     },
  50.     {
  51.         name='time',
  52.         arg='%H.%M',
  53.         max=23,
  54.         bg_colour=0xcccccc,
  55.         bg_alpha=0.1,
  56.         fg_colour=0xcccccc,
  57.         fg_alpha=0.8,
  58.         x=170, y=170,
  59.         radius=80,
  60.         thickness=10,
  61.         start_angle=0,
  62.         end_angle=360
  63.     },
  64.     {
  65.         name='time',
  66.         arg='%M.%S',
  67.         max=59,
  68.         bg_colour=0xcccccc,
  69.         bg_alpha=0.1,
  70.         fg_colour=0xcccccc,
  71.         fg_alpha=0.8,
  72.         x=170, y=170,
  73.         radius=70,
  74.         thickness=5,
  75.         start_angle=0,
  76.         end_angle=360
  77.     },
  78.     {
  79.         name='time',
  80.         arg='%S',
  81.         max=59,
  82.         bg_colour=0xcccccc,
  83.         bg_alpha=0.1,
  84.         fg_colour=0xcccccc,
  85.         fg_alpha=0.8,
  86.         x=170, y=170,
  87.         radius=65,
  88.         thickness=3,
  89.         start_angle=0,
  90.         end_angle=360
  91.     },
  92.     --[[{
  93.         name='cpu',
  94.         arg='cpu2',
  95.         max=100,
  96.         bg_colour=0xffffff,
  97.         bg_alpha=0,
  98.         fg_colour=0xffffff,
  99.         fg_alpha=0.1,
  100.         x=165, y=170,
  101.         radius=76,
  102.         thickness=5,
  103.         start_angle=60,
  104.         end_angle=120
  105.     },
  106.     {
  107.         name='cpu',
  108.         arg='cpu0',
  109.         max=100,
  110.         bg_colour=0xffffff,
  111.         bg_alpha=0.1,
  112.         fg_colour=0xffffff,
  113.         fg_alpha=0.4,
  114.         x=165, y=170,
  115.         radius=84.5,
  116.         thickness=8,
  117.         start_angle=60,
  118.         end_angle=120
  119.     },
  120.     {
  121.         name='battery_percent',
  122.         arg='BAT1',
  123.         max=100,
  124.         bg_colour=0xffffff,
  125.         bg_alpha=0.1,
  126.         fg_colour=0xffffff,
  127.         fg_alpha=0.6,
  128.         x=165, y=170,
  129.         radius=72,
  130.         thickness=11,
  131.         start_angle=122,
  132.         end_angle=210
  133.     },
  134.     {
  135.         name='memperc',
  136.         arg='',
  137.         max=100,
  138.         bg_colour=0xffffff,
  139.         bg_alpha=0.1,
  140.         fg_colour=0xffffff,
  141.         fg_alpha=0.8,
  142.         x=165, y=170,
  143.         radius=83.5,
  144.         thickness=8,
  145.         start_angle=122,
  146.         end_angle=210
  147.     },
  148.     {
  149.         name='time',
  150.         arg='%d',
  151.         max=31,
  152.         bg_colour=0xffffff,
  153.         bg_alpha=0.1,
  154.         fg_colour=0xffffff,
  155.         fg_alpha=0.8,
  156.         x=165, y=170,
  157.         radius=70,
  158.         thickness=5,
  159.         start_angle=212,
  160.         end_angle=360
  161.     },
  162.     {
  163.         name='time',
  164.         arg='%m',
  165.         max=12,
  166.         bg_colour=0xffffff,
  167.         bg_alpha=0.1,
  168.         fg_colour=0xffffff,
  169.         fg_alpha=0.8,
  170.         x=165, y=170,
  171.         radius=76,
  172.         thickness=5,
  173.         start_angle=212,
  174.         end_angle=360
  175.     },
  176.     {
  177.         name='fs_used_perc',
  178.         arg='/',
  179.         max=150,
  180.         bg_colour=0xffffff,
  181.         bg_alpha=0.2,
  182.         fg_colour=0xffffff,
  183.         fg_alpha=0.3,
  184.         x=165, y=170,
  185.         radius=108.5,
  186.         thickness=3,
  187.         start_angle=-120,
  188.         end_angle=240
  189.     },
  190.     {
  191.         name='fs_used_perc',
  192.         arg='/',
  193.         max=100,
  194.         bg_colour=0xffffff,
  195.         bg_alpha=0.2,
  196.         fg_colour=0xffffff,
  197.         fg_alpha=0.3,
  198.         x=165, y=170,
  199.         radius=135,
  200.         thickness=50,
  201.         start_angle=-120,
  202.         end_angle=120
  203.     },]]
  204. }
  205.  
  206. require 'cairo'
  207.  
  208. function rgb_to_r_g_b(colour,alpha)
  209.     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  210. end
  211.  
  212. function draw_ring(cr,t,pt)
  213.     local w,h=conky_window.width,conky_window.height
  214.  
  215.     local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
  216.     local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  217.  
  218.     local angle_0=sa*(2*math.pi/360)-math.pi/2
  219.     local angle_f=ea*(2*math.pi/360)-math.pi/2
  220.     local t_arc=t*(angle_f-angle_0)
  221.  
  222.     -- Draw background ring
  223.  
  224.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  225.     cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  226.     cairo_set_line_width(cr,ring_w)
  227.     cairo_stroke(cr)
  228.  
  229.     -- Draw indicator ring
  230.  
  231.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
  232.     cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  233.     cairo_stroke(cr)        
  234. end
  235.  
  236. function conky_ring_stats()
  237.     local function setup_rings(cr,pt)
  238.         local str=''
  239.         local value=0
  240.  
  241.         str=string.format('${%s %s}',pt['name'],pt['arg'])
  242.         str=conky_parse(str)
  243.  
  244.         value=tonumber(str)
  245.         if value == nil then value = 0 end
  246.         pct=value/pt['max']
  247.  
  248.         draw_ring(cr,pct,pt)
  249.     end
  250.  
  251.     if conky_window==nil then return end
  252.     local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  253.  
  254.     local cr=cairo_create(cs)    
  255.  
  256.     local updates=conky_parse('${updates}')
  257.     update_num=tonumber(updates)
  258.  
  259.     if update_num>5 then
  260.         for i in pairs(settings_table) do
  261.             setup_rings(cr,settings_table[i])
  262.         end
  263.     end
  264. end
  265.  
  266. --[[ This is a script made for draw a transaprent background for conky ]]
  267. -- Change these settings to affect your background.
  268. -- "corner_r" is the radius, in pixels, of the rounded corners. If you don't want rounded corners, use 0.
  269.  
  270. corner_r=300
  271.  
  272. -- Set the colour and transparency (alpha) of your background.
  273.  
  274. bg_colour=0x000000
  275. bg_alpha=0.2
  276.  
  277. function conky_draw_bg()
  278.         if conky_window==nil then return end
  279.         local w=conky_window.width
  280.         local h=conky_window.height
  281.         local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
  282.         cr=cairo_create(cs)
  283.  
  284.         cairo_move_to(cr,corner_r,0)
  285.         cairo_line_to(cr,w-corner_r,0)
  286.         cairo_curve_to(cr,w,0,w,0,w,corner_r)
  287.         cairo_line_to(cr,w,h-corner_r)
  288.         cairo_curve_to(cr,w,h,w,h,w-corner_r,h)
  289.         cairo_line_to(cr,corner_r,h)
  290.         cairo_curve_to(cr,0,h,0,h,0,h-corner_r)
  291.         cairo_line_to(cr,0,corner_r)
  292.         cairo_curve_to(cr,0,0,0,0,corner_r,0)
  293.         cairo_close_path(cr)
  294.  
  295.         cairo_set_source_rgba(cr,rgb_to_r_g_b(bg_colour,bg_alpha))
  296.         cairo_fill(cr)
  297. end
  298.  
  299. function conky_main()
  300.         conky_draw_bg()
  301.         conky_ring_stats()
  302. end
  303.  
  304.