Skip to content

Commit

Permalink
[breaking change: fix lcpz#550] moves away from global variables
Browse files Browse the repository at this point in the history
- going forward, there are a couple of ways to access state from
  the settings callbacks
  - `settings` functions should always take in `widget` and `now` as the
    arguments.  so the signature is `settings = function(widget, now)`
  - use `foo.widget` and `foo.now` to access the awesome widget and
    current state (respectively) of `foo` widget
- weather has been done in lcpz#549
- sysload's state is now in a `now` table.  access the 1, 5 and 15 min
  load averages via `now[1]`, `now[5]`, and `now[15]`
- the bat and contrib/tp_smapi widgets have NOT been migrated as i do
  not have a laptop to verify behaviors
  • Loading branch information
razamatan committed Jan 20, 2023
1 parent 16b26a8 commit 3336106
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 141 deletions.
9 changes: 4 additions & 5 deletions widget/alsa.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function factory(args)
args = args or {}
local alsa = { widget = args.widget or wibox.widget.textbox() }
local timeout = args.timeout or 5
local settings = args.settings or function() end
local settings = args.settings or function(widget, now) end

alsa.cmd = args.cmd or "amixer"
alsa.channel = args.channel or "Master"
Expand All @@ -38,10 +38,9 @@ local function factory(args)
local l,s = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
l = tonumber(l)
if alsa.last.level ~= l or alsa.last.status ~= s then
volume_now = { level = l, status = s }
widget = alsa.widget
settings()
alsa.last = volume_now
alsa.now = { level = l, status = s }
settings(alsa.widget, alsa.now)
alsa.last = alsa.now
end
end)
end
Expand Down
6 changes: 3 additions & 3 deletions widget/alsabar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ local function factory(args)
args = args or {}

local timeout = args.timeout or 5
local settings = args.settings or function() end
local settings = args.settings or function(widget, now) end
local width = args.width or 63
local height = args.height or 1
local margins = args.margins or 1
Expand Down Expand Up @@ -95,12 +95,12 @@ local function factory(args)
alsabar.bar.color = alsabar.colors.unmute
end

volume_now = {
alsabar.now = {
level = alsabar._current_level,
status = alsabar._playback
}

settings()
settings(alsabar.bar, alsabar.now)

if type(callback) == "function" then callback() end
end
Expand Down
37 changes: 18 additions & 19 deletions widget/contrib/moc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ local function factory(args)
local cover_size = args.cover_size or 100
local default_art = args.default_art or ""
local followtag = args.followtag or false
local settings = args.settings or function() end
local settings = args.settings or function(widget, now) end

moc_notification_preset = { title = "Now playing", timeout = 6 }
local moc_notification_preset = { title = "Now playing", timeout = 6 }

helpers.set_map("current moc track", nil)

function moc.update()
helpers.async("mocp -i", function(f)
moc_now = {
moc.now = {
state = "N/A",
file = "N/A",
artist = "N/A",
Expand All @@ -47,25 +47,24 @@ local function factory(args)

for line in string.gmatch(f, "[^\n]+") do
for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
if k == "State" then moc_now.state = v
elseif k == "File" then moc_now.file = v
elseif k == "Artist" then moc_now.artist = escape_f(v)
elseif k == "SongTitle" then moc_now.title = escape_f(v)
elseif k == "Album" then moc_now.album = escape_f(v)
elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
elseif k == "TotalTime" then moc_now.total = escape_f(v)
if k == "State" then moc.now.state = v
elseif k == "File" then moc.now.file = v
elseif k == "Artist" then moc.now.artist = escape_f(v)
elseif k == "SongTitle" then moc.now.title = escape_f(v)
elseif k == "Album" then moc.now.album = escape_f(v)
elseif k == "CurrentTime" then moc.now.elapsed = escape_f(v)
elseif k == "TotalTime" then moc.now.total = escape_f(v)
end
end
end

moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc_now.artist,
moc_now.album, moc_now.total, moc_now.title)
widget = moc.widget
settings()
moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc.now.artist,
moc.now.album, moc.now.total, moc.now.title)
settings(moc.widget, moc.now)

if moc_now.state == "PLAY" then
if moc_now.title ~= helpers.get_map("current moc track") then
helpers.set_map("current moc track", moc_now.title)
if moc.now.state == "PLAY" then
if moc.now.title ~= helpers.get_map("current moc track") then
helpers.set_map("current moc track", moc.now.title)

if followtag then moc_notification_preset.screen = focused() end

Expand All @@ -76,14 +75,14 @@ local function factory(args)
replaces_id = moc.id,
}

local path = string.format("%s/%s", music_dir, string.match(moc_now.file, ".*/"))
local path = string.format("%s/%s", music_dir, string.match(moc.now.file, ".*/"))
local cover = string.format("find '%s' -maxdepth 1 -type f | egrep -i -m1 '%s'", path, cover_pattern)
helpers.async({ shell, "-c", cover }, function(current_icon)
common.icon = current_icon:gsub("\n", "")
moc.id = naughty.notify(common).id
end)
end
elseif moc_now.state ~= "PAUSE" then
elseif moc.now.state ~= "PAUSE" then
helpers.set_map("current moc track", nil)
end
end)
Expand Down
10 changes: 4 additions & 6 deletions widget/cpu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local function factory(args)

local cpu = { core = {}, widget = args.widget or wibox.widget.textbox() }
local timeout = args.timeout or 2
local settings = args.settings or function() end
local settings = args.settings or function(widget, now) end

function cpu.update()
-- Read the amount of time the CPUs have spent performing
Expand Down Expand Up @@ -60,11 +60,9 @@ local function factory(args)
end
end

cpu_now = cpu.core
cpu_now.usage = cpu_now[0].usage
widget = cpu.widget

settings()
cpu.now = cpu.core
cpu.now.usage = cpu.now[0].usage
settings(cpu.widget, cpu.now)
end

helpers.newtimer("cpu", timeout, cpu.update)
Expand Down
17 changes: 8 additions & 9 deletions widget/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ local function factory(args)
local partition = args.partition
local threshold = args.threshold or 99
local showpopup = args.showpopup or "on"
local settings = args.settings or function() end
local settings = args.settings or function(widget, now) end

fs.followtag = args.followtag or false
fs.notification_preset = args.notification_preset
Expand All @@ -73,7 +73,7 @@ local function factory(args)

local function update_synced()
local pathlen = 10
fs_now = {}
fs.now = {}

local notifypaths = {}
for _, mount in ipairs(Gio.unix_mounts_get()) do
Expand All @@ -89,15 +89,15 @@ local function factory(args)
if size > 0 then
local units = math.floor(math.log(size)/math.log(1024))

fs_now[path] = {
fs.now[path] = {
units = fs.units[units],
percentage = math.floor(100 * used / size), -- used percentage
size = size / math.pow(1024, units),
used = used / math.pow(1024, units),
free = free / math.pow(1024, units)
}

if fs_now[path].percentage > 0 then -- don't notify unused file systems
if fs.now[path].percentage > 0 then -- don't notify unused file systems
notifypaths[#notifypaths+1] = path

if #path > pathlen then
Expand All @@ -108,15 +108,14 @@ local function factory(args)
end
end

widget = fs.widget
settings()
settings(fs.widget, fs.now)

if partition and fs_now[partition] and fs_now[partition].percentage >= threshold then
if partition and fs.now[partition] and fs.now[partition].percentage >= threshold then
if not helpers.get_map(partition) then
naughty.notify {
preset = naughty.config.presets.critical,
title = "Warning",
text = string.format("%s is above %d%% (%d%%)", partition, threshold, fs_now[partition].percentage)
text = string.format("%s is above %d%% (%d%%)", partition, threshold, fs.now[partition].percentage)
}
helpers.set_map(partition, true)
else
Expand All @@ -128,7 +127,7 @@ local function factory(args)
local notifytable = { [1] = string.format(fmt, "path", "used", "free", "size") }
fmt = "\n%-" .. tostring(pathlen) .. "s %3s%%\t%6.2f\t%6.2f %s"
for _, path in ipairs(notifypaths) do
notifytable[#notifytable+1] = string.format(fmt, path, fs_now[path].percentage, fs_now[path].free, fs_now[path].size, fs_now[path].units)
notifytable[#notifytable+1] = string.format(fmt, path, fs.now[path].percentage, fs.now[path].free, fs.now[path].size, fs.now[path].units)
end

fs.notification_preset.text = tconcat(notifytable)
Expand Down
15 changes: 7 additions & 8 deletions widget/imap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ local function factory(args)
local is_plain = args.is_plain or false
local followtag = args.followtag or false
local notify = args.notify or "on"
local settings = args.settings or function() end
local settings = args.settings or function(widget, now) end

local head_command = "curl --connect-timeout 3 -fsm 3"
local request = "-X 'STATUS INBOX (MESSAGES RECENT UNSEEN)'"

if not server or not mail or not password then return end

mail_notification_preset = {
local mail_notification_preset = {
icon = helpers.icons_dir .. "mail.png",
position = "top_left"
}
Expand Down Expand Up @@ -65,14 +65,13 @@ local function factory(args)
head_command, server, port, mail, password, request)

helpers.async(curl, function(f)
imap_now = { ["MESSAGES"] = 0, ["RECENT"] = 0, ["UNSEEN"] = 0 }
imap.now = { ["MESSAGES"] = 0, ["RECENT"] = 0, ["UNSEEN"] = 0 }

for s,d in f:gmatch("(%w+)%s+(%d+)") do imap_now[s] = tonumber(d) end
mailcount = imap_now["UNSEEN"] -- backwards compatibility
widget = imap.widget
for s,d in f:gmatch("(%w+)%s+(%d+)") do imap.now[s] = tonumber(d) end

settings()
settings(imap.widget, imap.now)

local mailcount = imap.now["UNSEEN"]
if notify == "on" and mailcount and mailcount >= 1 and mailcount > helpers.get_map(mail) then
if followtag then mail_notification_preset.screen = awful.screen.focused() end
naughty.notify {
Expand All @@ -81,7 +80,7 @@ local function factory(args)
}
end

helpers.set_map(mail, imap_now["UNSEEN"])
helpers.set_map(mail, imap.now["UNSEEN"])
end)

end
Expand Down
27 changes: 13 additions & 14 deletions widget/mem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@ local function factory(args)

local mem = { widget = args.widget or wibox.widget.textbox() }
local timeout = args.timeout or 2
local settings = args.settings or function() end
local settings = args.settings or function(widget, now) end

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

mem_now.used = mem_now.total - mem_now.free - mem_now.buf - mem_now.cache - mem_now.srec
mem_now.swapused = mem_now.swap - mem_now.swapf
mem_now.perc = math.floor(mem_now.used / mem_now.total * 100)
mem.now.used = mem.now.total - mem.now.free - mem.now.buf - mem.now.cache - mem.now.srec
mem.now.swapused = mem.now.swap - mem.now.swapf
mem.now.perc = math.floor(mem.now.used / mem.now.total * 100)

widget = mem.widget
settings()
settings(mem.widget, mem.now)
end

helpers.newtimer("mem", timeout, mem.update)
Expand Down
Loading

0 comments on commit 3336106

Please sign in to comment.