-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconky.lua
144 lines (127 loc) · 3.96 KB
/
conky.lua
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
local heightBar = 0
local heightGraph = 0
local widthBarThread = 0
local widthGraphNet = 0
local hwmonNameCpu = ''
function conky_format(format, number)
return string.format(format, conky_parse(number))
end
function splitCsv(vals, fromConky)
local tab = {}
if fromConky then
vals = conky_parse(vals)
end
for s in string.gmatch(vals, "[^,]+") do
table.insert(tab, s)
end
return tab
end
yellow = "#ffff%02x"
red = "#ff%02x%02x"
p = 1 / 100
-- %02x -> x: hex, 2: 2 chars, 0: default char, %: string.format insert
-- example 255=ff or 0=00
-- keep conky beginning for other usecases
function conky_colorPercentage(percentage)
local perc = conky_parse(percentage) * p
local calc = math.floor(255 - perc * 255)
return string.format("${color " .. yellow .. "}", calc, calc, calc)
end
function execShRetRes(cmd)
local handle = io.popen(cmd)
local res = string.format("%s", handle:read("*a"))
handle:close()
return res
end
function conky_setSettings(newHeightBar, newHeightGraph, newWidthBarThread, newWidthGraphNet, newHwmonNameCpu)
-- sizes
heightBar = conky_parse(newHeightBar)
heightGraph = conky_parse(newHeightGraph)
widthBarThread = conky_parse(newWidthBarThread)
widthGraphNet = conky_parse(newWidthGraphNet)
-- hwmon
hwmonNameCpu = newHwmonNameCpu
return ""
end
nproc = tonumber(execShRetRes("nproc"))
cpuName = execShRetRes([[lscpu | grep "Model name:" | sed 's/.*:[ ]\+//g']])
function getCpuValues()
local i = 0
local parse_cpus = ""
for i = 1, nproc, 1 do
parse_cpus = parse_cpus .. string.format("${cpu cpu%i},", i)
end
parse_cpus = parse_cpus .. "${cpu cpu0}"
return conky_parse(parse_cpus)
end
function conky_getCpu()
local i = 0
local cpuP = splitCsv(getCpuValues(), true)
local cpu0 = cpuP[nproc + 1]
-- /sys/class/hwmon/hwmon*
local out = cpuName ..
[[${hwmon ]] .. hwmonNameCpu .. [[ temp 1}C]] .. conky_colorPercentage(cpu0) .. [[${alignr}${freq} MHz ]] .. string.format("%3.0f", cpu0) .. [[%
${color}${cpugraph cpu0 ]] .. heightGraph .. [[}
Threads
]]
for i = 1, nproc, 4 do
local to = math.min(i + 3, nproc)
local title = true
out = out .. string.format([[${color}%02i-%02i: ]], i, to)
for j = i, to, 1 do
out = out .. string.format("%s${cpubar cpu%i %i,%f} %3.0f%% ", conky_colorPercentage(cpuP[j]), j, heightBar, widthBarThread, cpuP[j])
end
out = out .. [[${color}
]]
end
out = string.gsub(out, ".$", "")
return out
end
function getNet(name)
local out = [[<ifname>: ${addr <ifname>}
▼ ${downspeed <ifname>} ${alignr}▲ ${upspeed <ifname>}
${downspeedgraph <ifname> <height>,<width>} ${alignr}${upspeedgraph <ifname> <height>,<width>}
∑ ${totaldown <ifname>} ${alignr}∑ ${totalup <ifname>}]]
out = string.gsub(out, "<ifname>", name)
out = string.gsub(out, "<height>", heightGraph)
out = string.gsub(out, "<width>", widthGraphNet)
return out
end
nets = splitCsv(execShRetRes([[ip link | grep -oPz '(?<=: )(enp|wlp|usb).*(?=:)' | tr '\0' ',']]))
function conky_net()
local res = ""
for _, n in pairs(nets) do
res = res .. getNet(n) .. "\n\n"
end
return res
end
-- needed for lua_graph usage below
function conky_retGpuUtil()
return gpuUtil
end
function conky_retGpuMem()
return gpuMem
end
d = 1 / 1024
function conky_gpuInfo()
local result = execShRetRes([[ \
nvidia-smi \
--query-gpu=gpu_name,memory.used,memory.total,utilization.gpu,clocks.sm,temperature.gpu \
--format=csv,nounits,noheader]])
local tab = splitCsv(result)
if tab[1] == nil then
return ''
end
local memUsed = tonumber(tab[2]) * d
local memTotal = tonumber(tab[3]) * d
local memPerc = memUsed / memTotal * 100
gpuUtil = tab[4]
gpuMem = memPerc
local ostr = string.format([[NVIDIA %s
%iC ${alignr}%iMHz %3i%%
${lua_graph conky_retGpuUtil <height>}
GPU RAM ${alignr}%0.2fGiB / %0.2fGiB %3.0f%%
${lua_graph conky_retGpuMem <height>}]], tab[1], tab[6], tab[5], tab[4], memUsed, memTotal, memPerc)
ostr = string.gsub(ostr, "<height>", heightGraph)
return ostr
end