Wednesday, August 8, 2012

Conky Lua Scripts (main.lua)

I have this saved in ~/.conky/main.lua. It handles pretty much everything except checking my e-mail.
do
	home = os.getenv("HOME");
	dofile(home .. "/.conky/queue.lua")
	dofile(home .. "/.conky/color.lua")
	fs_output = {}
	-- first argument is the number of lines to log
	-- remaining arguments are file names
	function conky_startup(...)
		linecount = tonumber(table.remove(arg, 1))
		log = Queue.new(linecount) -- queue of lines of log files
		
		filecount = #arg -- number of log files
		filenames = {} -- their names
		filehandles = {} -- the handles

		-- log each file with a different color 
		-- equal spacing around the color wheel (colors as different as possible)
		filecolors = {} 
		local dH = 360 / filecount
		for i = 0, filecount - 1 do
			table.insert(filecolors, string.format("${color %06x}", hsv2rgb(i * dH, 0.7, 1)))
		end

		for i,filename in ipairs(arg) do
			table.insert(filenames, filename)
			handle = io.open(filename)
			handle:seek("end");
			table.insert(filehandles, handle)
		end
	end
	function filllog()
		for i,handle in ipairs(filehandles) do
			for line in handle:lines() do
				Queue.enqueue(log, filecolors[i] .. line)
			end
		end
	end
	function conky_printlog()
		filllog()
		local output = "${color grey}Watching "
		for i,filename in ipairs(filenames) do
			if (i < #filenames) then
				output = output .. filecolors[i] .. filename .. "${color grey}, "
			else
				output = output .. filecolors[i] .. filename .. "${color grey}:\n"
			end
		end
		for i = log.last, log.first, -1 do 
			output = output .. log[i] .. "\n"
		end
		return output
	end
--	function conky_cpus(ncpus)
--		local output = {};
--		for i = 1,ncpus do 
--			output[i] = string.format("%s %s", cpuinfo(i, 501), cpuinfo(i, 502))
--		end
--		return table.concat(output, "\n")
--	end
	function conky_cpupairinfo(cpuN1, cpuN2)
		return string.format("%s${alignr}%s", cpuinfo(cpuN1, 800), cpuinfo(cpuN2, 800));
	end
	function cpuinfo(cpuN, width)
		local usage = conky_parse(string.format("${cpu cpu%d}", cpuN))
		local color = percent2rb(usage)
		return string.format(
			"${color grey}CPU%d (${freq_g %d} GHz):${color %06x}% 3d%% ${cpubar cpu%d 6, %d}", 
			cpuN, cpuN, color, usage, cpuN, width
			)
	end
	function conky_meminfo()
		local usage = conky_parse(string.format("${memperc}"))
		local colorram = percent2rg(usage);
		usage = conky_parse(string.format("${swapperc}"))
		local colorswap = percent2rg(usage);

		return string.format(
			"${color grey}RAM:${color %06x} $mem/$memmax ${membar 6, 800}${alignr}${color grey}Swap:${color %06x} $swap/$swapmax ${swapbar 6, 800}",
			colorram, colorswap
			)
	end
	function conky_batteryinfo()
		local perc = conky_parse(string.format("${battery_percent}", cpuN))
		local color = percent2rb(100 - tonumber(perc));
		return string.format("${color %06x}${battery_bar}", color)
	end
	function split(s,re)
		local i1 = 1
		local ls = {}
		local append = table.insert
		if not re then re = '%s+' end
		if re == '' then return {s} end
		while true do
			local i2,i3 = s:find(re,i1)
			if not i2 then
				local last = s:sub(i1)
				if last ~= '' then append(ls,last) end
				if #ls == 1 and ls[1] == '' then
					return {}
				else
					return ls
				end
			end
			append(ls, s:sub(i1,i2-1))
			i1 = i3+1
		end
	end
	function conky_buildfilesystems(width)
		local mounts = { };
		for line in io.lines("/proc/mounts") do
			local elems = split(line)
			if 	elems[3] ~= "sysfs" and		elems[3] ~= "proc" and
				elems[3] ~= "devtmpfs" and 	elems[3] ~= "tmpfs" and
				elems[3] ~= "rpc_pipefs" and	elems[3] ~= "fusectl" and
				elems[3] ~= "devpts" and	elems[3] ~= "binfmt_misc"
			then
				mounts[elems[2]] = elems[3]
			end
		end
		local entries = {}
		local i = 1
		for mount,fstype in pairs(mounts) do 
			local used = conky_parse(string.format("${fs_used_perc %s}", mount))
			local color = percent2rg(used)
			local frac = conky_parse(string.format("${fs_used %s}/${fs_size %s}", mount, mount))
			local heading = string.format("%s (%s)", mount, fstype)
			entries[i] = string.format(
				"${color grey}% 25s ${color %06x} % 15s ${fs_bar 6,%d %s}", 
				heading, color, frac, width, mount
			)
			i = i + 1
		end
		table.sort(entries)
		fs_output = {}
		local halfcount = #entries / 2;
		for i = 1,math.floor(halfcount) do
			fs_output[i] = string.format("%s${alignr}%s", entries[i], entries[i + math.ceil(halfcount)])
		end
		i = math.ceil(halfcount)
		if i > math.floor(halfcount) then
			fs_output[i] = entries[i]
		end
		return ""
	end
	function conky_filesystemline(line)
		local n = tonumber(line)
		if n <= #fs_output then return fs_output[n]
		else return ""
		end
	end
	function conky_filesystemnumlines()
		return tostring(#fs_output)
	end
	function conky_coloredtop(nprocs, ncols)
		local nrows = math.ceil(nprocs / ncols)
		local output = { }
		
		output[1] = "${color grey}Processes:$color $processes    ${color grey}Running:$color $running_processes"
		
		local header = { }
		for i = 1, ncols do
			header[i] = "${color grey}№ Name               PID   CPU%   MEM%"
		end
		output[2] = table.concat(header, "                               ")

		for j = 3, nrows + 2 do
			local line = { }
			for i = 1, ncols do
				local pn = (i - 1) * nrows + j - 2
				if pn > 10 then break end
				local usage = conky_parse(string.format("${top cpu %d}", pn))
				local color = percent2rb(usage)

				line[i] = string.format("${color %06x}%d ${top name %d} ${top pid %d} ${top cpu %d} ${top mem %d}", color, pn, pn, pn, pn, pn)
			end
			output[j] = table.concat(line, "${color grey}                               ");
		end

		return table.concat(output, "\n")
	end
end

No comments:

Post a Comment