Skip to content

Commit

Permalink
feat: new component battery
Browse files Browse the repository at this point in the history
  • Loading branch information
sontungexpt committed Nov 13, 2023
1 parent fa400c4 commit d8bb1d4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lua/sttusline/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,13 @@ end

M.start_timer = function(opts)
if glob_timer == nil then glob_timer = vim.loop.new_timer() end
glob_timer:start(1000, 1000, vim.schedule_wrap(function() M.run(opts) end))
glob_timer:start(0, 1000, vim.schedule_wrap(function() M.run(opts) end))
end

M.start_sub_timer = function(opts, component, index, timing)
if comp_timers[index] == nil then comp_timers[index] = vim.loop.new_timer() end
comp_timers[index]:start(
timing,
0,
timing,
vim.schedule_wrap(function()
if statusline_hidden then return end
Expand Down
96 changes: 96 additions & 0 deletions lua/sttusline/components/battery.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
local fn = vim.fn
local colors = require("sttusline.utils.color")

return {
name = "battery",
timing = true,
configs = {
icons = {
charging = {
"󰢟",
"󰢜",
"󰂆",
"󰂇",
"󰂈",
"󰢝",
"󰂉",
"󰢞",
"󰂊",
"󰂋",
"󰂅",
},
discharging = {
"󰂎",
"󰁺",
"󰁻",
"󰁼",
"󰁽",
"󰁾",
"󰁿",
"󰂀",
"󰂁",
"󰂂",
"󰁹",
},
},
},
colors = {
fg = colors.green,
},
space = function()
local bat = fn.system("ls /sys/class/power_supply/ | grep BAT"):match("(.-)%s*$") or ""
local bat_dir = "/sys/class/power_supply/" .. bat .. "/"

local read_battery_file = function(filename)
local f = io.open(bat_dir .. filename, "r")
if not f then return "" end
local content = f:read("*all")
f:close()
return content:match("(.-)%s*$")
end
return {
get_status = function() return read_battery_file("status") end,
get_capacity = function() return read_battery_file("capacity") end,
curr_charging_index = 0,
}
end,
update = function(configs, space)
local status = space.get_status()
local capacity = space.get_capacity()
local icon_index = math.floor(capacity / 10) + 1
local battery_color = icon_index > 8 and colors.green
or icon_index > 4 and colors.yellow
or colors.red

if status == "Charging" then
space.curr_charging_index = space.curr_charging_index == 0 and icon_index
or space.curr_charging_index < #configs.icons.charging and space.curr_charging_index + 1
or icon_index

return {
{
configs.icons.charging[space.curr_charging_index] .. " " .. capacity .. "%%",
{ fg = battery_color },
},
}
elseif status == "Discharging" then
space.curr_charging_index = 0
return {
{
configs.icons.discharging[icon_index] .. " " .. capacity .. "%%",
{ fg = battery_color },
},
}
elseif status == "Full" then
space.curr_charging_index = 0
return "󰂄 " .. capacity .. "%%"
elseif status == "Not charging" then
space.curr_charging_index = 0
return "󰁹 " .. capacity .. "%%"
else
space.curr_charging_index = 0
return "Battery: " .. capacity .. "%%"
end
end,
condition = function() return vim.loop.os_uname().sysname == "Linux" end,
}

0 comments on commit d8bb1d4

Please sign in to comment.