Added settings.lua

This commit is contained in:
2023-01-25 20:10:58 +01:00
parent 80cc950271
commit 5cf452e053
38 changed files with 3880 additions and 3450 deletions

View File

@@ -10,9 +10,9 @@
--]]
local wrequire = require("lain.helpers").wrequire
local wrequire = require('lain.helpers').wrequire
local setmetatable = setmetatable
local widget = { _NAME = "lain.widget.contrib" }
local widget = { _NAME = 'lain.widget.contrib' }
return setmetatable(widget, { __index = wrequire })

View File

@@ -5,93 +5,103 @@
--]]
local helpers = require("lain.helpers")
local shell = require("awful.util").shell
local focused = require("awful.screen").focused
local escape_f = require("awful.util").escape
local naughty = require("naughty")
local wibox = require("wibox")
local os = os
local string = string
local helpers = require('lain.helpers')
local shell = require('awful.util').shell
local focused = require('awful.screen').focused
local escape_f = require('awful.util').escape
local naughty = require('naughty')
local wibox = require('wibox')
local os = os
local string = string
-- MOC audio player
-- lain.widget.contrib.moc
local function factory(args)
args = args or {}
args = args or {}
local moc = { widget = args.widget or wibox.widget.textbox() }
local timeout = args.timeout or 2
local music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
local cover_pattern = args.cover_pattern or "*\\.(jpg|jpeg|png|gif)$"
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 moc = { widget = args.widget or wibox.widget.textbox() }
local timeout = args.timeout or 2
local music_dir = args.music_dir or os.getenv('HOME') .. '/Music'
local cover_pattern = args.cover_pattern or '*\\.(jpg|jpeg|png|gif)$'
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
moc_notification_preset = { title = "Now playing", timeout = 6 }
moc_notification_preset = { title = 'Now playing', timeout = 6 }
helpers.set_map("current moc track", nil)
helpers.set_map('current moc track', nil)
function moc.update()
helpers.async("mocp -i", function(f)
moc_now = {
state = "N/A",
file = "N/A",
artist = "N/A",
title = "N/A",
album = "N/A",
elapsed = "N/A",
total = "N/A"
}
function moc.update()
helpers.async('mocp -i', function(f)
moc_now = {
state = 'N/A',
file = 'N/A',
artist = 'N/A',
title = 'N/A',
album = 'N/A',
elapsed = 'N/A',
total = 'N/A',
}
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)
end
end
end
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)
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)
widget = moc.widget
settings()
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
if followtag then
moc_notification_preset.screen = focused()
end
local common = {
preset = moc_notification_preset,
icon = default_art,
icon_size = cover_size,
replaces_id = moc.id,
}
local common = {
preset = moc_notification_preset,
icon = default_art,
icon_size = cover_size,
replaces_id = moc.id,
}
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
helpers.set_map("current moc track", nil)
end
end)
end
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
helpers.set_map('current moc track', nil)
end
end)
end
moc.timer = helpers.newtimer("moc", timeout, moc.update, true, true)
moc.timer = helpers.newtimer('moc', timeout, moc.update, true, true)
return moc
return moc
end
return factory

View File

@@ -6,36 +6,36 @@
--]]
local async = require("lain.helpers").async
local awful = require("awful")
local async = require('lain.helpers').async
local awful = require('awful')
local execute = os.execute
local type = type
local type = type
-- Redshift
-- lain.widget.contrib.redshift
local redshift = { active = false, pid = nil }
function redshift.start()
execute("pkill redshift")
awful.spawn.with_shell("redshift -x") -- clear adjustments
redshift.pid = awful.spawn.with_shell("redshift")
redshift.active = true
if type(redshift.update_fun) == "function" then
redshift.update_fun(redshift.active)
end
execute('pkill redshift')
awful.spawn.with_shell('redshift -x') -- clear adjustments
redshift.pid = awful.spawn.with_shell('redshift')
redshift.active = true
if type(redshift.update_fun) == 'function' then
redshift.update_fun(redshift.active)
end
end
function redshift.toggle()
async({ awful.util.shell, "-c", string.format("ps -p %d -o pid=", redshift.pid) }, function(f)
if f and #f > 0 then -- redshift is running
-- Sending -USR1 toggles redshift (See project website)
execute("pkill -USR1 redshift")
redshift.active = not redshift.active
else -- not started or killed, (re)start it
redshift.start()
end
redshift.update_fun(redshift.active)
end)
async({ awful.util.shell, '-c', string.format('ps -p %d -o pid=', redshift.pid) }, function(f)
if f and #f > 0 then -- redshift is running
-- Sending -USR1 toggles redshift (See project website)
execute('pkill -USR1 redshift')
redshift.active = not redshift.active
else -- not started or killed, (re)start it
redshift.start()
end
redshift.update_fun(redshift.active)
end)
end
-- Attach to a widget
@@ -44,11 +44,15 @@ end
-- @param fun: Function to be run each time redshift is toggled (optional).
-- Use it to update widget text or icons on status change.
function redshift.attach(widget, fun)
redshift.update_fun = fun or function() end
if not redshift.pid then redshift.start() end
if widget then
widget:buttons(awful.util.table.join(awful.button({}, 1, function () redshift.toggle() end)))
end
redshift.update_fun = fun or function() end
if not redshift.pid then
redshift.start()
end
if widget then
widget:buttons(awful.util.table.join(awful.button({}, 1, function()
redshift.toggle()
end)))
end
end
return redshift

View File

@@ -5,88 +5,92 @@
--]]
local helpers = require("lain.helpers")
local markup = require("lain.util").markup
local awful = require("awful")
local naughty = require("naughty")
local mouse = mouse
local helpers = require('lain.helpers')
local markup = require('lain.util').markup
local awful = require('awful')
local naughty = require('naughty')
local mouse = mouse
-- Taskwarrior notification
-- lain.widget.contrib.task
local task = {}
function task.hide()
if not task.notification then return end
naughty.destroy(task.notification)
task.notification = nil
if not task.notification then
return
end
naughty.destroy(task.notification)
task.notification = nil
end
function task.show(scr)
task.notification_preset.screen = task.followtag and awful.screen.focused() or scr or 1
task.notification_preset.screen = task.followtag and awful.screen.focused() or scr or 1
helpers.async({ awful.util.shell, "-c", task.show_cmd }, function(f)
local widget_focused = true
helpers.async({ awful.util.shell, '-c', task.show_cmd }, function(f)
local widget_focused = true
if mouse.current_widgets then
widget_focused = false
for _,v in ipairs(mouse.current_widgets) do
if task.widget == v then
widget_focused = true
break
end
end
end
if mouse.current_widgets then
widget_focused = false
for _, v in ipairs(mouse.current_widgets) do
if task.widget == v then
widget_focused = true
break
end
end
end
if widget_focused then
task.hide()
task.notification = naughty.notify {
preset = task.notification_preset,
title = "task next",
text = markup.font(task.notification_preset.font,
awful.util.escape(f:gsub("\n*$", "")))
}
end
end)
if widget_focused then
task.hide()
task.notification = naughty.notify({
preset = task.notification_preset,
title = 'task next',
text = markup.font(task.notification_preset.font, awful.util.escape(f:gsub('\n*$', ''))),
})
end
end)
end
function task.prompt()
awful.prompt.run {
prompt = task.prompt_text,
textbox = awful.screen.focused().mypromptbox.widget,
exe_callback = function(t)
helpers.async(t, function(f)
naughty.notify {
preset = task.notification_preset,
title = t,
text = markup.font(task.notification_preset.font,
awful.util.escape(f:gsub("\n*$", "")))
}
end)
end,
history_path = awful.util.getdir("cache") .. "/history_task"
}
awful.prompt.run({
prompt = task.prompt_text,
textbox = awful.screen.focused().mypromptbox.widget,
exe_callback = function(t)
helpers.async(t, function(f)
naughty.notify({
preset = task.notification_preset,
title = t,
text = markup.font(task.notification_preset.font, awful.util.escape(f:gsub('\n*$', ''))),
})
end)
end,
history_path = awful.util.getdir('cache') .. '/history_task',
})
end
function task.attach(widget, args)
args = args or {}
args = args or {}
task.show_cmd = args.show_cmd or "task next"
task.prompt_text = args.prompt_text or "Enter task command: "
task.followtag = args.followtag or false
task.notification_preset = args.notification_preset
task.widget = widget
task.show_cmd = args.show_cmd or 'task next'
task.prompt_text = args.prompt_text or 'Enter task command: '
task.followtag = args.followtag or false
task.notification_preset = args.notification_preset
task.widget = widget
if not task.notification_preset then
task.notification_preset = {
font = "Monospace 10",
icon = helpers.icons_dir .. "/taskwarrior.png"
}
end
if not task.notification_preset then
task.notification_preset = {
font = 'Monospace 10',
icon = helpers.icons_dir .. '/taskwarrior.png',
}
end
if widget then
widget:connect_signal("mouse::enter", function () task.show() end)
widget:connect_signal("mouse::leave", function () task.hide() end)
end
if widget then
widget:connect_signal('mouse::enter', function()
task.show()
end)
widget:connect_signal('mouse::leave', function()
task.hide()
end)
end
end
return task

View File

@@ -6,142 +6,159 @@
--]]
local helpers = require("lain.helpers")
local focused = require("awful.screen").focused
local naughty = require("naughty")
local wibox = require("wibox")
local string = string
local type = type
local helpers = require('lain.helpers')
local focused = require('awful.screen').focused
local naughty = require('naughty')
local wibox = require('wibox')
local string = string
local type = type
-- ThinkPad battery infos and widget creator
-- http://www.thinkwiki.org/wiki/Tp_smapi
-- lain.widget.contrib.tp_smapi
local function factory(apipath)
local tp_smapi = {
path = apipath or "/sys/devices/platform/smapi"
}
local tp_smapi = {
path = apipath or '/sys/devices/platform/smapi',
}
function tp_smapi.get(batid, feature)
return helpers.first_line(string.format("%s/%s/%s", tp_smapi.path, batid or "BAT0", feature or ""))
end
function tp_smapi.get(batid, feature)
return helpers.first_line(string.format('%s/%s/%s', tp_smapi.path, batid or 'BAT0', feature or ''))
end
function tp_smapi.installed(batid)
return tp_smapi.get(batid, "installed") == "1"
end
function tp_smapi.installed(batid)
return tp_smapi.get(batid, 'installed') == '1'
end
function tp_smapi.status(batid)
return tp_smapi.get(batid, "state")
end
function tp_smapi.status(batid)
return tp_smapi.get(batid, 'state')
end
function tp_smapi.percentage(batid)
return tp_smapi.get(batid, "remaining_percent")
end
function tp_smapi.percentage(batid)
return tp_smapi.get(batid, 'remaining_percent')
end
-- either running or charging time
function tp_smapi.time(batid)
local status = tp_smapi.status(batid)
local mins_left = tp_smapi.get(batid, string.match(string.lower(status), "discharging") and "remaining_running_time" or "remaining_charging_time")
if not string.find(mins_left, "^%d+") then return "N/A" end
return string.format("%02d:%02d", math.floor(mins_left / 60), mins_left % 60) -- HH:mm
end
-- either running or charging time
function tp_smapi.time(batid)
local status = tp_smapi.status(batid)
local mins_left = tp_smapi.get(
batid,
string.match(string.lower(status), 'discharging') and 'remaining_running_time' or 'remaining_charging_time'
)
if not string.find(mins_left, '^%d+') then
return 'N/A'
end
return string.format('%02d:%02d', math.floor(mins_left / 60), mins_left % 60) -- HH:mm
end
function tp_smapi.hide()
if not tp_smapi.notification then return end
naughty.destroy(tp_smapi.notification)
tp_smapi.notification = nil
end
function tp_smapi.hide()
if not tp_smapi.notification then
return
end
naughty.destroy(tp_smapi.notification)
tp_smapi.notification = nil
end
function tp_smapi.show(batid, seconds, scr)
if not tp_smapi.installed(batid) then return end
function tp_smapi.show(batid, seconds, scr)
if not tp_smapi.installed(batid) then
return
end
local mfgr = tp_smapi.get(batid, "manufacturer") or "no_mfgr"
local model = tp_smapi.get(batid, "model") or "no_model"
local chem = tp_smapi.get(batid, "chemistry") or "no_chem"
local status = tp_smapi.get(batid, "state")
local time = tp_smapi.time(batid)
local msg
local mfgr = tp_smapi.get(batid, 'manufacturer') or 'no_mfgr'
local model = tp_smapi.get(batid, 'model') or 'no_model'
local chem = tp_smapi.get(batid, 'chemistry') or 'no_chem'
local status = tp_smapi.get(batid, 'state')
local time = tp_smapi.time(batid)
local msg
if status and status ~= "idle" then
msg = string.format("[%s] %s %s", status, time ~= "N/A" and time or "unknown remaining time",
string.lower(status):gsub(" ", ""):gsub("\n", "") == "charging" and " until charged" or " remaining")
else
msg = "On AC power"
end
if status and status ~= 'idle' then
msg = string.format(
'[%s] %s %s',
status,
time ~= 'N/A' and time or 'unknown remaining time',
string.lower(status):gsub(' ', ''):gsub('\n', '') == 'charging' and ' until charged' or ' remaining'
)
else
msg = 'On AC power'
end
tp_smapi.hide()
tp_smapi.notification = naughty.notify {
title = string.format("%s: %s %s (%s)", batid, mfgr, model, chem),
text = msg,
timeout = type(seconds) == "number" and seconds or 0,
screen = scr or focused()
}
end
tp_smapi.hide()
tp_smapi.notification = naughty.notify({
title = string.format('%s: %s %s (%s)', batid, mfgr, model, chem),
text = msg,
timeout = type(seconds) == 'number' and seconds or 0,
screen = scr or focused(),
})
end
function tp_smapi.create_widget(args)
args = args or {}
function tp_smapi.create_widget(args)
args = args or {}
local pspath = args.pspath or "/sys/class/power_supply/"
local batteries = args.batteries or (args.battery and {args.battery}) or {}
local timeout = args.timeout or 30
local settings = args.settings or function() end
local pspath = args.pspath or '/sys/class/power_supply/'
local batteries = args.batteries or (args.battery and { args.battery }) or {}
local timeout = args.timeout or 30
local settings = args.settings or function() end
if #batteries == 0 then
helpers.line_callback("ls -1 " .. pspath, function(line)
local bstr = string.match(line, "BAT%w+")
if bstr then batteries[#batteries + 1] = bstr end
end)
end
if #batteries == 0 then
helpers.line_callback('ls -1 ' .. pspath, function(line)
local bstr = string.match(line, 'BAT%w+')
if bstr then
batteries[#batteries + 1] = bstr
end
end)
end
local all_batteries_installed = true
local all_batteries_installed = true
for _, battery in ipairs(batteries) do
if not tp_smapi.installed(battery) then
naughty.notify {
preset = naughty.config.critical,
title = "tp_smapi: error while creating widget",
text = string.format("battery %s is not installed", battery)
}
all_batteries_installed = false
break
end
end
for _, battery in ipairs(batteries) do
if not tp_smapi.installed(battery) then
naughty.notify({
preset = naughty.config.critical,
title = 'tp_smapi: error while creating widget',
text = string.format('battery %s is not installed', battery),
})
all_batteries_installed = false
break
end
end
if not all_batteries_installed then return end
if not all_batteries_installed then
return
end
tpbat = {
batteries = batteries,
widget = args.widget or wibox.widget.textbox()
}
tpbat = {
batteries = batteries,
widget = args.widget or wibox.widget.textbox(),
}
function tpbat.update()
tpbat_now = {
n_status = {},
n_perc = {},
n_time = {},
status = "N/A"
}
function tpbat.update()
tpbat_now = {
n_status = {},
n_perc = {},
n_time = {},
status = 'N/A',
}
for i = 1, #batteries do
tpbat_now.n_status[i] = tp_smapi.status(batteries[i]) or "N/A"
tpbat_now.n_perc[i] = tp_smapi.percentage(batteries[i])
tpbat_now.n_time[i] = tp_smapi.time(batteries[i]) or "N/A"
for i = 1, #batteries do
tpbat_now.n_status[i] = tp_smapi.status(batteries[i]) or 'N/A'
tpbat_now.n_perc[i] = tp_smapi.percentage(batteries[i])
tpbat_now.n_time[i] = tp_smapi.time(batteries[i]) or 'N/A'
if not tpbat_now.n_status[i]:lower():match("full") then
tpbat_now.status = tpbat_now.n_status[i]
end
end
if not tpbat_now.n_status[i]:lower():match('full') then
tpbat_now.status = tpbat_now.n_status[i]
end
end
widget = tpbat.widget -- backwards compatibility
settings()
end
widget = tpbat.widget -- backwards compatibility
settings()
end
helpers.newtimer("thinkpad-batteries", timeout, tpbat.update)
helpers.newtimer('thinkpad-batteries', timeout, tpbat.update)
return tpbat
end
return tpbat
end
return tp_smapi
return tp_smapi
end
return factory