lua rewrite
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
local function get_system_age()
|
||||
local install_time = 1793894400 -- Choose your own install time (unix timestamp)
|
||||
local now = tonumber(util.process_get_one_line("date +%s"))
|
||||
|
||||
return util.format_time(now - install_time)
|
||||
end
|
||||
|
||||
return { name = "age", cmd = get_system_age }
|
||||
@@ -0,0 +1,64 @@
|
||||
local M = { color = {}, separator = {} }
|
||||
|
||||
-- Color definitions (ANSI escape sequences)
|
||||
|
||||
M.color.reset = "\27[0m"
|
||||
M.color.gray = "\27[38;2;102;92;84m"
|
||||
M.color.cyan = "\27[38;2;125;174;163m"
|
||||
M.color.white = "\27[38;2;235;219;178m"
|
||||
M.color.byellow_bold = "\27[1;38;2;250;189;47m"
|
||||
M.color.bwhite_bold = "\27[1;38;2;235;219;178m"
|
||||
M.color.dimyellow_italic = "\27[3;38;2;215;153;33m"
|
||||
M.color.aqua = "\27[38;2;142;192;124m"
|
||||
|
||||
-- Info field colors
|
||||
|
||||
M.color.info_name = M.color.cyan
|
||||
M.color.info_value = M.color.dimyellow_italic
|
||||
|
||||
-- Horizontal separator color (under username @ hostname)
|
||||
|
||||
M.color.horizontal_separator = M.color.dimyellow_italic
|
||||
|
||||
|
||||
-- User and hostname definitions for the header
|
||||
|
||||
M.user = require("modules/user").user
|
||||
M.hostname = require("modules/hostname").hostname
|
||||
|
||||
|
||||
-- Separator of the logo and the info fields
|
||||
|
||||
M.separator.logo_char = " | "
|
||||
M.separator.logo = M.color.gray ..
|
||||
M.separator.logo_char .. M.color.reset
|
||||
|
||||
-- Separator of the info name and value
|
||||
|
||||
M.separator.info_char = " : "
|
||||
M.separator.info = M.color.dimyellow_italic ..
|
||||
M.separator.info_char .. M.color.reset
|
||||
|
||||
-- Horizontal separator under the heading
|
||||
|
||||
M.separator.horizontal_char = "-"
|
||||
M.separator.horizontal = M.color.dimyellow_italic ..
|
||||
string.rep(
|
||||
M.separator.horizontal_char,
|
||||
(#M.user + #M.hostname + 3)
|
||||
) .. M.color.reset
|
||||
|
||||
-- Logo definition
|
||||
|
||||
M.logo = {
|
||||
" ",
|
||||
M.color.cyan .. " /\\ /\\ ",
|
||||
M.color.cyan .. " // \\ \\ ",
|
||||
M.color.cyan .. " // \\ \\ ",
|
||||
M.color.cyan .. " /// \\ \\ ",
|
||||
M.color.cyan .. " // \\ \\",
|
||||
M.color.cyan .. " \\ ",
|
||||
" "
|
||||
}
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,16 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
-- CPU modelname
|
||||
local function get_cpu_modelname()
|
||||
local name = util.grep("/proc/cpuinfo", "^model name%s*:%s*(.+)") or
|
||||
util.grep("/proc/cpuinfo", "^Hardware%s*:%s*(.+)")
|
||||
|
||||
-- Shorter modelname for Intel i3/i5/i7/i9 CPUs
|
||||
if name then
|
||||
name = name:match("([iI][3579]%-[%d]+[A-Za-z]*)") or name
|
||||
end
|
||||
|
||||
return name
|
||||
end
|
||||
|
||||
return { name = "cpu", cmd = get_cpu_modelname }
|
||||
@@ -0,0 +1,7 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
local function get_distro()
|
||||
return util.grep("/etc/os-release", "%s*NAME%s*=%s*\"(.*)\"")
|
||||
end
|
||||
|
||||
return { name = "distro", cmd = get_distro }
|
||||
@@ -0,0 +1,5 @@
|
||||
-- Reads /etc/hostname and returns the first line
|
||||
|
||||
local util = require("modules/util")
|
||||
|
||||
return { hostname = os.getenv("HOSTNAME") or util.cat_first_line("/etc/hostname") }
|
||||
@@ -0,0 +1,7 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
local function get_system_kernel()
|
||||
return util.cat_first_line("/proc/sys/kernel/osrelease")
|
||||
end
|
||||
|
||||
return { name = "kernel", cmd = get_system_kernel }
|
||||
@@ -0,0 +1,11 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
local function get_num_installed_pkgs()
|
||||
local distros = {}
|
||||
distros.alpine = "apk list --installed | wc -l"
|
||||
|
||||
-- Choose and/or add your own distro
|
||||
return util.process_get_one_line(distros.alpine)
|
||||
end
|
||||
|
||||
return { name = "pkgs", cmd = get_num_installed_pkgs }
|
||||
@@ -0,0 +1,29 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
-- RAM amount in GB
|
||||
local function get_ram_gb()
|
||||
local free, total, avail
|
||||
|
||||
util.for_line_in_file("/proc/meminfo", function(line)
|
||||
local total_match = line:match("^MemTotal%s*:%s*(.+)%s*[kK][bB]")
|
||||
local avail_match = line:match("^MemAvailable%s*:%s*(.+)%s*[kK][bB]")
|
||||
|
||||
if total_match then
|
||||
total = tonumber(total_match)
|
||||
end
|
||||
|
||||
if avail_match then
|
||||
avail = tonumber(avail_match)
|
||||
end
|
||||
|
||||
if total and avail then
|
||||
free = string.format("%.1f GB", (total - avail) / 1000000)
|
||||
end
|
||||
|
||||
return free ~= nil
|
||||
end)
|
||||
|
||||
return free
|
||||
end
|
||||
|
||||
return { name = "ram", cmd = get_ram_gb }
|
||||
@@ -0,0 +1,6 @@
|
||||
local function get_current_shell()
|
||||
local shell = os.getenv("SHELL") or "unknown"
|
||||
return shell:match("([^/]*)$")
|
||||
end
|
||||
|
||||
return { name = "shell", cmd = get_current_shell }
|
||||
@@ -0,0 +1,43 @@
|
||||
local M = { }
|
||||
|
||||
local util = require("modules/util")
|
||||
|
||||
function M.modules(modules, config_)
|
||||
local config = config_ or require("modules/config")
|
||||
local info_name_padding_amount = util.longest_info_name_length(modules)
|
||||
|
||||
M.header =
|
||||
config.logo[1] .. config.separator.logo ..
|
||||
config.color.cyan .. config.user ..
|
||||
config.color.gray .. " @ " ..
|
||||
config.color.white .. config.hostname
|
||||
|
||||
-- Optional plus above the info separator
|
||||
-- config.separator.horizontal = string.rep("-", info_name_padding_amount + 1) ..
|
||||
-- "+" .. string.rep("-", ((#config.user + 1 + #config.hostname) - info_name_padding_amount))
|
||||
|
||||
M.horizontal_separator =
|
||||
config.logo[2] .. config.separator.logo .. config.separator.horizontal
|
||||
|
||||
print(M.header)
|
||||
|
||||
print(M.horizontal_separator)
|
||||
|
||||
for i, info in ipairs(modules) do
|
||||
print(
|
||||
(config.logo[i+2] or "[! OUT OF LOGO !]") ..
|
||||
config.separator.logo ..
|
||||
|
||||
config.color.cyan .. info.name ..
|
||||
string.rep(" ", (info_name_padding_amount - #info.name)) ..
|
||||
|
||||
config.color.dimyellow_italic ..
|
||||
config.separator.info ..
|
||||
config.color.dimyellow_italic ..
|
||||
info.cmd() ..
|
||||
config.color.reset
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,5 @@
|
||||
local function get_current_terminal()
|
||||
return os.getenv("TERM") or "unknown"
|
||||
end
|
||||
|
||||
return { name = "term", cmd = get_current_terminal }
|
||||
@@ -0,0 +1,12 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
local function get_current_gtk_theme()
|
||||
local theme = util.grep(
|
||||
(os.getenv("HOME") or "~") .. "/.config/gtk-4.0/settings.ini",
|
||||
"^gtk%-theme%-name%s*=%s*(.*)"
|
||||
) or "unknown"
|
||||
|
||||
return theme:gsub("%-", " ")
|
||||
end
|
||||
|
||||
return { name = "theme", cmd = get_current_gtk_theme }
|
||||
@@ -0,0 +1,7 @@
|
||||
local util = require("modules/util")
|
||||
|
||||
local function get_system_uptime()
|
||||
return util.format_time(tonumber(util.grep("/proc/uptime", "^(%S+)")))
|
||||
end
|
||||
|
||||
return { name = "uptime", cmd = get_system_uptime }
|
||||
@@ -0,0 +1 @@
|
||||
return { user = os.getenv("USER") }
|
||||
@@ -0,0 +1,95 @@
|
||||
local M = {}
|
||||
|
||||
-- Helper functions
|
||||
|
||||
function M.format_time(time)
|
||||
local seconds = time
|
||||
local days = math.floor(seconds / 86400)
|
||||
local hours = math.floor(seconds % 86400 / 3600)
|
||||
local minutes = math.floor(seconds % 3600 / 60)
|
||||
local result = ""
|
||||
|
||||
if days > 0 then
|
||||
result = result .. days .. "d "
|
||||
end
|
||||
|
||||
if hours > 0 then
|
||||
result = result .. hours .. "h "
|
||||
end
|
||||
|
||||
if minutes > 0 then
|
||||
result = result .. minutes .. "m"
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
-- Capture the first line of an invoked system process
|
||||
-- (discards of error messages of the last command (the
|
||||
-- one after the last pipe ('|')))
|
||||
function M.process_get_one_line(proc)
|
||||
local pipe = io.popen(proc .. " 2>/dev/null", "r")
|
||||
if not pipe then return nil end
|
||||
|
||||
local data = pipe:read("*l")
|
||||
pipe:close()
|
||||
return data
|
||||
end
|
||||
|
||||
-- Get the lenght of the longest info name (for padding)
|
||||
function M.longest_info_name_length(modules)
|
||||
local longest = 0
|
||||
|
||||
for _, mod in ipairs(modules) do
|
||||
if type(mod.name) == "string" and #mod.name > longest then
|
||||
longest = #mod.name
|
||||
end
|
||||
end
|
||||
|
||||
return longest
|
||||
end
|
||||
|
||||
--[[
|
||||
-- Loop through a file and for each line perform
|
||||
-- a callback function, if it returns true, the
|
||||
-- loop can end prematurely (this is wanted)
|
||||
--]]
|
||||
function M.for_line_in_file(filename, callback)
|
||||
local file = io.open(filename, "r")
|
||||
if not file then return nil end
|
||||
|
||||
for line in file:lines() do
|
||||
if callback(line) then
|
||||
file:close()
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
file:close()
|
||||
return false
|
||||
end
|
||||
|
||||
-- filter a file line-by-line and return the given match
|
||||
function M.grep(filename, regex)
|
||||
local capture
|
||||
|
||||
M.for_line_in_file(filename, function(line)
|
||||
capture = line:match(regex)
|
||||
|
||||
return capture ~= nil
|
||||
end)
|
||||
|
||||
return capture
|
||||
end
|
||||
|
||||
-- Returns the contents of the first line in a file
|
||||
function M.cat_first_line(filename)
|
||||
local file = io.open(filename, "r")
|
||||
if not file then return nil end
|
||||
|
||||
local line = file:read("*l")
|
||||
file:close()
|
||||
return line
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,11 @@
|
||||
local function get_current_wm()
|
||||
if os.getenv("SWAYSOCK") then
|
||||
return "sway"
|
||||
elseif os.getenv("HYPRLAND_INSTANCE_SIGNATURE") then
|
||||
return "hyprland"
|
||||
else
|
||||
return "unknown"
|
||||
end
|
||||
end
|
||||
|
||||
return { name = "wm", cmd = get_current_wm }
|
||||
Reference in New Issue
Block a user