lockhl: allow setting a default state
This allows e.g. to invert the warning for numlock
This commit is contained in:
parent
f2595cacca
commit
821d35c8df
2 changed files with 53 additions and 30 deletions
43
lockhl.lua
43
lockhl.lua
|
@ -6,23 +6,38 @@ local beautiful = require("beautiful")
|
||||||
local lockhl = { bg_lock = beautiful.bg_urgent, bg_normal = beautiful.bg_normal }
|
local lockhl = { bg_lock = beautiful.bg_urgent, bg_normal = beautiful.bg_normal }
|
||||||
local target_wibars = {}
|
local target_wibars = {}
|
||||||
|
|
||||||
function lockhl:setup(wibars, bg_lock, bg_normal)
|
function lockhl:setup(wibars, bg_lock, bg_normal, defaults)
|
||||||
target_wibars = wibars
|
target_wibars = wibars
|
||||||
if bg_lock then self.bg_lock = bg_lock end
|
if bg_lock then
|
||||||
if bg_normal then self.bg_normal = bg_normal end
|
self.bg_lock = bg_lock
|
||||||
|
end
|
||||||
|
if bg_normal then
|
||||||
|
self.bg_normal = bg_normal
|
||||||
|
end
|
||||||
|
|
||||||
|
if defaults ~= nil then
|
||||||
|
for lock, normal_state in pairs(defaults) do
|
||||||
|
print("lock: " .. lock .. " normal_state: " .. tostring(normal_state))
|
||||||
|
lockhl:on_lock(lock, normal_state)
|
||||||
|
end
|
||||||
|
end
|
||||||
return lockhl
|
return lockhl
|
||||||
end
|
end
|
||||||
|
|
||||||
local function check_lock(lock, cb)
|
local function check_lock(lock, cb)
|
||||||
awful.spawn.with_line_callback(
|
awful.spawn.with_line_callback(
|
||||||
'bash -c "sleep 0.2; xset q | grep -Po \\"' .. lock .. ' Lock:\\\\s*\\\\K(on|off)\\" 2>&1"',
|
'bash -c "sleep 0.2; xset q | grep -Po \\"' .. lock .. ' Lock:\\\\s*\\\\K(on|off)\\" 2>&1"',
|
||||||
{ stdout = function(output) cb(output == "on") end }
|
{
|
||||||
|
stdout = function(output)
|
||||||
|
cb(output == "on")
|
||||||
|
end,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function lockhl:target_color(is_on, lock)
|
function lockhl:target_color(is_on, lock)
|
||||||
if is_on then
|
if is_on then
|
||||||
if type(self.bg_lock) == 'table' then
|
if type(self.bg_lock) == "table" then
|
||||||
return self.bg_lock[lock]
|
return self.bg_lock[lock]
|
||||||
else
|
else
|
||||||
return self.bg_lock
|
return self.bg_lock
|
||||||
|
@ -32,15 +47,23 @@ function lockhl:target_color(is_on, lock)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function lockhl:on_lock(lock)
|
local function xor(a, b)
|
||||||
|
return not (not a == not b)
|
||||||
|
end
|
||||||
|
|
||||||
|
function lockhl:on_lock(lock, normal_state)
|
||||||
check_lock(lock, function(is_on)
|
check_lock(lock, function(is_on)
|
||||||
local newbg = self:target_color(is_on, lock)
|
local newbg = self:target_color(xor(is_on, normal_state), lock)
|
||||||
for _, bar in pairs(target_wibars) do
|
for _, bar in pairs(target_wibars) do
|
||||||
bar.bg = newbg
|
bar.bg = newbg
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable(lockhl, { __call = function(_, lock)
|
return setmetatable(lockhl, {
|
||||||
return function() lockhl:on_lock(lock) end
|
__call = function(_, lock, normal_state)
|
||||||
end })
|
return function()
|
||||||
|
lockhl:on_lock(lock, normal_state)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
|
@ -18,7 +18,7 @@ for s in screen do
|
||||||
table.insert(wibars, s.rightwibar)
|
table.insert(wibars, s.rightwibar)
|
||||||
end
|
end
|
||||||
local lockhl = require("lockhl")
|
local lockhl = require("lockhl")
|
||||||
lockhl:setup(wibars, "#F2C740")
|
lockhl:setup(wibars, "#F2C740", require("beautiful").bg_normal, { Num = true, Caps = false })
|
||||||
|
|
||||||
local function mpdserver(host)
|
local function mpdserver(host)
|
||||||
return function()
|
return function()
|
||||||
|
@ -189,7 +189,7 @@ local myglobalkeys = awful.util.table.join(
|
||||||
awful.key({}, "XF86Display", mb.grabf({ keymap = displaymap, name = "Rotate" })),
|
awful.key({}, "XF86Display", mb.grabf({ keymap = displaymap, name = "Rotate" })),
|
||||||
|
|
||||||
awful.key({ modkey }, "e", binder.spawn("rofi -show emoji")),
|
awful.key({ modkey }, "e", binder.spawn("rofi -show emoji")),
|
||||||
awful.key({}, "Num_Lock", lockhl("Num")),
|
awful.key({}, "Num_Lock", lockhl("Num", true)),
|
||||||
awful.key({}, "Caps_Lock", lockhl("Caps"))
|
awful.key({}, "Caps_Lock", lockhl("Caps"))
|
||||||
|
|
||||||
--}}}
|
--}}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue