Dump nixos config after scrubing

This commit is contained in:
Mariano Uvalle 2025-05-03 23:42:03 -07:00
commit 5fa4c76c24
854 changed files with 30072 additions and 0 deletions

View file

@ -0,0 +1,162 @@
## awesome.battery-widget
Battery indicator widget for awesome window manager.
![Screenshot](/battery-widget/screenshot.png?raw=true "Screenshot")
Displays status information from `/sys/class/power_supply`.
### Dependencies
Optionally, in order to receive status updates, you will also need `acpid`:
```bash
pacman -S acpid
systemctl enable --now acpid
```
### Usage
In order to add a battery widget to your wibox, you have to import the module
and then instanciate a widget with the desired options like this:
```lua
local deficient = require("deficient")
-- instanciate widget
local battery_widget = deficient.battery_widget {
-- pass options here
}
-- add to wibox
s.mywibox:setup {
...,
{ -- Right widgets
...,
battery_widget,
},
}
```
If you pass an adapter name using the `adapter = "..."` option, a widget for
that specific battery adapter will be instanciated. If the `adapter` option is
not specified, the call will return a table containing widgets for each of the
battery adapters in `/sys/class/power_supply`. In that case if there are no
batteries an empty table will be returned and no error will occur on machines
without batteries.
### Options
The behaviour and appearance of the widget can be tweaked using a few options.
This is an example using all available options:
```lua
battery_widget {
ac = "AC",
adapter = "BAT0",
ac_prefix = "AC: ",
battery_prefix = "Bat: ",
percent_colors = {
{ 25, "red" },
{ 50, "orange"},
{999, "green" },
},
listen = true,
timeout = 10,
widget_text = "${AC_BAT}${color_on}${percent}%${color_off}",
widget_font = "Deja Vu Sans Mono 16",
tooltip_text = "Battery ${state}${time_est}\nCapacity: ${capacity_percent}%",
alert_threshold = 5,
alert_timeout = 0,
alert_title = "Low battery !",
alert_text = "${AC_BAT}${time_est}",
alert_icon = "~/Downloads/low_battery_icon.png",
warn_full_battery = true,
full_battery_icon = "~/Downloads/full_battery_icon.png",
}
```
`adapter`
The name of the directory entry in `/sys/class/power_supply` corresponding to the requested battery adapter.
`ac`
The name of the directory entry in `/sys/class/power_supply` corresponding to your AC status.
`ac_prefix`
The prefix to populate `${AC_BAT}` when your computer is using ac power. If your font supports unicode characters, you could use "🔌".
`battery_prefix`
The prefix to populate `${AC_BAT}` when your computer is using battery power. If your font supports unicode characters, you could use "🔋". Can also be configured as a table like `percent_colors` to show different prefixes at different battery percentages.
`percent_colors` (`limits` for backwards compatibility)
The colors that the percentage changes to, as well as the upper-bound limit of when it will change. Ex. `{100, "green"}` means any percentage lower than 100 is colored green.
`listen`
Tells the widget to listen to updates via `acpi_listen`. When an event is fired, the widget updates.
`timeout`
The time interval that the widget waits before it updates itself, in seconds.
`widget_text`, `tooltip_text`
The text which shows up on the toolbar and when you highlight the widget, respectively. Please refer to function `battery_widget:update()` for other interpolatable variables.
`widget_font`
The font description used for the widget text, for instance "Deja Vu Sans Mono 16". If this is empty or unspecified, the default font will be used.
`alert_threshold`
The percentage used as the maximum value at which an alert will be generated, `-1` to disable alerts. Once the alert is dismissed (or expired) it will not show up again until the battery has been charging.
`alert_timeout`
The time after which the alert expire, `0` for no timeout.
`alert_title`, `alert_text`, `alert_icon`
The text which shows up on the alert notification, respectively the title, body text and image path.
`warn_full_battery`, boolean
Whether a notification should be displayed when the battery gets fully charged.
`full_battery_icon`
Path to the image, which should be shown as part of the notification when battery gets fully charged (depends on `warn_full_battery`).
### Usage Examples
Percentage tables can be used for `ac_prefix`, `battery_prefix`, and `percent_colors` to show different things depending on the battery charge level, e.g.:
```lua
battery_widget {
-- Show different prefixes when charging on AC
ac_prefix = {
{ 25, "not charged" },
{ 50, "1/4 charged" },
{ 75, "2/4 charged" },
{ 95, "3/4 charged" },
{100, "fully charged" },
},
-- Show a visual indicator of charge level when on battery power
battery_prefix = {
{ 25, "#--- "},
{ 50, "##-- "},
{ 75, "###- "},
{100, "#### "},
}
}
```
`ac_prefix`, `battery_prefix`, and `widget_text` can be further customized with spans to specify colors or fonts, e.g.:
```lua
battery_widget {
-- Use different colors for ac_prefix and battery_prefix
ac_prefix = '<span color="red">AC: </span>',
battery_prefix = '<span color="green">Bat: </span>',
-- Use a bold font for both prefixes (overrides widget_font)
widget_text = '<span font="Deja Vu Sans Bold 16">${AC_BAT}</span>${color_on}${percent}%${color_off}'
}
```

View file

@ -0,0 +1,306 @@
-- Battery widget
local awful = require("awful")
local gears = require("gears")
local wibox = require("wibox")
local naughty = require("naughty")
local timer = gears.timer or timer
local watch = awful.spawn and awful.spawn.with_line_callback
------------------------------------------
-- Private utility functions
------------------------------------------
local tolower = string.lower
local function file_exists(command)
local f = io.open(command)
if f then f:close() end
return f and true or false
end
local function readfile(command)
local file = io.open(command)
if not file then return nil end
local text = file:read('*all')
file:close()
return text
end
local function color_tags(color)
if color
then return '<span color="' .. color .. '">', '</span>'
else return '', ''
end
end
local function round(value)
return math.floor(value + 0.5)
end
local function trim(s)
if not s then return nil end
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function read_trim(filename)
return trim(readfile(filename)) or ""
end
local function substitute(template, context)
if type(template) == "string" then
return (template:gsub("%${([%w_]+)}", function(key)
return tostring(context[key] or "Err!")
end))
else
-- function / functor:
return template(context)
end
end
local function lookup_by_limits(limits, value)
if type(limits) == "table" then
local last = nil
if value then
for k, v in ipairs(limits) do
if (value <= v[1]) then
return v[2]
end
last = v[2]
end
end
return last
else
return limits
end
end
------------------------------------------
-- Battery widget interface
------------------------------------------
local battery_widget = {}
local sysfs_names = {
charging = {
present = "present",
state = "status",
rate = "current_now",
charge = "charge_now",
capacity = "charge_full",
design = "charge_full_design",
percent = "capacity",
},
discharging = {
present = "present",
state = "status",
rate = "power_now",
charge = "energy_now",
capacity = "energy_full",
design = "energy_full_design",
percent = "capacity"
},
}
function battery_widget:new(args)
if args.adapter then
return setmetatable({}, {__index = self}):init(args)
end
-- creates an empty container wibox, which can be added to your panel even if its empty
local widgets = { layout = wibox.layout.fixed.horizontal }
local batteries, mains, usb, ups = self:discover()
local ac = mains[1] or usb[1] or ups[1]
for i, adapter in ipairs(batteries) do
local _args = setmetatable({adapter = adapter, ac = ac}, {__index = args})
table.insert(widgets, self(_args).widget)
end
return widgets
end
function battery_widget:discover()
local pow = "/sys/class/power_supply/"
local adapters = { Battery = {}, UPS = {}, Mains = {}, USB = {} }
for adapter in io.popen("ls -1 " .. pow):lines() do
local type = read_trim(pow .. adapter .. "/type")
table.insert(adapters[type], adapter)
end
return adapters.Battery, adapters.Mains, adapters.USB, adapters.UPS
end
function battery_widget:init(args)
self.ac = args.ac or "AC"
self.adapter = args.adapter or "BAT0"
self.ac_prefix = args.ac_prefix or "AC: "
self.battery_prefix = args.battery_prefix or "Bat: "
self.percent_colors = args.percent_colors or args.limits or {
{ 25, "red" },
{ 50, "orange"},
{999, "green" },
}
self.widget_text = args.widget_text or (
"${AC_BAT}${color_on}${percent}%${color_off}")
self.tooltip_text = args.tooltip_text or (
"Battery ${state}${time_est}\nCapacity: ${capacity_percent}%")
self.alert_threshold = args.alert_threshold or 5
self.alert_timeout = args.alert_timeout or 0
self.alert_title = args.alert_title or "Low battery !"
self.alert_text = args.alert_text or "${AC_BAT}${time_est}"
self.alert_icon = args.alert_icon or nil
self.widget = wibox.widget.textbox()
self.widget.font = args.widget_font
self.tooltip = awful.tooltip({objects={self.widget}})
self.warn_full_battery = args.warn_full_battery
self.full_battery_icon = args.full_battery_icon or nil
self.widget:buttons(awful.util.table.join(
awful.button({ }, 1, function() self:update() end),
awful.button({ }, 3, function() self:update() end)
))
self.timer = timer({ timeout = args.timeout or 10 })
self.timer:connect_signal("timeout", function() self:update() end)
self.timer:start()
self:update()
if (args.listen or args.listen == nil) and watch then
self.listener = watch("acpi_listen", {
stdout = function(line) self:update() end,
})
awesome.connect_signal("exit", function()
awesome.kill(self.listener, awesome.unix_signal.SIGTERM)
end)
end
return self
end
function battery_widget:get_state()
local pow = "/sys/class/power_supply/"
local ac = pow .. self.ac
local bat = pow .. self.adapter
local sysfs = (file_exists(bat.."/"..sysfs_names.charging.rate)
and sysfs_names.charging
or sysfs_names.discharging)
-- If there is no battery on this machine.
if not sysfs.state then return nil end
-- return value
local r = {
state = tolower (read_trim(bat.."/"..sysfs.state)),
present = tonumber(read_trim(bat.."/"..sysfs.present)),
rate = tonumber(read_trim(bat.."/"..sysfs.rate)),
charge = tonumber(read_trim(bat.."/"..sysfs.charge)),
capacity = tonumber(read_trim(bat.."/"..sysfs.capacity)),
design = tonumber(read_trim(bat.."/"..sysfs.design)),
percent = tonumber(read_trim(bat.."/"..sysfs.percent)),
}
r.ac_state = tonumber(read_trim(ac.."/online"))
if r.state == "unknown" then
r.state = "charged"
end
if r.percent == nil and r.charge and r.capacity then
r.percent = round(r.charge * 100 / r.capacity)
end
return r
end
function battery_widget:update()
local ctx = self:get_state()
-- If there is no battery on this machine.
if not ctx then return nil end
-- AC/battery prefix
ctx.AC_BAT = (ctx.ac_state == 1
and lookup_by_limits(self.ac_prefix, ctx.percent)
or lookup_by_limits(self.battery_prefix, ctx.percent)
or "Err!")
-- Colors
ctx.color_on, ctx.color_off = color_tags(
lookup_by_limits(self.percent_colors, ctx.percent))
-- estimate time
ctx.charge_dir = 0 -- +1|0|-1 -> charging|static|discharging
ctx.time_left = nil -- time until charging/discharging complete
ctx.time_text = ""
ctx.time_est = ""
if ctx.rate and ctx.rate ~= 0 then
if not ctx.state or ctx.state == "discharging" then
ctx.charge_dir = -1
ctx.time_left = ctx.charge / ctx.rate
elseif ctx.state == "charging" then
ctx.charge_dir = 1
ctx.time_left = (ctx.capacity - ctx.charge) / ctx.rate
end
end
if ctx.time_left then
ctx.hours = math.floor((ctx.time_left))
ctx.minutes = math.floor((ctx.time_left - ctx.hours) * 60)
if ctx.hours > 0
then ctx.time_text = ctx.hours .. "h " .. ctx.minutes .. "m"
else ctx.time_text = ctx.minutes .. "m"
end
ctx.time_est = ": " .. ctx.time_text .. " remaining"
end
-- capacity text
if ctx.capacity and ctx.design then
ctx.capacity_percent = round(ctx.capacity/ctx.design*100)
end
-- for use in functions
ctx.obj = self
-- update text
self.widget:set_markup(substitute(self.widget_text, ctx))
self.tooltip:set_text(substitute(self.tooltip_text, ctx))
-- low battery notification
if naughty then
if (ctx.state == "discharging" and
ctx.percent and ctx.percent <= self.alert_threshold) then
self:notify(substitute(self.alert_title, ctx),
substitute(self.alert_text, ctx),
self.alert_icon)
elseif ctx.state == "full" and self.warn_full_battery then
self:notify('Battery Full!', 'Remove power cord', self.full_battery_icon)
else
if self.alert then
naughty.destroy(
self.alert,
naughty.notificationClosedReason.dismissedByCommand)
self.alert = nil
end
end
end
end
function battery_widget:notify(title, text, icon)
if self.alert then
naughty.replace_text(self.alert, title, text)
else
self.alert = naughty.notify({
title = title,
text = text,
icon = icon,
preset = naughty.config.presets.critical,
timeout = self.alert_timeout
})
end
end
return setmetatable(battery_widget, {
__call = battery_widget.new,
})

View file

@ -0,0 +1 @@
battery-widget.lua

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB