forked from waiting-for-dev/ergoterm.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.lua
More file actions
107 lines (96 loc) · 2.36 KB
/
update.lua
File metadata and controls
107 lines (96 loc) · 2.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
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
---@diagnostic disable: invisible
---@module "ergoterm.mode"
local mode = require("ergoterm.mode")
---@module "ergoterm.utils"
local utils = require("ergoterm.utils")
local ALLOWED_SETTINGS = {
"auto_scroll",
"bang_target",
"watch_files",
"clear_env",
"cleanup_on_success",
"cleanup_on_failure",
"default_action",
"layout",
"env",
"name",
"meta",
"fixed_width",
"fixed_height",
"float_props",
"float_winblend",
"persist_mode",
"auto_list",
"size",
"start_in_insert",
"sticky",
"on_close",
"on_create",
"on_focus",
"on_job_stderr",
"on_job_stdout",
"on_job_exit",
"on_open",
"on_start",
"on_stop",
"show_on_success",
"show_on_failure",
"tags"
}
local M = {}
---@class UpdateOptions
---@field deep_merge? boolean whether to deep merge table properties (default: false)
---
---@param term Terminal
---@param settings TerminalCreateSettings
---@param opts? UpdateOptions
---
---@return Terminal?
function M.update(term, settings, opts)
opts = opts or {}
local deep_merge = opts.deep_merge or false
for setting, _ in pairs(settings) do
if not vim.tbl_contains(ALLOWED_SETTINGS, setting) then
return utils.notify(
string.format("Cannot change %s after terminal creation", setting),
"error"
)
end
end
for k, v in pairs(settings) do
M._update_setting(term, k, v, deep_merge)
end
M._recompute_state(term)
return term
end
---@private
---@param term Terminal
---@param setting string
---@param value any
---@param deep_merge boolean
function M._update_setting(term, setting, value, deep_merge)
local should_merge = deep_merge
and type(value) == "table"
and type(term[setting]) == "table"
and not vim.islist(value)
if should_merge then
term[setting] = vim.tbl_deep_extend("force", term[setting], value)
else
term[setting] = value
end
end
---@private
---@param term Terminal
function M._recompute_state(term)
term._state.mode = mode.get_initial(term.start_in_insert)
term._state.layout = term.layout
term._state.on_job_exit = term:_compute_exit_handler(term.on_job_exit)
term._state.on_job_stdout = term:_compute_output_handler(term.on_job_stdout)
term._state.on_job_stderr = term:_compute_output_handler(term.on_job_stderr)
term._state.size = term:_compute_size()
end
return setmetatable(M, {
__call = function(_, ...)
return M.update(...)
end
})