Skip to content

Commit

Permalink
widgets fix
Browse files Browse the repository at this point in the history
  • Loading branch information
luke bonham authored and copycat-killer committed Aug 5, 2015
1 parent bc57461 commit 4bde4b5
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 88 deletions.
6 changes: 4 additions & 2 deletions helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
--]]

local debug = require("debug")
local rawget = rawget

local capi = { timer = timer }
local io = { open = io.open }
local rawget = rawget

-- Lain helper functions for internal use
-- lain.helpers
Expand Down Expand Up @@ -49,7 +51,7 @@ end
helpers.timer_table = {}

function helpers.newtimer(name, timeout, fun, nostart)
helpers.timer_table[name] = timer({ timeout = timeout })
helpers.timer_table[name] = capi.timer({ timeout = timeout })
helpers.timer_table[name]:connect_signal("timeout", fun)
helpers.timer_table[name]:start()
if not nostart then
Expand Down
21 changes: 11 additions & 10 deletions widgets/alsa.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,31 @@ local function worker(args)
alsa.widget = wibox.widget.textbox('')

function alsa.update()
local f = io.popen('amixer get ' .. channel)
local f = assert(io.popen('amixer get ' .. channel))
local mixer = f:read("*all")
f:close()

volume = {}
volume_now = {}

volume.level, volume.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
volume_now.level, volume_now.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)")

if volume.level == nil
if volume_now.level == nil
then
volume.level = 0
volume.status = "off"
volume_now.level = "0"
volume_now.status = "off"
end

if volume.status == ""
if volume_now.status == ""
then
if volume.level == 0
if volume_now.level == "0"
then
volume.status = "off"
volume_now.status = "off"
else
volume.status = "on"
volume_now.status = "on"
end
end

widget = alsa.widget
settings()
end

Expand Down
6 changes: 3 additions & 3 deletions widgets/bat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ local function worker(args)

bat.widget = wibox.widget.textbox('')

function bat.update()
function update()
bat_now = {
status = "not present",
status = "Not present",
perc = "N/A",
time = "N/A",
watt = "N/A"
Expand Down Expand Up @@ -110,7 +110,7 @@ local function worker(args)
settings()
end

newtimer("bat", timeout, bat.update)
newtimer("bat", timeout, update)

return bat.widget
end
Expand Down
20 changes: 10 additions & 10 deletions widgets/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ local setmetatable = setmetatable
-- Calendar notification
-- lain.widgets.calendar
local calendar = {}
local notification = nil
local cal_notification = nil

function calendar:hide()
if notification ~= nil then
naughty.destroy(notification)
notification = nil
if cal_notification ~= nil then
naughty.destroy(cal_notification)
cal_notification = nil
end
end

Expand Down Expand Up @@ -95,12 +95,12 @@ function calendar:show(t_out, inc_offset)
.. "</span></tt>"
f:close()

notification = naughty.notify({ text = c_text,
icon = calendar.notify_icon,
position = calendar.position,
fg = calendar.fg,
bg = calendar.bg,
timeout = tims })
cal_notification = naughty.notify({ text = c_text,
icon = calendar.notify_icon,
position = calendar.position,
fg = calendar.fg,
bg = calendar.bg,
timeout = tims })
end

function calendar:attach(widget, args)
Expand Down
4 changes: 2 additions & 2 deletions widgets/cpu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ local function worker(args)

cpu.widget = wibox.widget.textbox('')

function cpu.update()
function update()
-- Read the amount of time the CPUs have spent performing
-- different kinds of work. Read the first line of /proc/stat
-- which is the sum of all CPUs.
Expand Down Expand Up @@ -68,7 +68,7 @@ local function worker(args)
cpu.last_total = total
end

newtimer("cpu", timeout, cpu.update)
newtimer("cpu", timeout, update)

return cpu.widget
end
Expand Down
18 changes: 5 additions & 13 deletions widgets/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local setmetatable = setmetatable
local fs = {}

local notification = nil
notification_preset = { fg = beautiful.fg_normal }
fs_notification_preset = { fg = beautiful.fg_normal }

function fs:hide()
if notification ~= nil then
Expand All @@ -44,7 +44,7 @@ function fs:show(t_out)
f:close()

notification = naughty.notify({
preset = notification_preset,
preset = fs_notification_preset,
text = ws,
timeout = t_out
})
Expand All @@ -63,7 +63,7 @@ local function worker(args)

helpers.set_map("fs", false)

function fs.update()
function update()
fs_info = {}

local f = io.popen("LC_ALL=C df -kP")
Expand Down Expand Up @@ -108,20 +108,12 @@ local function worker(args)
end
end

helpers.newtimer(partition, timeout, fs.update)
helpers.newtimer(partition, timeout, update)

widget:connect_signal('mouse::enter', function () fs:show(0) end)
widget:connect_signal('mouse::leave', function () fs:hide() end)

output = {
widget = fs.widget,
show = function(t_out)
fs.update()
fs:show(t_out)
end
}

return setmetatable(output, { __index = output.widget })
return setmetatable(fs, { __index = fs.widget })
end

return setmetatable(fs, { __call = function(_, ...) return worker(...) end })
8 changes: 4 additions & 4 deletions widgets/imap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ local function worker(args)

imap.widget = wibox.widget.textbox('')

function imap.update()
notification_preset = {
function update()
mail_notification_preset = {
icon = helpers.icons_dir .. "mail.png",
position = "top_left"
}
Expand All @@ -73,13 +73,13 @@ local function worker(args)
else
nt = mail .. " has <b>" .. mailcount .. "</b> new messages"
end
naughty.notify({ preset = notification_preset, text = nt })
naughty.notify({ preset = mail_notification_preset, text = nt })
end

helpers.set_map(mail, mailcount)
end

helpers.newtimer(mail, timeout, imap.update, true)
helpers.newtimer(mail, timeout, update, true)
return imap.widget
end

Expand Down
5 changes: 2 additions & 3 deletions widgets/maildir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ local function worker(args)

maildir.widget = wibox.widget.textbox('')

function maildir.update()
function update()
-- Find pathes to mailboxes.
local p = io.popen("find " .. mailpath ..
" -mindepth 1 -maxdepth 1 -type d" ..
Expand Down Expand Up @@ -88,8 +88,7 @@ local function worker(args)
settings()
end

newtimer(mailpath, timeout, maildir.update, true)

newtimer(mailpath, timeout, update, true)
return maildir.widget
end

Expand Down
34 changes: 16 additions & 18 deletions widgets/mem.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

--[[
Licensed under GNU General Public License v2
* (c) 2013, Luke Bonham
* (c) 2010-2012, Peter Hofmann
* (c) 2010, Adrian C. <[email protected]>
* (c) 2009, Lucas de Vries <[email protected]>
Licensed under GNU General Public License v2
* (c) 2013, Luke Bonham
* (c) 2010-2012, Peter Hofmann
--]]

local newtimer = require("lain.helpers").newtimer
Expand All @@ -32,30 +30,30 @@ local function worker(args)

mem.widget = wibox.widget.textbox('')

function mem.update()
mem = {}
function update()
mem_now = {}
for line in io.lines("/proc/meminfo")
do
for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+")
do
if k == "MemTotal" then mem.total = math.floor(v / 1024)
elseif k == "MemFree" then mem.free = math.floor(v / 1024)
elseif k == "Buffers" then mem.buf = math.floor(v / 1024)
elseif k == "Cached" then mem.cache = math.floor(v / 1024)
elseif k == "SwapTotal" then mem.swap = math.floor(v / 1024)
elseif k == "SwapFree" then mem.swapf = math.floor(v / 1024)
if k == "MemTotal" then mem_now.total = math.floor(v / 1024)
elseif k == "MemFree" then mem_now.free = math.floor(v / 1024)
elseif k == "Buffers" then mem_now.buf = math.floor(v / 1024)
elseif k == "Cached" then mem_now.cache = math.floor(v / 1024)
elseif k == "SwapTotal" then mem_now.swap = math.floor(v / 1024)
elseif k == "SwapFree" then mem_now.swapf = math.floor(v / 1024)
end
end
end

used = mem.total - (mem.free + mem.buf + mem.cache)
swapused = mem.swap - mem.swapf
mem_now.used = mem_now.total - (mem_now.free + mem_now.buf + mem_now.cache)
mem_now.swapused = mem_now.swap - mem_now.swapf

widget = mem.widget
settings()
end

newtimer("mem", timeout, mem.update)
newtimer("mem", timeout, update)

return mem.widget
end
Expand Down
8 changes: 4 additions & 4 deletions widgets/mpd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ local function worker(args)

mpd.widget = wibox.widget.textbox('')

notification_preset = {
mpd_notification_preset = {
title = "Now playing",
timeout = 6
}
Expand Down Expand Up @@ -73,8 +73,8 @@ local function worker(args)

f:close()

notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
mpd_now.album, mpd_now.date, mpd_now.title)
mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
mpd_now.album, mpd_now.date, mpd_now.title)
widget = mpd.widget
settings()

Expand All @@ -87,7 +87,7 @@ local function worker(args)
os.execute(string.format("%s %q %q", mpdcover, music_dir, mpd_now.file))

mpd.id = naughty.notify({
preset = notification_preset,
preset = mpd_notification_preset,
icon = "/tmp/mpdcover.png",
replaces_id = mpd.id
}).id
Expand Down
21 changes: 11 additions & 10 deletions widgets/net.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,33 @@ local function worker(args)

helpers.set_map(iface, true)

function net.update()
function update()
net_now = {}

if iface == "" then iface = net.get_device() end

carrier = helpers.first_line('/sys/class/net/' .. iface ..
net_now.carrier = helpers.first_line('/sys/class/net/' .. iface ..
'/carrier') or "0"
state = helpers.first_line('/sys/class/net/' .. iface ..
net_now.state = helpers.first_line('/sys/class/net/' .. iface ..
'/operstate') or "down"
local now_t = helpers.first_line('/sys/class/net/' .. iface ..
'/statistics/tx_bytes') or 0
local now_r = helpers.first_line('/sys/class/net/' .. iface ..
'/statistics/rx_bytes') or 0

sent = tostring((now_t - net.last_t) / timeout / units)
sent = string.gsub(string.format('%.1f', sent), ",", ".")
net_now.sent = tostring((now_t - net.last_t) / timeout / units)
net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ",", ".")

received = tostring((now_r - net.last_r) / timeout / units)
received = string.gsub(string.format('%.1f', received), ",", ".")
net_now.received = tostring((now_r - net.last_r) / timeout / units)
net_now.received = string.gsub(string.format('%.1f', net_now.received), ",", ".")

widget = net.widget
settings()

net.last_t = now_t
net.last_r = now_r

if carrier ~= "1"
if net_now.carrier ~= "1"
then
if helpers.get_map(iface)
then
Expand All @@ -95,8 +97,7 @@ local function worker(args)
end
end

helpers.newtimer(iface, timeout, net.update)

helpers.newtimer(iface, timeout, update)
return net.widget
end

Expand Down
7 changes: 3 additions & 4 deletions widgets/sysload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ local function worker(args)

sysload.widget = wibox.widget.textbox('')

function sysload.update()
function update()
local f = io.open("/proc/loadavg")
local ret = f:read("*all")
f:close()

a, b, c = string.match(ret, "([^%s]+) ([^%s]+) ([^%s]+)")
load_1, load_5, load_15 = string.match(ret, "([^%s]+) ([^%s]+) ([^%s]+)")

widget = sysload.widget
settings()
end

newtimer("sysload", timeout, sysload.update)

newtimer("sysload", timeout, update)
return sysload.widget
end

Expand Down
Loading

0 comments on commit 4bde4b5

Please sign in to comment.