Improve handling of images
dkl9

dkl9 commited on 2024-20 13:22:35
Showing 1 changed files, with 13 additions and 23 deletions.


All images displayed for a prompt are now shown in one iv window,
navigated between with arrows.
... ...
@@ -16,47 +16,37 @@ end
16 16
 local function imgify(text, basepath, ind, climn, conf, question)
17 17
     local fh = io.popen("tput lines")
18 18
     local tl = tonumber(fh:read("a"))
19
+    fh:close()
19 20
     io.write(("\n"):rep(math.floor(0.4 * tl)))
21
+    local pt = text:gsub(IMG_PATTERN, "")
20 22
     showprogress(ind, climn, conf)
21 23
     if conf:get("BoldContext") then
22
-        local csi, cei = text:find("^[^:]+:")
24
+        local csi, cei = pt:find("^[^:]+:")
23 25
         if csi then
24
-            local cpt = text:sub(csi, cei)
26
+            local cpt = pt:sub(csi, cei)
25 27
             io.write("\x1b[1m")
26 28
             io.write(cpt)
27 29
             io.write("\x1b[0m")
28
-            text = text:sub(cei + 1)
30
+            pt = pt:sub(cei + 1)
29 31
             if question then
30 32
                 io.flush()
31 33
                 os.execute("sleep 0.3")
32 34
             end
33 35
         end
34 36
     end
35
-    -- adapted from https://stackoverflow.com/a/1579673
36
-    local PATTERN = "(.-)" .. IMG_PATTERN
37
+    io.write(pt)
38
+    local ifl = {}
39
+    for p in text:gmatch(IMG_PATTERN) do
40
+        table.insert(ifl, 1, basepath .. p)
41
+    end
37 42
     -- Linux/dwm
38
-    local DISPIMG = function(path, st)
39
-        if path and #path > 0 then
43
+    if #ifl > 0 then
40 44
         os.execute(string.format(
41
-                "/usr/local/bin/iv -t '%s' %s%s &",
42
-                st:gsub("'", "\""):gsub(IMG_PATTERN, ""):gsub("\x1b[^m]+m", ""), basepath, path
45
+            "/usr/local/bin/iv -t '%s' %s &",
46
+            pt:gsub("\n", " "):gsub("'", "\""):gsub("\x1b[^m]+m", ""), table.concat(ifl, " ")
43 47
         ))
44 48
     end
45 49
 end
46
-    local lastend = 1
47
-    local mstart, mend, prevtext, imgpath = text:find(PATTERN)
48
-    io.write(prevtext or "")
49
-    DISPIMG(imgpath, text)
50
-    while mstart do
51
-        lastend = mend + 1
52
-        mstart, mend, prevtext, imgpath = text:find(PATTERN, lastend)
53
-        io.write(prevtext or "")
54
-        DISPIMG(imgpath, text)
55
-    end
56
-    if lastend <= #text then
57
-        io.write(text:sub(lastend))
58
-    end
59
-end
60 50
 
61 51
 --[[
62 52
     rounds `x` to `n` places (default 0) after the radix point
63 53