-
Notifications
You must be signed in to change notification settings - Fork 7
/
scaling.lua
57 lines (48 loc) · 1.88 KB
/
scaling.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
-- This script adds basic scaling support to conky. It uses a new table
-- "conky.sizes", which should have vertical resolutions (e.g. 1080) as keys
-- and tables of sizes as values. Sizes can be specified as key, value pairs
-- which can then be used in the config through the "<key>" placeholder (this
-- is configurable). This script then chooses the correct sizes depending on
-- the resolution of the screen. If the resolution is not supported (e.g. not
-- in the "conky.sizes" table), the autoscale algorithm is used which
-- calculates the sizes to use from the closest specified resolution. For
-- reference the primary screen is used. "Xrandr" must be installed for this to
-- work as intended.
local scaling = {
format = "<%s>",
resolution = tonumber(io.popen("xrandr"):read("*a"):match("primary %d+x(%d+)")) or 1080
}
function scaling:autoscale()
local fallback, autosizes = math.huge, {}
for resolution, _ in pairs(conky.sizes) do
if math.abs(self.resolution - resolution) < math.abs(self.resolution - fallback) then
fallback = resolution
end
end
for id, value in pairs(conky.sizes[fallback]) do
local scaled = (self.resolution * value) / fallback
autosizes[id] = math.floor(scaled + 0.5)
end
return autosizes
end
function scaling:apply(str)
for id, value in pairs(conky.sizes[self.resolution] or self:autoscale()) do
str = string.gsub(str, string.format(self.format, id), value)
end
return tonumber(str) or str
end
function scaling:scale(item)
if conky.sizes then
if type(item) == "table" then
for index, value in pairs(item) do
if type(value) == "string" then
item[index] = self:apply(value)
end
end
elseif type(item) == "string" then
item = self:apply(item)
end
end
return item
end
_G.conky = setmetatable({}, {__newindex = function(_, key, value) rawset(conky, key, scaling:scale(value)) end})