scripts/flowerfetch.lua
2023-06-28 16:25:32 -04:00

119 lines
2.9 KiB
Lua
Executable file

#!/usr/bin/luajit
local lfs = require("lfs")
local luadir = arg[0]:match("@?(.*/)");
-- Clear
print("\27[H\27[2J\27[3J")
-- Print out flower from text file
local flower = io.open(luadir .. "flower.txt", "r");
if (flower ~= nil) then
print(flower:read("*a"));
flower:close();
else
print("Error: Missing ASCII flower");
os.exit(-1);
end
function Getfile(loc, perm, splitstr, strip)
local table = {};
local gotfile = io.open(loc, perm);
if (gotfile ~= nil) then
for line in gotfile:lines() do
for lineid, lineval in line:gmatch(splitstr) do
table[lineid] = lineval:gsub(strip, "");
end
end
gotfile:close();
end
return table;
end
function Getline(loc)
local item = io.open(loc, 'r')
if (item ~= nil) then
local str = item:read('*l')
item:close()
return str
else
return ""
end
end
-- Get battery stuff
local battery = ""
local batteries = ""
for filename in lfs.dir("/sys/class/power_supply") do
if filename ~= "." and filename ~= ".." and filename ~= "AC" then
battery = Getline("/sys/class/power_supply/" .. filename .. "/capacity")
batteries = string.format("[ %s %s%% ] ", filename, battery) .. batteries
end
end
-- Get wifi link quality
local winit = {}
local wifistring = ""
local wifi = io.open('/proc/net/wireless', 'r')
if (wifi ~= nil) then
for line in wifi:lines() do
table.insert(winit, line);
end
-- what the fuck is lua regex.
local wifiregex = "%s(.*):.*%s(%d+)%."
-- Assemble wifi into printable set of strings.
for item = 1, #winit - 2 do
for wif, wqual in winit[item + 2]:gmatch(wifiregex) do
wifistring = wifistring .. string.format("[ %s %s%% ] ", wif, wqual)
end
end
wifi:close()
end
-- Get individual elements
local model = Getline('/sys/devices/virtual/dmi/id/product_version');
local kernel = Getline('/proc/sys/kernel/osrelease');
local shell = os.getenv("SHELL"):gsub(".*/", "");
local osrel = Getfile("/etc/os-release", "r", "(.*)=(.*)", "\"");
local uptime = Getline("/proc/uptime"):gsub("%..*", "");
local up_d = math.floor(uptime / (3600 * 24))
local up_h = math.floor((uptime % (3600 * 24)) / 3600)
local up_m = math.floor(((uptime % (3600 * 24)) % 3600) / 60)
local upstring = ""
if up_d > 0 then
upstring = string.format("%sd %sh %sm", up_d, up_h, up_m)
elseif up_h > 0 then
upstring = string.format("%sh %sm", up_d, up_h, up_m)
else
upstring = string.format("%sm", up_d, up_h, up_m)
end
local output = {
{ "model", model },
{ "distro", osrel['PRETTY_NAME'] },
{ "kernel", kernel },
{ "shell", shell },
{ "uptime", upstring },
{ "bat", batteries },
{ "wifi", wifistring },
};
local count = 0;
for _, value in pairs(output) do
local color = "";
if count == 0 then
color = "\27[34m";
count = count + 1;
else
color = "\27[36m";
count = 0;
end
print(string.format("%s%6s \27[37m-\27[37m %s", color, value[1], value[2]))
end
print()