The layer for LLeMbas in the terminal: AI on/off, the CLI whole in a tab, the display instead of the split, the pane's status record
llembas-tty / linux (push) Failing after 35s
llembas-tty / linux (push) Failing after 35s
- An option `ai` and a switch that works at any moment; with AI off every key of the layer but the switch does nothing, and says so once. - A key opens the LLeMbas CLI whole, in a new tab, as if `lembas` were typed. - A question goes to the terminal's display (`lembas terminal`, started to the right of the pane at a third of its width, the focus staying where it was) and never to the CLI's own interface in a split. A CLI without the display is said so in words, with how to update. - Keys and palette entries that act on the display from the person's own pane: allow, allow always, deny, stop the turn, new conversation, show/hide, choose model, choose mode. - The shell integration writes the pane's status record at every prompt and when a command starts (a file of the person's own, and the user variable llembas_tty_status), and the layer keeps the file for a shell that could not write it. - The layer talks to the terminal's own drawing part (wezterm.llembas) when there is one: the ask line in place, a word at the cursor's row, what is on. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
7 files changed
+2227
-457
No files matched your search
@@ -54,6 +54,8 @@ jobs:
|
||||
sudo apt-get install -y lua5.4 luajit zsh shellcheck yamllint desktop-file-utils appstream
|
||||
- name: The LLeMbas layer's tests (Lua 5.4 and LuaJIT)
|
||||
run: scripts/test-lua.sh --all
|
||||
- name: The shell integration's status record (bash and zsh, each on a terminal of its own)
|
||||
run: llembas/tests/shell-test.sh
|
||||
- name: Shell scripts, the workflow, the desktop file
|
||||
run: |
|
||||
shellcheck scripts/*.sh llembas/packaging/*.sh llembas/tests/*.sh llembas/ci/*.sh \
|
||||
|
||||
+450
-74
@@ -16,6 +16,14 @@ M.CONTEXT_VERSION = 1
|
||||
-- `lembas complete --shell`. See M.cli_supports_terminal.
|
||||
M.MIN_CLI_VERSION = '2.0.0-beta.18'
|
||||
|
||||
-- What the terminal needs of the CLI beyond that, by the names the CLI gives
|
||||
-- its own parts (`lembas --version --json` -> capabilities). See M.cli_info.
|
||||
M.CONTRACT = 2
|
||||
M.CAP = {
|
||||
display = 'terminal_surface', -- `lembas terminal`, the display
|
||||
models = 'terminal_models', -- `lembas terminal models`
|
||||
}
|
||||
|
||||
M.LIMITS = {
|
||||
max_blocks = 20, -- the most command blocks a context may carry
|
||||
total_bytes = 256 * 1024, -- the context file stays under this
|
||||
@@ -23,6 +31,9 @@ M.LIMITS = {
|
||||
value_chars = 64, -- one value in the session bar
|
||||
title_chars = 80,
|
||||
question_bytes = 16 * 1024,
|
||||
status_bytes = 4096, -- a pane's status record
|
||||
status_value_chars = 1024,
|
||||
model_ref_chars = 128,
|
||||
}
|
||||
|
||||
M.STATES = {
|
||||
@@ -46,11 +57,37 @@ M.BAR_FIELDS = {
|
||||
spend = true,
|
||||
}
|
||||
|
||||
M.MODES = { manual = true, auto = true }
|
||||
|
||||
-- What a colour may be drawn with by default: a number of the colour scheme's
|
||||
-- palette (0-15 are the ANSI colours), so that every colour follows the theme.
|
||||
M.COLORS = {
|
||||
prefix = 6, -- the ask line's own prefix
|
||||
ghost = 8, -- a suggestion after the cursor
|
||||
notice = 3, -- a word from the terminal at the cursor's row
|
||||
command = 2, -- a command that exists
|
||||
unknown = 1, -- a command that does not
|
||||
option = false, -- false: left as the shell drew it
|
||||
string = 3,
|
||||
variable = 6,
|
||||
operator = 5,
|
||||
comment = 8,
|
||||
path = 4, -- a path that exists
|
||||
}
|
||||
|
||||
-- What the layer does when nothing is said. A person's own configuration
|
||||
-- passes a table of the same shape to apply(); only what it names changes.
|
||||
function M.defaults()
|
||||
local colors = {}
|
||||
for k, v in pairs(M.COLORS) do
|
||||
colors[k] = v
|
||||
end
|
||||
return {
|
||||
enabled = true,
|
||||
-- AI features on or off when the terminal starts. Off: every LLeMbas key
|
||||
-- but the switch does nothing, nothing is started, nothing of a pane is
|
||||
-- read and nothing leaves the machine. On: still nothing until a key.
|
||||
ai = true,
|
||||
-- Path of the LLeMbas CLI. nil: `lembas` on PATH, then in ~/.local/bin
|
||||
-- (where its installer puts it). The terminal carries no copy of its own.
|
||||
cli = nil,
|
||||
@@ -64,17 +101,27 @@ function M.defaults()
|
||||
},
|
||||
keys = {
|
||||
ask = { key = 'A', mods = 'CTRL|SHIFT' },
|
||||
new_session = { key = 'S', mods = 'CTRL|SHIFT' },
|
||||
resume = { key = 'O', mods = 'CTRL|SHIFT' },
|
||||
toggle_display = { key = 'D', mods = 'CTRL|SHIFT' },
|
||||
cli = { key = 'S', mods = 'CTRL|SHIFT' },
|
||||
ai = { key = 'I', mods = 'CTRL|SHIFT' },
|
||||
approve = { key = 'Y', mods = 'CTRL|SHIFT' },
|
||||
deny = { key = 'J', mods = 'CTRL|SHIFT' },
|
||||
stop = { key = 'G', mods = 'CTRL|SHIFT' },
|
||||
display = { key = 'D', mods = 'CTRL|SHIFT' },
|
||||
private = { key = 'H', mods = 'CTRL|SHIFT' },
|
||||
ide = { key = 'E', mods = 'CTRL|SHIFT' },
|
||||
-- in the command palette only, until given a key:
|
||||
ask_screen = false,
|
||||
always = false,
|
||||
new = false,
|
||||
model = false,
|
||||
mode = false,
|
||||
},
|
||||
split = {
|
||||
direction = 'Right',
|
||||
size = 0.45,
|
||||
-- The display: the pane on the right in which LLeMbas answers.
|
||||
display = {
|
||||
width = 0.33, -- of the pane it is opened beside
|
||||
},
|
||||
new_session_in = 'split',
|
||||
-- The mode a display starts in: 'manual' (every command asked) or 'auto'.
|
||||
mode = 'manual',
|
||||
context = {
|
||||
enabled = true,
|
||||
blocks = 5,
|
||||
@@ -83,7 +130,16 @@ function M.defaults()
|
||||
screen_lines = 200,
|
||||
alt_screen = false,
|
||||
},
|
||||
autocomplete = false,
|
||||
-- Drawn by the terminal on the shell's input line (needs the shell integration):
|
||||
suggestions = true, -- the rest of the line from your own command history, dimmed
|
||||
highlight = true, -- the command line in colours
|
||||
autocomplete = false, -- AI: the rest of the line from LLeMbas, when history has none
|
||||
-- The terminal's own history of commands it saw run, for the suggestions.
|
||||
history = {
|
||||
enabled = true,
|
||||
max = 5000,
|
||||
},
|
||||
colors = colors,
|
||||
}
|
||||
end
|
||||
|
||||
@@ -348,6 +404,9 @@ local function merge_into(base, over, path, warnings)
|
||||
local d = base[k]
|
||||
if d == nil and not (path == '' and (k == 'cli' or k == 'ide')) then
|
||||
warnings[#warnings + 1] = where .. ': not an option of the LLeMbas layer'
|
||||
elseif path == 'colors.' then
|
||||
-- checked as a whole afterwards (M.options)
|
||||
base[k] = v
|
||||
elseif path == 'keys.' then
|
||||
-- a key: false turns it off, a table { key =, mods = } sets it
|
||||
if v == false then
|
||||
@@ -384,6 +443,17 @@ local function clamp(v, lo, hi, fallback)
|
||||
return v
|
||||
end
|
||||
|
||||
-- A colour of the layer: a number of the palette, '#rrggbb', or false for none.
|
||||
function M.is_color(v)
|
||||
if v == false then
|
||||
return true
|
||||
end
|
||||
if type(v) == 'number' then
|
||||
return v == math.floor(v) and v >= 0 and v <= 255
|
||||
end
|
||||
return type(v) == 'string' and v:match('^#%x%x%x%x%x%x$') ~= nil
|
||||
end
|
||||
|
||||
-- options(user) -> the effective options, and a list of what was wrong with
|
||||
-- what the person passed (never an error: a typo must not take the terminal's
|
||||
-- configuration down).
|
||||
@@ -403,21 +473,24 @@ function M.options(user)
|
||||
c.output_bytes = math.floor(clamp(c.output_bytes, 256, 64 * 1024, d.context.output_bytes))
|
||||
c.screen_lines = math.floor(clamp(c.screen_lines, 1, 1000, d.context.screen_lines))
|
||||
|
||||
local size = o.split.size
|
||||
if type(size) ~= 'number' or size ~= size or size <= 0 then
|
||||
size = d.split.size
|
||||
elseif size > 0.9 and size < 1 then
|
||||
size = 0.9
|
||||
local width = o.display.width
|
||||
if type(width) ~= 'number' or width ~= width or width <= 0 or width >= 1 then
|
||||
if width ~= d.display.width then
|
||||
warnings[#warnings + 1] = 'display.width: expected a part of the pane, more than 0 and less than 1'
|
||||
end
|
||||
width = d.display.width
|
||||
end
|
||||
o.split.size = size
|
||||
local directions = { Right = true, Left = true, Top = true, Bottom = true }
|
||||
if not directions[o.split.direction] then
|
||||
warnings[#warnings + 1] = 'split.direction: expected Right, Left, Top or Bottom'
|
||||
o.split.direction = d.split.direction
|
||||
o.display.width = clamp(width, 0.15, 0.8, d.display.width)
|
||||
if not M.MODES[o.mode] then
|
||||
warnings[#warnings + 1] = "mode: expected 'manual' or 'auto'"
|
||||
o.mode = d.mode
|
||||
end
|
||||
if o.new_session_in ~= 'split' and o.new_session_in ~= 'tab' then
|
||||
warnings[#warnings + 1] = "new_session_in: expected 'split' or 'tab'"
|
||||
o.new_session_in = d.new_session_in
|
||||
o.history.max = math.floor(clamp(o.history.max, 0, 100000, d.history.max))
|
||||
for name, default in pairs(M.COLORS) do
|
||||
if not M.is_color(o.colors[name]) then
|
||||
warnings[#warnings + 1] = 'colors.' .. name .. ": expected a palette number 0-255, '#rrggbb' or false"
|
||||
o.colors[name] = default
|
||||
end
|
||||
end
|
||||
|
||||
local fields = {}
|
||||
@@ -481,6 +554,11 @@ function M.session_from_vars(vars)
|
||||
if state and M.STATES[state] then
|
||||
s.state = state
|
||||
end
|
||||
-- The terminal's display says that it is one; the CLI's own interface does not.
|
||||
if get('lembas_surface', 16) == 'display' then
|
||||
s.display = true
|
||||
end
|
||||
s.ask = M.parse_ask(vars.lembas_ask)
|
||||
local ctx = get('lembas_context', 8)
|
||||
if ctx then
|
||||
local n = tonumber((ctx:gsub('%%$', '')))
|
||||
@@ -491,9 +569,27 @@ function M.session_from_vars(vars)
|
||||
return s
|
||||
end
|
||||
|
||||
-- What the display asks of the person now, from its `lembas_ask` variable:
|
||||
-- "<kind> <keys> <text>", as in "approval yn terminal_run: make test".
|
||||
-- nil when nothing is asked (or the value is not that).
|
||||
function M.parse_ask(value)
|
||||
if type(value) ~= 'string' or value == '' then
|
||||
return nil
|
||||
end
|
||||
local line = M.one_line(value, 200)
|
||||
local kind, keys, text = line:match('^(%a+) (%S+) ?(.*)$')
|
||||
if kind ~= 'approval' and kind ~= 'question' then
|
||||
return nil
|
||||
end
|
||||
return { kind = kind, keys = keys, text = text }
|
||||
end
|
||||
|
||||
-- The session bar as a list of { text =, attention = } parts.
|
||||
function M.bar_parts(session, fields, private)
|
||||
function M.bar_parts(session, fields, private, ai_off)
|
||||
local parts = {}
|
||||
if ai_off then
|
||||
return { { text = 'AI off', off = true } }
|
||||
end
|
||||
if session then
|
||||
for _, f in ipairs(fields) do
|
||||
local text
|
||||
@@ -517,8 +613,8 @@ end
|
||||
|
||||
-- The same as items for the terminal's format function. Only Text items carry
|
||||
-- text from the pane, and that text has no control character left in it.
|
||||
function M.bar_items(session, fields, private)
|
||||
local parts = M.bar_parts(session, fields, private)
|
||||
function M.bar_items(session, fields, private, ai_off)
|
||||
local parts = M.bar_parts(session, fields, private, ai_off)
|
||||
if #parts == 0 then
|
||||
return nil
|
||||
end
|
||||
@@ -534,6 +630,10 @@ function M.bar_items(session, fields, private)
|
||||
items[#items + 1] = { Foreground = { AnsiColor = 'Yellow' } }
|
||||
items[#items + 1] = { Text = p.text }
|
||||
items[#items + 1] = 'ResetAttributes'
|
||||
elseif p.off then
|
||||
items[#items + 1] = { Attribute = { Intensity = 'Half' } }
|
||||
items[#items + 1] = { Text = p.text }
|
||||
items[#items + 1] = 'ResetAttributes'
|
||||
elseif p.private then
|
||||
items[#items + 1] = { Foreground = { AnsiColor = 'Fuchsia' } }
|
||||
items[#items + 1] = { Text = p.text }
|
||||
@@ -597,19 +697,22 @@ function M.tab_title(tab, opts)
|
||||
}
|
||||
end
|
||||
|
||||
-- Which pane of a tab runs LLeMbas. panes: a list of { pane_id =, user_vars = }.
|
||||
-- Prefers one that is not `except` (the pane the person is in).
|
||||
function M.find_session_pane(panes, except)
|
||||
local found
|
||||
-- Which pane of a tab is the terminal's display. panes: a list of
|
||||
-- { pane_id =, user_vars = }. `started`: the id of a pane the layer started
|
||||
-- as the display, which may not have said anything yet. A pane running the
|
||||
-- CLI's own interface is never the display.
|
||||
function M.find_display_pane(panes, started)
|
||||
local starting
|
||||
for _, p in ipairs(panes or {}) do
|
||||
if M.session_from_vars(p.user_vars) then
|
||||
if p.pane_id ~= except then
|
||||
return p.pane_id
|
||||
end
|
||||
found = found or p.pane_id
|
||||
local s = M.session_from_vars(p.user_vars)
|
||||
if s and s.display then
|
||||
return p.pane_id
|
||||
end
|
||||
if started ~= nil and p.pane_id == started and not s then
|
||||
starting = p.pane_id
|
||||
end
|
||||
end
|
||||
return found
|
||||
return starting
|
||||
end
|
||||
|
||||
----------------------------------------------------------------------------
|
||||
@@ -952,6 +1055,299 @@ function M.cli_supports_terminal(version_text)
|
||||
return cmp >= 0
|
||||
end
|
||||
|
||||
-- THE capability check of the display and what came with it, in one place.
|
||||
-- What `lembas --version --json` printed, decoded: { version, terminal_contract,
|
||||
-- capabilities = { names } }. Returns { version, contract, has = { name = true } },
|
||||
-- or nil when it is not that (a CLI from before it answers with plain text).
|
||||
function M.cli_info(decoded)
|
||||
if type(decoded) ~= 'table' or type(decoded.capabilities) ~= 'table' then
|
||||
return nil
|
||||
end
|
||||
local info = { has = {}, contract = 0, version = '' }
|
||||
if type(decoded.version) == 'string' then
|
||||
info.version = M.one_line(decoded.version, 64)
|
||||
end
|
||||
if type(decoded.terminal_contract) == 'number' then
|
||||
info.contract = decoded.terminal_contract
|
||||
end
|
||||
for _, name in ipairs(decoded.capabilities) do
|
||||
if type(name) == 'string' then
|
||||
info.has[name] = true
|
||||
end
|
||||
end
|
||||
return info
|
||||
end
|
||||
|
||||
-- Does that CLI have this part (a key of M.CAP)?
|
||||
function M.cli_has(info, part)
|
||||
return type(info) == 'table' and info.contract >= M.CONTRACT and info.has[M.CAP[part]] == true
|
||||
end
|
||||
|
||||
function M.version_json_args(cli)
|
||||
return { cli, '--version', '--json' }
|
||||
end
|
||||
|
||||
-- A model as it goes onto a command line or into a line for the display: one
|
||||
-- word of plain characters. Only what looks like one is passed on.
|
||||
function M.is_model_ref(ref)
|
||||
return type(ref) == 'string'
|
||||
and #ref > 0
|
||||
and #ref <= M.LIMITS.model_ref_chars
|
||||
and ref:match('^[%w][%w%._:/@%+%-]*$') ~= nil
|
||||
end
|
||||
|
||||
-- The id of a conversation: the same id is the same conversation for the CLI,
|
||||
-- so it is this run of the terminal (seed: when it first needed one) and the
|
||||
-- tab. Characters: letters, digits, '.', '_', '-'.
|
||||
function M.window_id(seed, tab_id)
|
||||
return string.format('t%d-%d', math.floor(seed), math.floor(tab_id))
|
||||
end
|
||||
|
||||
-- THE command that starts the display, in one place.
|
||||
-- o: { window = <id>, pane = <the person's pane>, context = <file>|nil,
|
||||
-- mode = 'manual'|'auto'|nil, model = <ref>|nil, new = true|nil }
|
||||
function M.display_args(cli, o)
|
||||
local args = { cli, 'terminal', '--window', o.window }
|
||||
if type(o.pane) == 'number' then
|
||||
args[#args + 1] = '--pane'
|
||||
args[#args + 1] = string.format('%d', o.pane)
|
||||
end
|
||||
if o.context then
|
||||
args[#args + 1] = '--terminal-context'
|
||||
args[#args + 1] = o.context
|
||||
end
|
||||
if M.is_model_ref(o.model) then
|
||||
args[#args + 1] = '-m'
|
||||
args[#args + 1] = o.model
|
||||
end
|
||||
if M.MODES[o.mode] then
|
||||
args[#args + 1] = '--mode'
|
||||
args[#args + 1] = o.mode
|
||||
end
|
||||
if o.new then
|
||||
args[#args + 1] = '--new'
|
||||
end
|
||||
return args
|
||||
end
|
||||
|
||||
-- THE lines and keys the terminal sends to the display on the person's
|
||||
-- behalf, in one place. Each is what is written to the display's pane.
|
||||
-- M.display_line('model', ref) | ('mode', name) | ('new')
|
||||
-- nil when the argument is not what that line takes.
|
||||
function M.display_line(name, arg)
|
||||
if name == 'model' then
|
||||
if not M.is_model_ref(arg) then
|
||||
return nil
|
||||
end
|
||||
return '/model ' .. arg .. '\r'
|
||||
elseif name == 'mode' then
|
||||
if not M.MODES[arg] then
|
||||
return nil
|
||||
end
|
||||
return '/mode ' .. arg .. '\r'
|
||||
elseif name == 'new' then
|
||||
return '/new\r'
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- One key each: allow once, allow always, deny; and what stops a turn.
|
||||
M.DISPLAY_KEYS = { approve = 'y', always = 'a', deny = 'n', stop = '\3' }
|
||||
|
||||
-- THE commands of the terminal's two models, in one place.
|
||||
function M.models_args(cli)
|
||||
return { cli, 'terminal', 'models', '--json' }
|
||||
end
|
||||
|
||||
-- which: 'main' or 'autocomplete'; ref: a model, or nil for "none of my own".
|
||||
function M.models_set_args(cli, which, ref)
|
||||
local flag = which == 'autocomplete' and '--autocomplete' or '--main'
|
||||
return { cli, 'terminal', 'models', 'set', flag, M.is_model_ref(ref) and ref or 'default' }
|
||||
end
|
||||
|
||||
-- What `lembas terminal models --json` printed, decoded, as what the layer
|
||||
-- keeps: { main = <the chat model a display would start on>|nil,
|
||||
-- autocomplete = <the autocomplete model>|nil,
|
||||
-- choices = { main = { { id =, label = } … }, autocomplete = { … } } }
|
||||
-- nil when it is not that.
|
||||
function M.terminal_models(decoded)
|
||||
if type(decoded) ~= 'table' or type(decoded.models) ~= 'table' then
|
||||
return nil
|
||||
end
|
||||
local function ref_of(v)
|
||||
return M.is_model_ref(v) and v or nil
|
||||
end
|
||||
local main = type(decoded.main) == 'table' and decoded.main or {}
|
||||
local ac = type(decoded.autocomplete) == 'table' and decoded.autocomplete or {}
|
||||
local out = {
|
||||
main = ref_of(main.model) or ref_of(main.start),
|
||||
autocomplete = ref_of(ac.model) or ref_of(ac.account),
|
||||
choices = { main = {}, autocomplete = {} },
|
||||
}
|
||||
for _, m in ipairs(decoded.models) do
|
||||
if type(m) == 'table' then
|
||||
local name = type(m.name) == 'string' and M.one_line(m.name, 48) or ''
|
||||
local uses = type(m['for']) == 'table' and m['for'] or { 'main' }
|
||||
for _, use in ipairs(uses) do
|
||||
-- a display takes a model by its ref, a completion by its id
|
||||
local id = ref_of(use == 'autocomplete' and m.id or m.ref) or ref_of(m.id)
|
||||
local list = out.choices[use]
|
||||
if id and list then
|
||||
list[#list + 1] = { id = id, label = (name ~= '' and name ~= id) and (name .. ' · ' .. id) or id }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- What stands before the question on the ask line, so that the person sees
|
||||
-- which model and mode will answer before sending: "llembas qwen · auto".
|
||||
function M.ask_prefix(model, mode, screen)
|
||||
local parts = {}
|
||||
if type(model) == 'string' and model ~= '' then
|
||||
parts[#parts + 1] = M.one_line(model, 40)
|
||||
end
|
||||
if type(mode) == 'string' and mode ~= '' then
|
||||
parts[#parts + 1] = M.one_line(mode, 16)
|
||||
end
|
||||
if screen then
|
||||
parts[#parts + 1] = 'this screen'
|
||||
end
|
||||
if #parts == 0 then
|
||||
return 'llembas'
|
||||
end
|
||||
return 'llembas ' .. table.concat(parts, ' · ')
|
||||
end
|
||||
|
||||
----------------------------------------------------------------------------
|
||||
-- A pane's status record: what the shell integration says at every prompt
|
||||
-- (llembas/shell/llembas-tty.sh), as the user variable `llembas_tty_status`.
|
||||
|
||||
M.STATUS_VAR = 'llembas_tty_status'
|
||||
|
||||
-- parse_status(text) -> { id, seq, state, exit|nil, ran, time|nil, shell|nil,
|
||||
-- cwd|nil, host|nil } or nil when the text is not a record. It is what a pane
|
||||
-- said of itself: text, never trusted for more than that.
|
||||
function M.parse_status(text)
|
||||
if type(text) ~= 'string' or #text > M.LIMITS.status_bytes then
|
||||
return nil
|
||||
end
|
||||
local fields = {}
|
||||
local first = true
|
||||
for line in (text .. '\n'):gmatch('([^\n]*)\n') do
|
||||
line = line:gsub('\r$', '')
|
||||
if first then
|
||||
if line ~= 'llembas-tty-status=1' then
|
||||
return nil
|
||||
end
|
||||
first = false
|
||||
elseif line ~= '' then
|
||||
local k, v = line:match('^([%w_%-]+)=(.*)$')
|
||||
if not k or #v > M.LIMITS.status_value_chars then
|
||||
return nil
|
||||
end
|
||||
if fields[k] == nil then
|
||||
fields[k] = M.clean(v, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
if first then
|
||||
return nil
|
||||
end
|
||||
local id = fields.id
|
||||
if type(id) ~= 'string' or #id < 1 or #id > 64 or not id:match('^[%w%._:%-]+$') then
|
||||
return nil
|
||||
end
|
||||
local seq = fields.seq and fields.seq:match('^%d+$') and tonumber(fields.seq)
|
||||
if not seq then
|
||||
return nil
|
||||
end
|
||||
local state = fields.state or 'prompt'
|
||||
if state ~= 'prompt' and state ~= 'running' then
|
||||
return nil
|
||||
end
|
||||
local rec = { id = id, seq = seq, state = state, ran = fields.ran == '1' }
|
||||
if fields.exit and fields.exit:match('^%d+$') then
|
||||
local e = tonumber(fields.exit)
|
||||
if e <= 255 then
|
||||
rec.exit = e
|
||||
end
|
||||
end
|
||||
if fields.time and fields.time:match('^%d+$') then
|
||||
rec.time = tonumber(fields.time)
|
||||
end
|
||||
for _, k in ipairs { 'shell', 'cwd', 'host' } do
|
||||
if fields[k] and fields[k] ~= '' then
|
||||
rec[k] = fields[k]
|
||||
end
|
||||
end
|
||||
return rec
|
||||
end
|
||||
|
||||
-- The record as the terminal writes it to the pane's status file when it
|
||||
-- came by user variable: no process id (the shell may be on another machine)
|
||||
-- and `via=uservar`, so that a reader knows it is what the pane said.
|
||||
function M.status_text(rec)
|
||||
local lines = { 'llembas-tty-status=1', 'id=' .. rec.id, string.format('seq=%d', rec.seq), 'state=' .. rec.state }
|
||||
if rec.exit then
|
||||
lines[#lines + 1] = string.format('exit=%d', rec.exit)
|
||||
end
|
||||
if rec.ran then
|
||||
lines[#lines + 1] = 'ran=1'
|
||||
end
|
||||
if rec.time then
|
||||
lines[#lines + 1] = string.format('time=%d', rec.time)
|
||||
end
|
||||
for _, k in ipairs { 'shell', 'host' } do
|
||||
if rec[k] then
|
||||
lines[#lines + 1] = k .. '=' .. rec[k]
|
||||
end
|
||||
end
|
||||
lines[#lines + 1] = 'via=uservar'
|
||||
if rec.cwd then
|
||||
lines[#lines + 1] = 'cwd=' .. rec.cwd
|
||||
end
|
||||
return table.concat(lines, '\n') .. '\n'
|
||||
end
|
||||
|
||||
-- Is `a` the same moment of the same shell as `b`? Then the file is right as it is.
|
||||
function M.same_status(a, b)
|
||||
return type(a) == 'table' and type(b) == 'table' and a.id == b.id and a.seq == b.seq and a.state == b.state
|
||||
end
|
||||
|
||||
-- Where the status records are: the same rule the shell integration follows.
|
||||
-- getenv: function(name) -> value|nil
|
||||
function M.status_dir(getenv, home)
|
||||
local function abs(name)
|
||||
local v = getenv(name)
|
||||
if type(v) == 'string' and v:sub(1, 1) == '/' then
|
||||
return v
|
||||
end
|
||||
return nil
|
||||
end
|
||||
local explicit = abs 'LLEMBAS_TTY_STATUS_DIR'
|
||||
if explicit then
|
||||
return explicit
|
||||
end
|
||||
local runtime = abs 'XDG_RUNTIME_DIR'
|
||||
if runtime then
|
||||
return runtime .. '/llembas-tty'
|
||||
end
|
||||
local state = abs 'XDG_STATE_HOME'
|
||||
if state then
|
||||
return state .. '/llembas-tty'
|
||||
end
|
||||
if type(home) == 'string' and home ~= '' then
|
||||
return home .. '/.local/state/llembas-tty'
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function M.status_file_name(pane_id)
|
||||
return string.format('pane-%d.status', pane_id)
|
||||
end
|
||||
|
||||
-- Where a program of the LLeMbas family may be, in the order tried. Each is an installation of
|
||||
-- its own, the same one a person uses outside this terminal: the terminal carries no copy.
|
||||
-- name: 'lembas' or 'llembas-ide'
|
||||
@@ -997,44 +1393,6 @@ function M.ide_candidates(explicit, home, path, windows)
|
||||
return M.program_candidates('llembas-ide', explicit, home, path, windows)
|
||||
end
|
||||
|
||||
-- The choices of the "resume a session" list from what
|
||||
-- `lembas sessions --json` printed, already decoded: a list of objects with
|
||||
-- at least an `id`. nil when it is not that (an older CLI prints plain text).
|
||||
function M.session_choices(decoded)
|
||||
if type(decoded) ~= 'table' then
|
||||
return nil
|
||||
end
|
||||
local list = decoded
|
||||
if type(decoded.sessions) == 'table' then
|
||||
list = decoded.sessions
|
||||
end
|
||||
local choices = {}
|
||||
for _, s in ipairs(list) do
|
||||
if type(s) == 'table' and M.is_session_id(s.id) then
|
||||
local parts = {}
|
||||
local title = type(s.title) == 'string' and M.one_line(s.title, M.LIMITS.title_chars) or ''
|
||||
parts[1] = title ~= '' and title or s.id
|
||||
if type(s.model) == 'string' and s.model ~= '' then
|
||||
parts[#parts + 1] = M.one_line(s.model, 48)
|
||||
end
|
||||
if type(s.root) == 'string' and s.root ~= '' then
|
||||
parts[#parts + 1] = M.one_line(s.root, 64)
|
||||
end
|
||||
choices[#choices + 1] = { id = s.id, label = table.concat(parts, ' · ') }
|
||||
end
|
||||
end
|
||||
if #choices == 0 then
|
||||
return nil
|
||||
end
|
||||
return choices
|
||||
end
|
||||
|
||||
-- A session id goes onto a command line as one argument; still, only what
|
||||
-- looks like an id is passed on.
|
||||
function M.is_session_id(id)
|
||||
return type(id) == 'string' and #id > 0 and #id <= 128 and id:match('^[%w][%w%._:%-]*$') ~= nil
|
||||
end
|
||||
|
||||
----------------------------------------------------------------------------
|
||||
-- Words
|
||||
|
||||
@@ -1054,13 +1412,31 @@ M.messages = {
|
||||
setup_missing = 'The set-up program (llembas-tty-setup) is not beside this terminal. Install the LLeMbas CLI and LLeMbas IDE by their own installers.',
|
||||
cli_too_old = 'This LLeMbas CLI is too old to work with the terminal: it is %s, and %s or newer is needed.\r\n'
|
||||
.. 'Update it with: lembas update',
|
||||
display_missing = 'This LLeMbas CLI (%s) cannot be the terminal\'s display yet: that came in a newer one.\r\n'
|
||||
.. 'Update it with: lembas update\r\n\r\n'
|
||||
.. 'Until then, the key that opens the CLI itself works: %s.',
|
||||
models_missing = 'This LLeMbas CLI (%s) does not list models for the terminal yet.\r\n' .. 'Update it with: lembas update',
|
||||
models_failed = 'The models could not be read from the LLeMbas CLI. Are you logged in? lembas login',
|
||||
ai_off = 'AI is off in this terminal: the LLeMbas keys do nothing. %s turns it on.',
|
||||
ai_now_on = 'AI on',
|
||||
ai_now_off = 'AI off',
|
||||
private = 'This pane is private: nothing of it is passed on. The question was sent alone.',
|
||||
password = 'The pane is asking for a password: nothing of it is passed on. The question was sent alone.',
|
||||
remote = 'LLeMbas works in local panes for now: this pane runs somewhere else.',
|
||||
write_failed = 'The terminal context could not be written (%s). The question was sent alone.',
|
||||
no_session_pane = 'There is no LLeMbas pane in this tab.',
|
||||
private_on = 'This pane is now private: nothing of it is passed to LLeMbas.',
|
||||
no_display = 'LLeMbas is not running in this tab: ask it something first.',
|
||||
nothing_waits = 'LLeMbas is not waiting for an answer.',
|
||||
answer_first = 'LLeMbas is waiting for an answer: allow or deny first (%s, %s).',
|
||||
asks = 'LLeMbas asks: %s (%s allow, %s deny)',
|
||||
busy = 'LLeMbas is in the middle of a turn: stop it first, or wait.',
|
||||
new_next = 'The next question starts a new conversation.',
|
||||
model_set = 'model: %s',
|
||||
mode_set = 'mode: %s',
|
||||
private_on = 'This pane is now private: nothing of it is passed to LLeMbas, and its commands are not remembered.',
|
||||
private_off = 'This pane is no longer private.',
|
||||
switched = '%s %s',
|
||||
history_cleared = 'The command history of this terminal is cleared.',
|
||||
needs_terminal = 'This is drawn by the terminal itself, and this build has no such part.',
|
||||
}
|
||||
|
||||
return M
|
||||
+673
-154
File diff suppressed because it is too large.
Load diff
@@ -6,7 +6,7 @@
|
||||
#
|
||||
# [ -n "$LLEMBAS_TTY" ] && . /usr/share/llembas-tty/shell/llembas-tty.sh
|
||||
#
|
||||
# It does two things, and only inside LLeMbas TTY, only in an interactive shell:
|
||||
# It does three things, and only inside LLeMbas TTY, only in an interactive shell:
|
||||
#
|
||||
# 1. Semantic zones and the working directory (OSC 133, OSC 7), by WezTerm's
|
||||
# own integration script, shipped beside this file as wezterm.sh. That is
|
||||
@@ -14,6 +14,32 @@
|
||||
# The WEZTERM_SHELL_SKIP_* variables documented in that file apply.
|
||||
# 2. Prompt autocomplete, when it was turned on (`autocomplete = true` in the
|
||||
# LLeMbas layer's options exports LLEMBAS_TTY_AUTOCOMPLETE=1). Off by default.
|
||||
# 3. The pane's status record: at every prompt, and when a command starts, a
|
||||
# few lines that say where this shell stands - at its prompt or running a
|
||||
# command, how the last command ended, when, in which directory. It says
|
||||
# nothing of what the command was. It is how the terminal, and LLeMbas when
|
||||
# it types a command into this pane for you, know that the command has
|
||||
# ended. It goes two ways:
|
||||
# - the file pane-<LLEMBAS_TTY_PANE>.status in the status directory
|
||||
# ($LLEMBAS_TTY_STATUS_DIR, else $XDG_RUNTIME_DIR/llembas-tty, else
|
||||
# $XDG_STATE_HOME/llembas-tty), readable by you alone, replaced whole.
|
||||
# Not in a shell reached over SSH: the file would be on the wrong machine;
|
||||
# - the user variable llembas_tty_status of the pane (OSC 1337 SetUserVar),
|
||||
# which reaches the terminal from another machine too.
|
||||
# LLEMBAS_TTY_SKIP_STATUS=1 turns it off.
|
||||
#
|
||||
# The record, a key=value line each:
|
||||
# llembas-tty-status=1
|
||||
# id=<this shell: its process id and when it started>
|
||||
# seq=<up by one at every prompt>
|
||||
# state=prompt | running
|
||||
# exit=<status of the command before this prompt; absent before the first>
|
||||
# ran=1 (at a prompt: a command line ran since the prompt before)
|
||||
# time=<seconds since the epoch>
|
||||
# shell=bash | zsh
|
||||
# pid=<the shell's process id; not over SSH>
|
||||
# host=<the machine; only over SSH>
|
||||
# cwd=<the working directory>
|
||||
|
||||
if [ -z "${LLEMBAS_TTY-}" ]; then
|
||||
return 0
|
||||
@@ -55,5 +81,133 @@ if [ "${LLEMBAS_TTY_AUTOCOMPLETE-}" = "1" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "${LLEMBAS_TTY_SKIP_STATUS-}" != "1" ] && [ -z "${__llembas_tty_seq-}" ]; then
|
||||
__llembas_tty_seq=0
|
||||
__llembas_tty_ran=
|
||||
__llembas_tty_exit=
|
||||
__llembas_tty_now="${EPOCHSECONDS-}"
|
||||
[ -n "$__llembas_tty_now" ] || __llembas_tty_now="$(date +%s)"
|
||||
__llembas_tty_id="$$.$__llembas_tty_now"
|
||||
if [ -n "${ZSH_NAME-}" ]; then
|
||||
__llembas_tty_shell=zsh
|
||||
else
|
||||
__llembas_tty_shell=bash
|
||||
fi
|
||||
|
||||
# The file, on the terminal's own machine only and for a pane with a number.
|
||||
__llembas_tty_status_file=
|
||||
if [ -z "${SSH_CONNECTION-}" ] && [ -z "${SSH_TTY-}" ]; then
|
||||
case "${LLEMBAS_TTY_PANE-}" in
|
||||
'' | *[!0-9]*) ;;
|
||||
*)
|
||||
case "${LLEMBAS_TTY_STATUS_DIR-}" in
|
||||
/*) __llembas_tty_status_file="$LLEMBAS_TTY_STATUS_DIR" ;;
|
||||
*)
|
||||
case "${XDG_RUNTIME_DIR-}" in
|
||||
/*) __llembas_tty_status_file="$XDG_RUNTIME_DIR/llembas-tty" ;;
|
||||
*)
|
||||
case "${XDG_STATE_HOME-}" in
|
||||
/*) __llembas_tty_status_file="$XDG_STATE_HOME/llembas-tty" ;;
|
||||
*) [ -z "${HOME-}" ] || __llembas_tty_status_file="$HOME/.local/state/llembas-tty" ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
[ -z "$__llembas_tty_status_file" ] || __llembas_tty_status_file="$__llembas_tty_status_file/pane-$LLEMBAS_TTY_PANE.status"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# __llembas_tty_record <state>: write the record both ways. The directory's
|
||||
# name is cut at a line break: a record is one fact a line.
|
||||
__llembas_tty_record() {
|
||||
local now record cwd="${PWD%%$'\n'*}"
|
||||
now="${EPOCHSECONDS-}"
|
||||
[ -n "$now" ] || now="$(date +%s)"
|
||||
record="llembas-tty-status=1
|
||||
id=$__llembas_tty_id
|
||||
seq=$__llembas_tty_seq
|
||||
state=$1"
|
||||
[ -z "$__llembas_tty_exit" ] || record="$record
|
||||
exit=$__llembas_tty_exit"
|
||||
if [ "$1" = prompt ] && [ -n "$__llembas_tty_ran" ]; then
|
||||
record="$record
|
||||
ran=1"
|
||||
fi
|
||||
record="$record
|
||||
time=$now
|
||||
shell=$__llembas_tty_shell"
|
||||
if [ -n "${SSH_CONNECTION-}" ] || [ -n "${SSH_TTY-}" ]; then
|
||||
record="$record
|
||||
host=${HOSTNAME:-${HOST:-unknown}}"
|
||||
else
|
||||
record="$record
|
||||
pid=$$"
|
||||
fi
|
||||
record="$record
|
||||
cwd=${cwd:0:1024}"
|
||||
if [ -n "$__llembas_tty_status_file" ]; then
|
||||
# Made beside the file, closed to everybody else, then moved over it:
|
||||
# a reader sees the old record or the new one, never half of one.
|
||||
(
|
||||
umask 077
|
||||
[ -d "${__llembas_tty_status_file%/*}" ] || mkdir -p "${__llembas_tty_status_file%/*}"
|
||||
printf '%s\n' "$record" >"$__llembas_tty_status_file.$$" &&
|
||||
mv -f "$__llembas_tty_status_file.$$" "$__llembas_tty_status_file"
|
||||
) 2>/dev/null
|
||||
fi
|
||||
if command -v base64 >/dev/null 2>&1; then
|
||||
if [ -z "${TMUX-}" ]; then
|
||||
printf '\033]1337;SetUserVar=%s=%s\007' llembas_tty_status "$(printf '%s\n' "$record" | base64 | tr -d '\r\n')"
|
||||
else
|
||||
# shellcheck disable=SC1003 # the backslash that ends tmux's passthrough
|
||||
printf '\033Ptmux;\033\033]1337;SetUserVar=%s=%s\007\033\\' llembas_tty_status "$(printf '%s\n' "$record" | base64 | tr -d '\r\n')"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
__llembas_tty_preexec() {
|
||||
__llembas_tty_ran=1
|
||||
__llembas_tty_record running
|
||||
}
|
||||
|
||||
__llembas_tty_precmd() {
|
||||
local ret="$?"
|
||||
__llembas_tty_seq=$((__llembas_tty_seq + 1))
|
||||
# Before the first prompt no command of this shell has ended.
|
||||
if [ "$__llembas_tty_seq" -gt 1 ]; then
|
||||
__llembas_tty_exit="$ret"
|
||||
fi
|
||||
__llembas_tty_record prompt
|
||||
__llembas_tty_ran=
|
||||
return "$ret"
|
||||
}
|
||||
|
||||
if [ -n "${ZSH_NAME-}" ] || [ -n "${bash_preexec_imported-}" ]; then
|
||||
# First of the prompt's functions, so that $? is still the command's.
|
||||
precmd_functions=(__llembas_tty_precmd "${precmd_functions[@]}")
|
||||
else
|
||||
PROMPT_COMMAND="__llembas_tty_precmd${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
|
||||
fi
|
||||
if [ -n "${ZSH_NAME-}" ]; then
|
||||
preexec_functions+=(__llembas_tty_preexec)
|
||||
elif [ "${BASH_VERSINFO[0]}" -gt 4 ] || { [ "${BASH_VERSINFO[0]}" -eq 4 ] && [ "${BASH_VERSINFO[1]}" -ge 4 ]; }; then
|
||||
# bash 4.4 and newer expand PS0 after every command line that is about to
|
||||
# run, and never for an empty one. (bash-preexec, which WezTerm's script
|
||||
# brings, does not see a command line that is all one subshell.) What the
|
||||
# first expansion prints is the record's user variable; the second prints
|
||||
# nothing and notes, in this shell, that a command ran.
|
||||
__llembas_tty_mark=
|
||||
# shellcheck disable=SC2016 # expanded by bash at each command, not here
|
||||
PS0="${PS0-}"'$(__llembas_tty_record running)${__llembas_tty_mark:$((__llembas_tty_ran=1,0)):0}'
|
||||
elif [ -n "${bash_preexec_imported-}" ]; then
|
||||
preexec_functions+=(__llembas_tty_preexec)
|
||||
else
|
||||
# Nothing tells a command from an empty line here: none is said to have run.
|
||||
:
|
||||
fi
|
||||
fi
|
||||
|
||||
unset __llembas_tty_dir
|
||||
true
|
||||
@@ -15,6 +15,8 @@ function F.new(env)
|
||||
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
|
||||
@@ -96,6 +98,35 @@ function F.new(env)
|
||||
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', 'configure', 'notice', 'set_private', 'command_ended', 'clear_history' } do
|
||||
n[name] = function(...)
|
||||
n.calls[#n.calls + 1] = { name, ... }
|
||||
return true
|
||||
end
|
||||
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 = {
|
||||
@@ -141,6 +172,9 @@ function F.pane(spec)
|
||||
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
|
||||
@@ -151,16 +185,23 @@ function F.pane(spec)
|
||||
self.splits[#self.splits + 1] = args
|
||||
local new = F.pane { id = (spec.next_id or 100) + #self.splits }
|
||||
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)
|
||||
local t = { list = panes }
|
||||
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
|
||||
@@ -173,8 +214,15 @@ function F.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
|
||||
|
||||
+796
-226
File diff suppressed because it is too large.
Load diff
Executable
+101
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env bash
|
||||
# The shell integration's command record (llembas/shell/llembas-tty.sh), in a real interactive
|
||||
# bash and zsh on a terminal of their own (script(1): bash tells a command from an empty line
|
||||
# only on a terminal): the file and the user variable, what counts as a command, and that
|
||||
# nothing at all happens outside LLeMbas TTY.
|
||||
#
|
||||
# llembas/tests/shell-test.sh
|
||||
set -uo pipefail
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
work="$(mktemp -d)"
|
||||
trap 'rm -rf "${work:?}"' EXIT
|
||||
failed=0
|
||||
ok() { echo "ok $1"; }
|
||||
bad() {
|
||||
echo "FAIL $1"
|
||||
[ -z "${GITHUB_ACTIONS:-}" ] || printf '::error title=shell::%s\n' "$(printf '%s' "$1" | head -c 900 | tr '\n\r' ' ' | sed 's/%/%25/g')"
|
||||
failed=1
|
||||
}
|
||||
expect() { if printf '%s' "$2" | grep -Eq -- "$3"; then ok "$1"; else bad "$1: wanted /$3/ in: $2"; fi; }
|
||||
|
||||
# run <name> <shell words…>: the lines on stdin are typed into that shell, in a home and a
|
||||
# runtime directory of its own. Extra variables come through EXTRA (one VAR=value a word).
|
||||
run() {
|
||||
local name="$1"
|
||||
shift
|
||||
mkdir -p "$work/$name/run" "$work/$name/home"
|
||||
# shellcheck disable=SC2086 # EXTRA and SHELL_DIR_OFF are words on purpose
|
||||
# The lines are typed slowly enough for each prompt to be there first.
|
||||
while IFS= read -r line || [ -n "$line" ]; do
|
||||
printf '%s\n' "$line"
|
||||
sleep 0.3
|
||||
done | env -i PATH=/usr/bin:/bin TERM=xterm-256color HOME="$work/$name/home" XDG_RUNTIME_DIR="$work/$name/run" \
|
||||
LLEMBAS_TTY_SHELL_DIR="$ROOT/assets/shell-integration" ${EXTRA-} ${SHELL_DIR_OFF-} \
|
||||
script -qec "$*" /dev/null 2>&1 | cat -v
|
||||
}
|
||||
# field <name> <record text>: the value of one key of a record.
|
||||
field() { printf '%s\n' "$2" | sed -n "s/^$1=//p" | head -n 1; }
|
||||
# The records in what a shell printed (the user variable, decoded), one a line, each as
|
||||
# "<seq> <state> <exit or -> <ran or ->".
|
||||
records() {
|
||||
printf '%s\n' "$1" | grep -o 'SetUserVar=llembas_tty_status=[A-Za-z0-9+/=]*' | while IFS= read -r v; do
|
||||
text="$(printf '%s\n' "${v#SetUserVar=llembas_tty_status=}" | base64 -d)"
|
||||
e="$(field exit "$text")"
|
||||
r="$(field ran "$text")"
|
||||
printf '%s %s %s %s\n' "$(field seq "$text")" "$(field state "$text")" "${e:--}" "${r:--}"
|
||||
done
|
||||
}
|
||||
|
||||
for shell in bash zsh; do
|
||||
command -v "$shell" >/dev/null 2>&1 || { echo "skip $shell: not here"; continue; }
|
||||
case "$shell" in
|
||||
bash) words=(bash --norc --noprofile -i) ;;
|
||||
*) words=(zsh -f -i +Z) ;;
|
||||
esac
|
||||
|
||||
# A first prompt, a command that works, an empty line, one that fails with 3 and is all a
|
||||
# subshell (which bash-preexec alone would not see).
|
||||
script=". '$ROOT/llembas/shell/llembas-tty.sh'"$'\ntrue\n\n(exit 3)\nexit\n'
|
||||
want='^1 prompt - -,1 running - -,2 prompt 0 1,3 prompt 0 -,3 running 0 -,4 prompt 3 1,4 running 3 -,$'
|
||||
out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=7" run "$shell-in" "${words[@]}")"
|
||||
file="$work/$shell-in/run/llembas-tty/pane-7.status"
|
||||
expect "$shell: a record at every prompt and when a command starts; an empty line ran nothing" "$(records "$out" | tr '\n' ',')" "$want"
|
||||
text="$(cat "$file" 2>/dev/null)"
|
||||
expect "$shell: the file starts with the format's line" "$(printf '%s\n' "$text" | head -n 1)" '^llembas-tty-status=1$'
|
||||
expect "$shell: the file holds the last record (exit, as it was typed, started)" "$(field seq "$text") $(field state "$text") $(field exit "$text")" '^4 running 3$'
|
||||
expect "$shell: the record names the shell, its process and its directory" "$(field shell "$text") $(field pid "$text") $(field id "$text") $(field cwd "$text") $(field time "$text")" "^$shell [0-9]+ [0-9]+\.[0-9]+ /.* [0-9]{9,}\$"
|
||||
expect "$shell: the file is the person's alone" "$(stat -c %a "$file" 2>/dev/null)" '^600$'
|
||||
expect "$shell: nothing is left beside the file" "$(cd "$work/$shell-in/run/llembas-tty" 2>/dev/null && find . -type f | sort | tr '\n' ' ')" '^\./pane-7\.status $'
|
||||
expect "$shell: the exit status still reaches the prompt" "$(printf '%s' ". '$ROOT/llembas/shell/llembas-tty.sh'"$'\n(exit 5)\necho "status=$?"\nexit\n' | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=7" run "$shell-st" "${words[@]}")" 'status=5'
|
||||
|
||||
# The directory the terminal names is where the file goes; else the state directory.
|
||||
mkdir -p "$work/$shell-dir/elsewhere"
|
||||
printf '%s' "$script" | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=12 LLEMBAS_TTY_STATUS_DIR=$work/$shell-dir/elsewhere" run "$shell-dir" "${words[@]}" >/dev/null
|
||||
expect "$shell: in LLEMBAS_TTY_STATUS_DIR" "$(field seq "$(cat "$work/$shell-dir/elsewhere/pane-12.status" 2>/dev/null)")" '^4$'
|
||||
printf '%s' "$script" | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=3 XDG_RUNTIME_DIR= XDG_STATE_HOME=$work/$shell-state/state" run "$shell-state" "${words[@]}" >/dev/null
|
||||
expect "$shell: with no runtime directory, in the state directory" "$(field seq "$(cat "$work/$shell-state/state/llembas-tty/pane-3.status" 2>/dev/null)")" '^4$'
|
||||
|
||||
# A pane id that is not a number names no file; the variable is still set.
|
||||
out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=../x" run "$shell-bad" "${words[@]}")"
|
||||
expect "$shell: a pane id that is not a number writes no file" "$(find "$work/$shell-bad/run" -type f | wc -l)" '^0$'
|
||||
expect "$shell: and the variable is still set" "$(records "$out" | tr '\n' ',')" "$want"
|
||||
|
||||
# Over SSH the file would be on the wrong machine: the variable alone, naming the machine.
|
||||
out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=7 SSH_CONNECTION=x HOSTNAME=workstation HOST=workstation" run "$shell-ssh" "${words[@]}")"
|
||||
one="$(printf '%s\n' "$out" | grep -o 'SetUserVar=llembas_tty_status=[A-Za-z0-9+/=]*' | head -n 1 | sed 's/.*=llembas_tty_status=//' | base64 -d)"
|
||||
expect "$shell: over SSH no file is written" "$(find "$work/$shell-ssh/run" -type f | wc -l)" '^0$'
|
||||
expect "$shell: over SSH the record names the machine and no process" "host=$(field host "$one") pid=$(field pid "$one")" '^host=workstation pid=$'
|
||||
|
||||
# Without WezTerm's script (no command marks at all), the record is still kept.
|
||||
out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=7" SHELL_DIR_OFF="LLEMBAS_TTY_SHELL_DIR=$work/nowhere" run "$shell-alone" "${words[@]}")"
|
||||
expect "$shell: kept without the command marks too" "$(records "$out" | tr '\n' ',')" "$want"
|
||||
|
||||
# Turned off, and outside LLeMbas TTY: nothing.
|
||||
out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TTY=1.0.0 LLEMBAS_TTY_PANE=7 LLEMBAS_TTY_SKIP_STATUS=1" run "$shell-off" "${words[@]}")"
|
||||
expect "$shell: LLEMBAS_TTY_SKIP_STATUS=1 turns it off" "$(records "$out" | wc -l) $(find "$work/$shell-off/run" -type f | wc -l)" '^0 0$'
|
||||
out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TTY_PANE=7" run "$shell-out" "${words[@]}")"
|
||||
expect "$shell: outside LLeMbas TTY nothing is written and nothing is printed" "$(printf '%s' "$out" | grep -c 'SetUserVar\|133;') $(find "$work/$shell-out/run" -type f | wc -l)" '^0 0$'
|
||||
done
|
||||
|
||||
[ "$failed" -eq 0 ] && echo "shell: all passed"
|
||||
exit "$failed"
|
||||
Reference in new issue
Block a user