This repository has been archived by the owner on May 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ueberzug.lua
95 lines (85 loc) · 1.79 KB
/
ueberzug.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
local u = require("vimage.util")
local log = u.log
local FIFO_PATH = u.os.env("FIFO_UEBERZUG")
local SCALER = {
CROP = "crop",
DISTORT = "distort",
FIT_CONTAIN = "fit_contain",
CONTAIN = "contain",
FORCED_COVER = "forced_cover",
COVER = "cover"
}
local rendered = {}
-- rendered helpers
local rendered_find_index = function (id)
local index = nil
for i, v in ipairs(rendered) do
if v == id then
index = i
end
end
return index
end
local rendered_add = function (id)
table.insert(rendered, id)
end
local rendered_remove = function (id)
local index = rendered_find_index(id)
if index ~= nil then
table.remove(rendered, index)
end
end
-- module
local ueberzug = {}
-- fifo
ueberzug.request = function(payload)
local path = FIFO_PATH
local fifo = io.open(path, "w")
if fifo == nil then
error("Cannot perform request, no fifo handle.")
end
local encoded_payload = u.json.encode(payload)
-- log(encoded_payload)
fifo:write(encoded_payload .. "\r\n")
fifo:close()
end
-- draw
ueberzug.draw = function(id, path, x, y, w, h)
local resolved_path = u.fs.resolve(path)
if resolved_path == nil then
error("Cannot find image at " .. path)
return
end
local payload = {
action = "add",
identifier = id,
path = resolved_path,
x = x,
y = y,
width = w,
height = h,
scaler = SCALER.FIT_CONTAIN
}
ueberzug.request(payload)
rendered_add(id)
end
-- clear
ueberzug.clear = function(id)
local payload = {
action = 'remove',
identifier = id
}
ueberzug.request(payload)
rendered_remove(id)
end
-- clear all
ueberzug.clear_all = function()
for _, id in ipairs(rendered) do
ueberzug.clear(id)
end
end
-- expose rendered images
ueberzug.get_rendered_images = function()
return rendered
end
return ueberzug