forked from junekept/luafetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluafetch.lua
More file actions
146 lines (119 loc) · 4.48 KB
/
luafetch.lua
File metadata and controls
146 lines (119 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
local config = require("config")
local Lunacolors = require("Lunacolors")
local function fileToString(fileStr)
local file = io.open(fileStr)
if file then
file:close()
io.input(fileStr)
return io.read("*a"):sub(1, -2)
else
return nil
end
end
-- Function calls for specific settings
local configStrings = {}
configStrings["user"] = function()
local username = io.popen("whoami"):read("*a"):sub(1, -2)
local hostname = fileToString("/proc/sys/kernel/hostname")
if not hostname then
hostname = "unknown"
end
return Lunacolors.format(string.format("{bold}{%s}%s", config.primaryColor, username))
.. Lunacolors.format(string.format("{%s}@", config.secondaryColor))
.. Lunacolors.format(string.format("{bold}{%s}%s", config.primaryColor, hostname))
end
configStrings["os"] = function()
local isDebian = io.popen("(ls /etc/*-release && echo yes) || echo no"):read("*a"):find("no")
if isDebian then
-- If both checks fail, then the OS is unknown
local isLinux = io.popen("(ls /etc/*_version && echo yes) || echo no"):read("*a"):find("no")
if not isLinux then
return "unknown"
end
return io.popen("cat /etc/*_version | grep \'Description\'")
:read("*a"):sub(1, -2)
:gsub('Description:.', '')
:sub(1, -2)
else
-- -3 gets rid of newline and trailing quotation mark
return io.popen("cat /etc/*-release | grep \'PRETTY_NAME\'")
:read("*a")
:gsub('PRETTY_NAME=\"', '')
:sub(1, -3)
end
end
configStrings["architecture"] = function()
return io.popen("uname -m"):read("*a"):sub(1, -2)
end
configStrings["host"] = function()
return fileToString("/sys/devices/virtual/dmi/id/product_name")
end
configStrings["kernel"] = function()
return io.popen("uname -r"):read("*a"):sub(1, -2)
end
configStrings["terminal"] = function()
-- the gsub is to remove any prefix in the file
return os.getenv("TERM"):gsub("^.*-", '')
end
configStrings["cpu"] = function()
local cpuInfo = fileToString("/proc/cpuinfo")
local cpuName, cpuMHz, coreCount
if cpuInfo then
-- get the CPU name
cpuName = cpuInfo:sub(select(1, cpuInfo:find("model name")), -1)
cpuName = cpuName:sub(1, select(2, cpuName:find('\n')) - 1)
:gsub("model name.+: ", '')
-- Get the CPU clock speed using the same method
cpuMHz = cpuInfo:sub(select(1, cpuInfo:find("cpu MHz")), -1)
-- For some stupid reason string.gsub() doesn't want to work on cpu MHz??? This is the last resort
cpuMHz = cpuMHz:sub(12, select(2, cpuMHz:find('\n')))
-- Get the number of occurences using cpuName
coreCount = select(2, cpuInfo:gsub("model name.:", ''))
if cpuName:find('@') then
cpuName = cpuName:gsub(" @.+", '')
end
else
cpuName = "unknown"
cpuMHz = "0"
coreCount = 0
end
return string.format("%s x%d @ %.3fGHz", cpuName, coreCount, tonumber(cpuMHz) / 1000)
end
configStrings["gpu"] = function()
return io.popen("glxinfo | grep -i 'OpenGL renderer string:'"):read("*a"):gsub("OpenGL renderer string: ", ''):sub(1, -2)
end
configStrings["shell"] = function()
return os.getenv("SHELL")
end
configStrings["memory"] = function()
local meminfo = fileToString("/proc/meminfo")
local memTotal, usedMem
if meminfo then
memTotal = meminfo:sub(select(1, meminfo:find("MemTotal:")), -1)
memTotal = memTotal:sub(1, select(2, memTotal:find('\n')))
:gsub("MemTotal:%s+", '')
:gsub(" kB\n", '')
usedMem = io.popen("free | grep Mem"):read("*a"):gsub("[%a%p]+%s+%d+%s+", '')
local i, j = usedMem:find("%d+")
usedMem = usedMem:sub(i, j)
else
memTotal = 0
usedMem = 0
end
return string.format("%.0f/%.0fMiB", tonumber(usedMem) / 1024, tonumber(memTotal) / 1024)
end
configStrings["lua"] = function()
return _VERSION
end
for _, item in ipairs(config.order) do
if(configStrings[item]) then
if config.showRaw[item] then
print(configStrings[item]())
else
print(Lunacolors.format(string.format("{bold}{%s}%s%s", config.primaryColor, config.prettyOrder[item] or item, config.seperator))
.. Lunacolors.format(string.format("{%s}%s", config.secondaryColor, configStrings[item]())))
end
else
print(Lunacolors.format(item))
end
end