NoPaste

clock1_rings.lua

von Apfelmann

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

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN