Skip to content

Commit efc2ba7

Browse files
committed
[breaking change: fix lcpz#550] moves away from global variables
- 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
1 parent f633920 commit efc2ba7

File tree

13 files changed

+129
-141
lines changed

13 files changed

+129
-141
lines changed

widget/alsa.lua

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ local function factory(args)
1818
args = args or {}
1919
local alsa = { widget = args.widget or wibox.widget.textbox() }
2020
local timeout = args.timeout or 5
21-
local settings = args.settings or function() end
21+
local settings = args.settings or function(widget, now) end
2222

2323
alsa.cmd = args.cmd or "amixer"
2424
alsa.channel = args.channel or "Master"
@@ -38,10 +38,9 @@ local function factory(args)
3838
local l,s = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
3939
l = tonumber(l)
4040
if alsa.last.level ~= l or alsa.last.status ~= s then
41-
volume_now = { level = l, status = s }
42-
widget = alsa.widget
43-
settings()
44-
alsa.last = volume_now
41+
alsa.now = { level = l, status = s }
42+
settings(alsa.widget, alsa.now)
43+
alsa.last = alsa.now
4544
end
4645
end)
4746
end

widget/alsabar.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ local function factory(args)
3333
args = args or {}
3434

3535
local timeout = args.timeout or 5
36-
local settings = args.settings or function() end
36+
local settings = args.settings or function(widget, now) end
3737
local width = args.width or 63
3838
local height = args.height or 1
3939
local margins = args.margins or 1
@@ -95,12 +95,12 @@ local function factory(args)
9595
alsabar.bar.color = alsabar.colors.unmute
9696
end
9797

98-
volume_now = {
98+
alsabar.now = {
9999
level = alsabar._current_level,
100100
status = alsabar._playback
101101
}
102102

103-
settings()
103+
settings(alsabar.bar, alsabar.now)
104104

105105
if type(callback) == "function" then callback() end
106106
end

widget/contrib/moc.lua

+18-19
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ local function factory(args)
2727
local cover_size = args.cover_size or 100
2828
local default_art = args.default_art or ""
2929
local followtag = args.followtag or false
30-
local settings = args.settings or function() end
30+
local settings = args.settings or function(widget, now) end
3131

32-
moc_notification_preset = { title = "Now playing", timeout = 6 }
32+
local moc_notification_preset = { title = "Now playing", timeout = 6 }
3333

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

3636
function moc.update()
3737
helpers.async("mocp -i", function(f)
38-
moc_now = {
38+
moc.now = {
3939
state = "N/A",
4040
file = "N/A",
4141
artist = "N/A",
@@ -47,25 +47,24 @@ local function factory(args)
4747

4848
for line in string.gmatch(f, "[^\n]+") do
4949
for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
50-
if k == "State" then moc_now.state = v
51-
elseif k == "File" then moc_now.file = v
52-
elseif k == "Artist" then moc_now.artist = escape_f(v)
53-
elseif k == "SongTitle" then moc_now.title = escape_f(v)
54-
elseif k == "Album" then moc_now.album = escape_f(v)
55-
elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
56-
elseif k == "TotalTime" then moc_now.total = escape_f(v)
50+
if k == "State" then moc.now.state = v
51+
elseif k == "File" then moc.now.file = v
52+
elseif k == "Artist" then moc.now.artist = escape_f(v)
53+
elseif k == "SongTitle" then moc.now.title = escape_f(v)
54+
elseif k == "Album" then moc.now.album = escape_f(v)
55+
elseif k == "CurrentTime" then moc.now.elapsed = escape_f(v)
56+
elseif k == "TotalTime" then moc.now.total = escape_f(v)
5757
end
5858
end
5959
end
6060

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

66-
if moc_now.state == "PLAY" then
67-
if moc_now.title ~= helpers.get_map("current moc track") then
68-
helpers.set_map("current moc track", moc_now.title)
65+
if moc.now.state == "PLAY" then
66+
if moc.now.title ~= helpers.get_map("current moc track") then
67+
helpers.set_map("current moc track", moc.now.title)
6968

7069
if followtag then moc_notification_preset.screen = focused() end
7170

@@ -76,14 +75,14 @@ local function factory(args)
7675
replaces_id = moc.id,
7776
}
7877

79-
local path = string.format("%s/%s", music_dir, string.match(moc_now.file, ".*/"))
78+
local path = string.format("%s/%s", music_dir, string.match(moc.now.file, ".*/"))
8079
local cover = string.format("find '%s' -maxdepth 1 -type f | egrep -i -m1 '%s'", path, cover_pattern)
8180
helpers.async({ shell, "-c", cover }, function(current_icon)
8281
common.icon = current_icon:gsub("\n", "")
8382
moc.id = naughty.notify(common).id
8483
end)
8584
end
86-
elseif moc_now.state ~= "PAUSE" then
85+
elseif moc.now.state ~= "PAUSE" then
8786
helpers.set_map("current moc track", nil)
8887
end
8988
end)

widget/cpu.lua

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local function factory(args)
1919

2020
local cpu = { core = {}, widget = args.widget or wibox.widget.textbox() }
2121
local timeout = args.timeout or 2
22-
local settings = args.settings or function() end
22+
local settings = args.settings or function(widget, now) end
2323

2424
function cpu.update()
2525
-- Read the amount of time the CPUs have spent performing
@@ -60,11 +60,9 @@ local function factory(args)
6060
end
6161
end
6262

63-
cpu_now = cpu.core
64-
cpu_now.usage = cpu_now[0].usage
65-
widget = cpu.widget
66-
67-
settings()
63+
cpu.now = cpu.core
64+
cpu.now.usage = cpu.now[0].usage
65+
settings(cpu.widget, cpu.now)
6866
end
6967

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

widget/fs.lua

+8-9
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ local function factory(args)
5858
local partition = args.partition
5959
local threshold = args.threshold or 99
6060
local showpopup = args.showpopup or "on"
61-
local settings = args.settings or function() end
61+
local settings = args.settings or function(widget, now) end
6262

6363
fs.followtag = args.followtag or false
6464
fs.notification_preset = args.notification_preset
@@ -73,7 +73,7 @@ local function factory(args)
7373

7474
local function update_synced()
7575
local pathlen = 10
76-
fs_now = {}
76+
fs.now = {}
7777

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

92-
fs_now[path] = {
92+
fs.now[path] = {
9393
units = fs.units[units],
9494
percentage = math.floor(100 * used / size), -- used percentage
9595
size = size / math.pow(1024, units),
9696
used = used / math.pow(1024, units),
9797
free = free / math.pow(1024, units)
9898
}
9999

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

103103
if #path > pathlen then
@@ -108,15 +108,14 @@ local function factory(args)
108108
end
109109
end
110110

111-
widget = fs.widget
112-
settings()
111+
settings(fs.widget, fs.now)
113112

114-
if partition and fs_now[partition] and fs_now[partition].percentage >= threshold then
113+
if partition and fs.now[partition] and fs.now[partition].percentage >= threshold then
115114
if not helpers.get_map(partition) then
116115
naughty.notify {
117116
preset = naughty.config.presets.critical,
118117
title = "Warning",
119-
text = string.format("%s is above %d%% (%d%%)", partition, threshold, fs_now[partition].percentage)
118+
text = string.format("%s is above %d%% (%d%%)", partition, threshold, fs.now[partition].percentage)
120119
}
121120
helpers.set_map(partition, true)
122121
else
@@ -128,7 +127,7 @@ local function factory(args)
128127
local notifytable = { [1] = string.format(fmt, "path", "used", "free", "size") }
129128
fmt = "\n%-" .. tostring(pathlen) .. "s %3s%%\t%6.2f\t%6.2f %s"
130129
for _, path in ipairs(notifypaths) do
131-
notifytable[#notifytable+1] = string.format(fmt, path, fs_now[path].percentage, fs_now[path].free, fs_now[path].size, fs_now[path].units)
130+
notifytable[#notifytable+1] = string.format(fmt, path, fs.now[path].percentage, fs.now[path].free, fs.now[path].size, fs.now[path].units)
132131
end
133132

134133
fs.notification_preset.text = tconcat(notifytable)

widget/imap.lua

+7-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ local function factory(args)
2929
local is_plain = args.is_plain or false
3030
local followtag = args.followtag or false
3131
local notify = args.notify or "on"
32-
local settings = args.settings or function() end
32+
local settings = args.settings or function(widget, now) end
3333

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

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

39-
mail_notification_preset = {
39+
local mail_notification_preset = {
4040
icon = helpers.icons_dir .. "mail.png",
4141
position = "top_left"
4242
}
@@ -65,14 +65,13 @@ local function factory(args)
6565
head_command, server, port, mail, password, request)
6666

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

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

74-
settings()
72+
settings(imap.widget, imap.now)
7573

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

84-
helpers.set_map(mail, imap_now["UNSEEN"])
83+
helpers.set_map(mail, imap.now["UNSEEN"])
8584
end)
8685

8786
end

widget/mem.lua

+13-14
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,28 @@ local function factory(args)
1818

1919
local mem = { widget = args.widget or wibox.widget.textbox() }
2020
local timeout = args.timeout or 2
21-
local settings = args.settings or function() end
21+
local settings = args.settings or function(widget, now) end
2222

2323
function mem.update()
24-
mem_now = {}
24+
mem.now = {}
2525
for line in lines("/proc/meminfo") do
2626
for k, v in gmatch(line, "([%a]+):[%s]+([%d]+).+") do
27-
if k == "MemTotal" then mem_now.total = floor(v / 1024 + 0.5)
28-
elseif k == "MemFree" then mem_now.free = floor(v / 1024 + 0.5)
29-
elseif k == "Buffers" then mem_now.buf = floor(v / 1024 + 0.5)
30-
elseif k == "Cached" then mem_now.cache = floor(v / 1024 + 0.5)
31-
elseif k == "SwapTotal" then mem_now.swap = floor(v / 1024 + 0.5)
32-
elseif k == "SwapFree" then mem_now.swapf = floor(v / 1024 + 0.5)
33-
elseif k == "SReclaimable" then mem_now.srec = floor(v / 1024 + 0.5)
27+
if k == "MemTotal" then mem.now.total = floor(v / 1024 + 0.5)
28+
elseif k == "MemFree" then mem.now.free = floor(v / 1024 + 0.5)
29+
elseif k == "Buffers" then mem.now.buf = floor(v / 1024 + 0.5)
30+
elseif k == "Cached" then mem.now.cache = floor(v / 1024 + 0.5)
31+
elseif k == "SwapTotal" then mem.now.swap = floor(v / 1024 + 0.5)
32+
elseif k == "SwapFree" then mem.now.swapf = floor(v / 1024 + 0.5)
33+
elseif k == "SReclaimable" then mem.now.srec = floor(v / 1024 + 0.5)
3434
end
3535
end
3636
end
3737

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

42-
widget = mem.widget
43-
settings()
42+
settings(mem.widget, mem.now)
4443
end
4544

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

0 commit comments

Comments
 (0)