-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexport.lua
More file actions
38 lines (35 loc) · 1.36 KB
/
Copy pathexport.lua
File metadata and controls
38 lines (35 loc) · 1.36 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
-- export — palette action that snapshots the current frame to disk.
--
-- The host hands over the rendered ANSI bytes via
-- `ttymap.api.frame.to_ansi()`; everything below — path choice,
-- success / failure notification — is plugin-side policy. Nothing
-- file-related lives in Rust; this script is the canonical example
-- of the "Rust hands the bytes, Lua decides what to do with them"
-- split.
--
-- Default destination is `/tmp` because ttymap exports are
-- throwaway snapshots, not long-term saves; users who want them
-- under `$XDG_DATA_HOME` can override this plugin in their own
-- runtime path (a same-named `plugin/export.lua` shadows this one).
local function build_path()
return string.format("/tmp/ttymap-%s.ans", os.date("%Y%m%d-%H%M%S"))
end
ttymap.register_palette_command({
label = "Export frame as ANSI",
invoke = function()
local ansi = ttymap.api.frame.to_ansi()
if not ansi then
ttymap.notify("export: no frame to write yet", { level = "warn" })
return
end
local path = build_path()
local f, err = io.open(path, "w")
if not f then
ttymap.notify("export: " .. (err or "open failed"), { level = "error" })
return
end
f:write(ansi)
f:close()
ttymap.notify("Frame exported to " .. path, { level = "info" })
end,
})