-- LLeMbas TERM: a stand-in for the terminal's `wezterm` module, for the tests. -- It records what the layer asks the terminal to do and does none of it, -- except chmod, which is run for real so that the file mode can be checked. local F = {} function F.new(env) env = env or {} local w = { GLOBAL = {}, target_triple = env.target_triple or 'x86_64-unknown-linux-gnu', executable_dir = env.executable_dir or '/nonexistent/bin', home_dir = env.home_dir or '/nonexistent/home', config_file = env.config_file, handlers = {}, logs = {}, spawned = {}, child_results = env.child_results or {}, -- the terminal's own drawing part, when a test gives one (see F.native) llembas = env.native, } -- wezterm.action.Name(args) and wezterm.action.Name { … } give a table -- that names the action; nothing more is needed to check what was bound. w.action = setmetatable({}, { __index = function(_, name) return function(args) return { action = name, args = args } end end, }) function w.on(name, fn) w.handlers[name] = w.handlers[name] or {} table.insert(w.handlers[name], fn) end function w.emit(name, ...) local result for _, fn in ipairs(w.handlers[name] or {}) do result = fn(...) end return result end function w.log_warn(...) w.logs[#w.logs + 1] = table.concat({ ... }, ' ') end w.log_info = w.log_warn w.log_error = w.log_warn function w.config_builder() return {} end function w.format(items) local out = {} for _, item in ipairs(items) do if type(item) == 'table' and item.Text then out[#out + 1] = item.Text end end return table.concat(out) end function w.run_child_process(args) w.spawned[#w.spawned + 1] = args if args[1] == 'chmod' then local ok = os.execute(string.format("chmod %s '%s'", args[2], args[3])) return ok == true or ok == 0, '', '' end local key = table.concat(args, ' ', 2) local r = w.child_results[key] if r then return r[1], r[2], '' end return false, '', 'not found' end function w.read_dir(dir) local list = {} local p = io.popen(string.format("ls -1 '%s' 2>/dev/null", dir)) if p then for name in p:lines() do list[#list + 1] = dir .. '/' .. name end p:close() end return list end function w.json_parse(text) if env.json_parse then return env.json_parse(text) end error 'not json' end return w end -- The terminal's own part (`wezterm.llembas`), recording what it is asked. function F.native() local n = { calls = {} } for _, name in ipairs { 'ask_line', 'ask_prefix', 'configure', 'notice', 'hint', 'status', 'set_private', 'command_ended', 'clear_history', 'configure_pane' } do n[name] = function(...) n.calls[#n.calls + 1] = { name, ... } return true end end -- the terminal's side of the autocomplete in words, and its data directory -- (a test sets n.status_text and n.dir) function n.complete_status() return n.status_text or 'engine: none' end -- is the pane's line the ask line (a test sets n.asking) function n.ask_active() return n.asking == true end function n.data_dir() return n.dir or '' end -- a path with its links followed (a test sets n.real[path]) function n.real_path(path) return n.real and n.real[path] or path end function n.last(name) for i = #n.calls, 1, -1 do if n.calls[i][1] == name then return n.calls[i] end end return nil end function n.count(name) local c = 0 for _, call in ipairs(n.calls) do if call[1] == name then c = c + 1 end end return c end return n end -- A pane that records what is sent to it. function F.pane(spec) local p = { id = spec.id or 1, vars = spec.vars or {}, sent = {}, splits = {}, spec = spec, } function p:pane_id() return self.id end function p:get_user_vars() return self.vars end function p:get_metadata() return spec.metadata end function p:is_alt_screen_active() return spec.alt == true end function p:get_semantic_zones() self.read_zones = true return spec.zones or {} end function p:get_text_from_semantic_zone(zone) self.read_text = true return zone.text end function p:get_lines_as_text() self.read_screen = true return spec.screen or '' end function p:get_current_working_dir() return spec.cwd end function p:get_foreground_process_name() if self.program ~= nil then return self.program or nil end return spec.program end -- What the terminal says of the process in front of the pane. A test sets -- p.process: a table, or false for "it cannot say". function p:get_foreground_process_info() if self.process ~= nil then return self.process or nil end return spec.process end function p:get_domain_name() return spec.domain or 'local' end function p:tab() return spec.tab end function p:activate() self.activated = (self.activated or 0) + 1 end function p:send_text(text) self.sent[#self.sent + 1] = { 'text', text } end function p:send_paste(text) self.sent[#self.sent + 1] = { 'paste', text } end function p:split(args) self.splits[#self.splits + 1] = args -- the command the layer started runs in the new pane: the shell that -- holds it, with the engine under it (or the engine itself) local words = {} for i, a in ipairs(args.args or {}) do words[i] = a end local process = { pid = 4000, executable = words[1], argv = words, children = {} } if words[1] == '/bin/sh' then process.executable = '/usr/bin/dash' local engine = {} for i = 5, #words do engine[#engine + 1] = words[i] end process.children = { [4001] = { pid = 4001, executable = engine[1], argv = engine, children = {} } } end local new = F.pane { id = (spec.next_id or 100) + #self.splits, process = process } self.last_split = new if spec.tab then table.insert(spec.tab.list, new) new.spec.tab = spec.tab end return new end return p end function F.tab(panes, id) local t = { list = panes, id = id or 1 } function t:panes() return self.list end function t:tab_id() return self.id end for _, p in ipairs(panes) do p.spec.tab = t end return t end function F.window(id) local win = { id = id or 1, actions = {}, status = nil } function win:window_id() return self.id end function win:perform_action(action, pane) -- an action that takes nothing is given by its name alone (act.TogglePaneZoomState) if type(action) == 'function' then action = action() end self.actions[#self.actions + 1] = { action = action, pane = pane } end function win:active_pane() return self.active end function win:set_right_status(text) self.status = text end return win end return F