Conky widgets (en)

From Conky PitStop

(Difference between revisions)
Jump to: navigation, search
(Created page with "__NOTITLE__ =Conky widgets= Category:English Category:Tips and tricks (en) Category:Lua scripts (en) Category:Londonali1010 scripts (en) {{i18n|en|Conky widgets}}...")
(Conky widgets)
Line 44: Line 44:
Easy, huh?
Easy, huh?
-
And in the example above, I’ve used two widget code blocks. First, Air Clock:
+
And in the example above, I’ve used two widget code blocks : Air Clock & the Ring Meter.
-
<pre>--[[ AIR CLOCK WIDGET ]]
+
'''Widgets available''' in the archive [[Media:script-londonali1010-widgets.tar.gz|.tar.gz]]
-
    --[[ Options (xc, yc, size):
+
-
        "xc" and "yc" are the x and y coordinates of the centre of the clock, in pixels, relative to the top left of the Conky window
+
-
        "size" is the total size of the widget, in pixels ]]
+
-
    function air_clock(cr, xc, yc, size)
+
# Air Clock. An analog clock, designed after the “Air” clock in KDE 4.3. ([[Air clock (en)|see also]])
-
        local offset = 0
+
# Ring Meter. A graphical ring display of system stats. ([[ring meters (en)|see also]])
 +
# Ring Meter (Counter-Clockwise). A counter-clockwise version of the ring meter above.
 +
# Clock Hands. A simple widget that draws clock hands only.
-
        shadow_width = size * 0.03
 
-
        shadow_xoffset = 0
 
-
        shadow_yoffset = size * 0.01
 
-
 
-
        if shadow_xoffset >= shadow_yoffset then
 
-
            offset = shadow_xoffset
 
-
        else offset = shadow_yoffset
 
-
        end
 
-
 
-
        local clock_r = (size - 2 * offset) / (2 * 1.25)
 
-
 
-
        show_seconds=true
 
-
 
-
        -- Grab time
 
-
 
-
        local hours=os.date("%I")
 
-
        local mins=os.date("%M")
 
-
        local secs=os.date("%S")
 
-
 
-
        secs_arc=(2*math.pi/60)*secs
 
-
        mins_arc=(2*math.pi/60)*mins
 
-
        hours_arc=(2*math.pi/12)*hours+mins_arc/12
 
-
 
-
        -- Drop shadow
 
-
 
-
        local ds_pat=cairo_pattern_create_radial(xc+shadow_xoffset,yc+shadow_yoffset,clock_r*1.25,xc+shadow_xoffset,yc+shadow_yoffset,clock_r*1.25+shadow_width)
 
-
        cairo_pattern_add_color_stop_rgba(ds_pat,0,0,0,0,0.2)
 
-
        cairo_pattern_add_color_stop_rgba(ds_pat,1,0,0,0,0)
 
-
 
-
        cairo_move_to(cr,0,0)
 
-
        cairo_line_to(cr,conky_window.width,0)
 
-
        cairo_line_to(cr,conky_window.width, conky_window.height)
 
-
        cairo_line_to(cr,0,conky_window.height)
 
-
        cairo_close_path(cr)
 
-
        cairo_new_sub_path(cr)
 
-
        cairo_arc(cr,xc,yc,clock_r*1.25,0,2*math.pi)
 
-
        cairo_set_source(cr,ds_pat)
 
-
        cairo_set_fill_rule(cr,CAIRO_FILL_RULE_EVEN_ODD)
 
-
        cairo_fill(cr)
 
-
 
-
        -- Glassy border
 
-
 
-
        cairo_arc(cr,xc,yc,clock_r*1.25,0,2*math.pi)
 
-
        cairo_set_source_rgba(cr,0.5,0.5,0.5,0.2)
 
-
        cairo_set_line_width(cr,1)
 
-
        cairo_stroke(cr)
 
-
 
-
        local border_pat=cairo_pattern_create_linear(xc,yc-clock_r*1.25,xc,yc+clock_r*1.25)
 
-
 
-
        cairo_pattern_add_color_stop_rgba(border_pat,0,1,1,1,0.7)
 
-
        cairo_pattern_add_color_stop_rgba(border_pat,0.3,1,1,1,0)
 
-
        cairo_pattern_add_color_stop_rgba(border_pat,0.5,1,1,1,0)
 
-
        cairo_pattern_add_color_stop_rgba(border_pat,0.7,1,1,1,0)
 
-
        cairo_pattern_add_color_stop_rgba(border_pat,1,1,1,1,0.7)
 
-
        cairo_set_source(cr,border_pat)
 
-
        cairo_arc(cr,xc,yc,clock_r*1.125,0,2*math.pi)
 
-
        cairo_close_path(cr)
 
-
        cairo_set_line_width(cr,clock_r*0.25)
 
-
        cairo_stroke(cr)
 
-
 
-
        -- Set clock face
 
-
 
-
        cairo_arc(cr,xc,yc,clock_r,0,2*math.pi)
 
-
        cairo_close_path(cr)
 
-
 
-
        local face_pat=cairo_pattern_create_radial(xc,yc-clock_r*0.75,0,xc,yc,clock_r)
 
-
 
-
        cairo_pattern_add_color_stop_rgba(face_pat,0,1,1,1,0.9)
 
-
        cairo_pattern_add_color_stop_rgba(face_pat,0.5,1,1,1,0.9)
 
-
        cairo_pattern_add_color_stop_rgba(face_pat,1,0.9,0.9,0.9,0.9)
 
-
        cairo_set_source(cr,face_pat)
 
-
        cairo_fill_preserve(cr)
 
-
        cairo_set_source_rgba(cr,0.5,0.5,0.5,0.2)
 
-
        cairo_set_line_width(cr, 1)
 
-
        cairo_stroke (cr)
 
-
 
-
        -- Draw hour hand
 
-
 
-
        xh=xc+0.7*clock_r*math.sin(hours_arc)
 
-
        yh=yc-0.7*clock_r*math.cos(hours_arc)
 
-
        cairo_move_to(cr,xc,yc)
 
-
        cairo_line_to(cr,xh,yh)
 
-
 
-
        cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
 
-
        cairo_set_line_width(cr,5)
 
-
        cairo_set_source_rgba(cr,0,0,0,0.5)
 
-
        cairo_stroke(cr)
 
-
 
-
        -- Draw minute hand
 
-
 
-
        xm=xc+0.9*clock_r*math.sin(mins_arc)
 
-
        ym=yc-0.9*clock_r*math.cos(mins_arc)
 
-
        cairo_move_to(cr,xc,yc)
 
-
        cairo_line_to(cr,xm,ym)
 
-
 
-
        cairo_set_line_width(cr,3)
 
-
        cairo_stroke(cr)
 
-
 
-
        -- Draw seconds hand
 
-
 
-
        if show_seconds then
 
-
            xs=xc+0.9*clock_r*math.sin(secs_arc)
 
-
            ys=yc-0.9*clock_r*math.cos(secs_arc)
 
-
            cairo_move_to(cr,xc,yc)
 
-
            cairo_line_to(cr,xs,ys)
 
-
 
-
            cairo_set_line_width(cr,1)
 
-
            cairo_stroke(cr)
 
-
        end
 
-
    end
 
-
 
-
    --[[ END AIR CLOCK WIDGET ]]
 
-
</pre>
 
-
And the Ring widget code block:
 
-
<pre>--[[ RING WIDGET ]]
 
-
    --[[ Options (name, arg, max, bg_colour, bg_alpha, xc, yc, radius, thickness, start_angle, end_angle):
 
-
        "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
 
-
        "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 ''.
 
-
        "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
 
-
        "bg_colour" is the colour of the base ring.
 
-
        "bg_alpha" is the alpha value of the base ring.
 
-
        "fg_colour" is the colour of the indicator part of the ring.
 
-
        "fg_alpha" is the alpha value of the indicator part of the ring.
 
-
        "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.
 
-
        "radius" is the radius of the ring.
 
-
        "thickness" is the thickness of the ring, centred around the radius.
 
-
        "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
 
-
        "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. ]]
 
-
 
-
    function ring(cr, name, arg, max, bgc, bga, fgc, fga, xc, yc, r, t, sa, ea)
 
-
        local function rgb_to_r_g_b(colour,alpha)
 
-
            return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
 
-
        end
 
-
 
-
        local function draw_ring(pct)
 
-
            local angle_0=sa*(2*math.pi/360)-math.pi/2
 
-
            local angle_f=ea*(2*math.pi/360)-math.pi/2
 
-
            local pct_arc=pct*(angle_f-angle_0)
 
-
 
-
            -- Draw background ring
 
-
 
-
            cairo_arc(cr,xc,yc,r,angle_0,angle_f)
 
-
            cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
 
-
            cairo_set_line_width(cr,t)
 
-
            cairo_stroke(cr)
 
-
 
-
            -- Draw indicator ring
 
-
 
-
            cairo_arc(cr,xc,yc,r,angle_0,angle_0+pct_arc)
 
-
            cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
 
-
            cairo_stroke(cr)
 
-
        end
 
-
 
-
        local function setup_ring()
 
-
            local str = ''
 
-
            local value = 0
 
-
 
-
            str = string.format('${%s %s}', name, arg)
 
-
            str = conky_parse(str)
 
-
 
-
            value = tonumber(str)
 
-
            if value == nil then value = 0 end
 
-
            pct = value/max
 
-
 
-
            draw_ring(pct)
 
-
        end   
 
-
 
-
        local updates=conky_parse('${updates}')
 
-
        update_num=tonumber(updates)
 
-
 
-
        if update_num>5 then setup_ring() end
 
-
    end
 
-
 
-
</pre>
 
-
 
-
You can download more widget code blocks [[Widget code blocks (en)|here]].
 
Ah, yes, and credits: the font is ''Journal'', available on [http://www.dafont.com/ dafont.com]. Apologies, I don’t have a source for the wallpaper, but if you come across it, please do let me know so I can give proper credit.
Ah, yes, and credits: the font is ''Journal'', available on [http://www.dafont.com/ dafont.com]. Apologies, I don’t have a source for the wallpaper, but if you come across it, please do let me know so I can give proper credit.

Revision as of 11:14, 3 April 2011

Conky widgets

 Language   English   Français   


So, I wanted to make a system by which I (or anyone else) could publish Lua widgets and make it as easy as possible to build them into Conky. I’ve come up with a simple Lua script into which you can drop widget code blocks, some simple configuration info, and display them all with just a single call in Conky. That way, you only need one .conkyrc and one conky_widgets.lua script for all your widgety needs!

A screenie to whet your appetite:

Script-londonali1010-widgets-1.png

The conky_widgets.lua script looks like this, before it’s populated:

    --[[
    Conky Widgets by londonali1010 (2009)

    This script is meant to be a "shell" to hold a suite of widgets for use in Conky.

    To configure:
    + Copy the widget's code block (will be framed by --(( WIDGET NAME )) and --(( END WIDGET NAME )), with "[" instead of "(") somewhere between "require 'cairo'" and "function conky_widgets()", ensuring not to paste into another widget's code block
    + To call the widget, add the following just before the last "end" of the entire script:
        cr = cairo_create(cs)
        NAME_OF_FUNCTION(cr, OPTIONS)
        cairo_destroy(cr)
    + Replace OPTIONS with the options for your widget (should be specified in the widget's code block) 

    Call this script in Conky using the following before TEXT (assuming you save this script to ~/scripts/conky_widgets.lua):
        lua_load ~/scripts/conky_widgets.lua
        lua_draw_hook_pre widgets

    Changelog:
    + v1.0 -- Original release (17.10.2009)
    ]]

    require 'cairo'

    function conky_widgets()
        if conky_window == nil then return end
        local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
    end

Easy, huh?

And in the example above, I’ve used two widget code blocks : Air Clock & the Ring Meter.

Widgets available in the archive .tar.gz

  1. Air Clock. An analog clock, designed after the “Air” clock in KDE 4.3. (see also)
  2. Ring Meter. A graphical ring display of system stats. (see also)
  3. Ring Meter (Counter-Clockwise). A counter-clockwise version of the ring meter above.
  4. Clock Hands. A simple widget that draws clock hands only.


Ah, yes, and credits: the font is Journal, available on dafont.com. Apologies, I don’t have a source for the wallpaper, but if you come across it, please do let me know so I can give proper credit.

Happy Conkying!

Author: londonali1010
Distro: Crunchbang 9.04
Conky Version: 1.7.2

Personal tools
Namespaces
Variants
Actions
Navigation
English
Français
Toolbox