Londonali1010 cairo pie.module (en)

From Conky PitStop

Jump to: navigation, search

londonali1010_cairo_pie.module

User:DK75

 Language   English   Français   


londonali1010 cairo_pie for One4All project

For some reason, at least for my copy of Conky 1.9.0 it reports NIL values by ${top} or ${top_mem} object so module aren't reliable.

Londonali1010-cairo-pie.png

Londonali1010 cairo pie.module-1.0.tar.gz

--[[
Conky Cairo Lua scripting example

Copyright (c) 2009 Brenden Matthews, all rights reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

You can tweak the settings_table below to suit your preferences.  You can draw
as many pies as you want, by simply adding more entries in the settings_table.

changelog:

v1.0	- (03.2012) modified by 'dk75' to one4all module
--]]

local modname = ...
local M = {}
_G[modname] = M
package.loaded[modname] = M



--[[
###############################################################################
###                              SETTINGS                                   ###
###############################################################################
--]]
local settings_table = {
	{
		-- Can be one of 'top', 'top_mem', or 'top_io' (requires iostats
		-- support in Conky).  This is the top stat we're basing our pie on.
		name='top',
		-- Draw a background pie? Can be true or false.
		draw_bg=true,
		-- Background pie colour, RGB value.
		bg_colour=0x172caf,
		-- Foreground pie colour, RGB value.
		fg_colour=0xffdf00,
		-- X and Y coordinates, relative to the centre of Conky, in pixels.
		x=0, y=0,
		-- Radius of our pie, in pixels.
		radius=100,
		-- Angle to rotate the pie, in degrees.
		angle=0,
		-- Reverse pie? Can be true or false.
		reverse=false,
		-- Shade each pie slice relative to the others.
		shade=true,
	},
	{
		name='top_mem',
		draw_bg=false,
		bg_colour=0x071672,
		fg_colour=0xff8700,
		x=0, y=0,
		radius=100,
		angle=0,
		reverse=true,
		shade=true,
	},
}
--[[
###############################################################################
###                           END OF SETTINGS                               ###
###############################################################################
--]]



function M.draw_pie(t, pt)
	local xi, yi, radius, angle, draw_bg, bg_colour, fg_colour, reverse, shade = pt['x'], pt['y'], pt['radius'], pt['angle'], pt['draw_bg'], pt['bg_colour'], pt['fg_colour'], pt['reverse'], pt['shade']

	local x, y = conky_window.text_start_x, conky_window.text_start_y
	local w, h = conky_window.text_width, conky_window.text_height
	local xc = x + w / 2. + xi
	local yc = y + h / 2. + yi
	local two_pi = 2 * math.pi

	local function draw_dat_pie(start, stop, radius, colour, alpha, str)
		if reverse then
			start, stop = -start, -stop
		end
		cairo_save(cr)

		cairo_translate(cr, xc, yc)
		cairo_rotate(cr, math.pi / 2 + math.rad(angle))

		pattern = cairo_pattern_create_radial(0, 0, radius * 0.95, 0, 0, radius)
		cairo_pattern_add_color_stop_rgba(pattern, 0, one4all_cairo.rgb2rgba(colour, alpha))
		cairo_pattern_add_color_stop_rgba(pattern, 1, 0, 0, 0, 0)


		local function coords()
			local x = radius * math.cos(two_pi * start)
			local y = radius * math.sin(two_pi * start)
			return x, y
		end

		cairo_move_to(cr, 0, 0)
		cairo_line_to(cr, coords())
		if not reverse then
			cairo_arc(cr, 0, 0, radius, two_pi * start, two_pi * stop)
		else
			cairo_arc_negative(cr, 0, 0, radius, two_pi * start, two_pi * stop)
		end
		cairo_line_to(cr, 0, 0)
		cairo_close_path(cr)

		cairo_set_source(cr, pattern)
		cairo_pattern_destroy(pattern)
		cairo_fill(cr)
		cairo_set_source(cr, pattern)


		if str then
			local extents = cairo_text_extents_t

			cairo_rotate(cr, (start + (stop - start) / 2) * two_pi + math.pi)

			cairo_set_source_rgba(cr, 1, 1, 1, alpha)
			cairo_select_font_face(cr, "Sans",
			CAIRO_FONT_SLANT_NORMAL,
			CAIRO_FONT_WEIGHT_NORMAL)

			cairo_set_font_size(cr, radius * 0.10)

			cairo_move_to (cr, -radius + radius * 0.075, radius * 0.03)
			cairo_show_text(cr, str)
		end

		cairo_restore(cr)
	end

	if draw_bg then	draw_dat_pie(0, 1, radius, bg_colour, 1) end
	local p = 0
	local f = t[1]['value'] / 100
	for i in pairs(t) do
		local v = t[i]['value']
		local str = string.format('%.1f %s', v, t[i]['name'])
		v = v / 100
		if shade then
			draw_dat_pie(p, p + v, radius * 0.94, fg_colour, v / p, str)
		else
			draw_dat_pie(p, p + v, radius * 0.94, fg_colour, 1, str)
		end
		p = p + v
	end
end


-- Compatibility: Lua-5.1
-- thanks to Lua users wiki for this
function M.split(str, pat)
	local t = {}  -- NOTE: use {n = 0} in Lua-5.0
	local fpat = "(.-)" .. pat
	local last_end = 1
	local s, e, cap = str:find(fpat, 1)
	while s do
		if s ~= 1 or cap ~= "" then
			table.insert(t, cap)
		end
		last_end = e+1
		s, e, cap = str:find(fpat, last_end)
	end
	if last_end <= #str then
		cap = str:sub(last_end)
		table.insert(t, cap)
	end
	return t
end

function M.tablify_top(t)
	local ret = {}
	for i in pairs(t) do
		table.insert(ret, {
			value=tonumber(string.match(t[i], '(%d+%.%d+)')),
			name=string.match(t[i], '%d+%.%d+ ([%w_]+)')
		})
	end
	return ret
end



--[[
###############################################################################
###                            MODULE MAIN LOOP                             ###
###############################################################################
--]]
function M.main()
	local function pie_me(pt)
		local str = 
		local sort_key = 
		if pt['name'] == 'top' then
			sort_key = 'cpu'
		elseif pt['name'] == 'top_mem' then
			sort_key = 'mem'
		elseif pt['name'] == 'top_io' then
			sort_key = 'io_perc'
		else
			print('Invalid top type')
			return
		end
		for i=1,10 do
			str = str .. string.format('${%s %s %i} ${%s name %i}\n', pt['name'], sort_key, i, pt['name'], i)
		end
		str = conky_parse(str)
		local tbl = M.split(str, '\n')
		tbl = M.tablify_top(tbl)
		if tonumber(conky_parse("${updates}")) < 10 then return end
		M.draw_pie(tbl, pt)
	end

	for i in pairs(settings_table) do
		pie_me(settings_table[i])
	end
end
Personal tools
Namespaces
Variants
Actions
Navigation
English
Français
Toolbox