mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
fix(tmux): read a one-binding key table from the full listing
A tmux built from master (`tmux -V`: next-3.7) prints nothing, exit 0, for `list-keys -T <table>` when the table holds exactly one binding. tmux 3.4 prints the line. One binding is the shape restrictTmuxPrefixTable leaves behind (`<prefix> send-prefix`), so on that tmux the reduced table read as needing a reset and every session create reset it again. `codeman tui` read its prefix table the same way, so readPrefixBinding called a bound key free and readDetachKey found no detach key. A blank answer is now read from the full `list-keys` listing, keeping only that table's lines. The parsers already match on `-T <table>`. Tests: recorded-command cases for the blank answer (reduced table kept, stray binding still reset) and TuiClient cases for both reads. The real-tmux helper reads tables through the same fallback, and the escaped `C-\` check accepts the unescaped form the full listing prints.
This commit is contained in:
1 parent
e99b42c387
commit
c497869ee5
6 files changed
+111
-9
No files matched your search
@@ -115,6 +115,32 @@ describe('restrictTmuxPrefixTable (recorded commands)', () => {
|
||||
expect(restrictTmuxPrefixTable(`tmux -L 'codeman'`, tmux.run)).toBe(false);
|
||||
expect(tmux.calls.filter((c) => c.includes('bind-key'))).toEqual([]);
|
||||
});
|
||||
|
||||
it('reads a one-binding table from the full listing when `list-keys -T prefix` prints nothing', () => {
|
||||
// A tmux `next-3.7` build lists a table holding exactly one binding as nothing at
|
||||
// all. The reduced table must still read as reduced, or every call resets it.
|
||||
const tmux = fakeTmux({
|
||||
'show-options -gv prefix': 'C-b\n',
|
||||
'list-keys -T prefix': '',
|
||||
'list-keys':
|
||||
'bind-key -T copy-mode C-a send-keys -X start-of-line\n' +
|
||||
'bind-key -T prefix C-b send-prefix\n' +
|
||||
'bind-key -T root F1 detach-client\n',
|
||||
});
|
||||
expect(restrictTmuxPrefixTable(`tmux -L 'codeman'`, tmux.run)).toBe(false);
|
||||
expect(tmux.calls).toContain(`tmux -L 'codeman' list-keys`);
|
||||
expect(tmux.calls.filter((c) => c.includes('bind-key') || c.includes('unbind-key'))).toEqual([]);
|
||||
});
|
||||
|
||||
it('still resets when the full listing shows a stock prefix binding', () => {
|
||||
const tmux = fakeTmux({
|
||||
'show-options -gv prefix': 'C-b\n',
|
||||
'list-keys -T prefix': '',
|
||||
'list-keys': 'bind-key -T prefix ) switch-client -n\n',
|
||||
});
|
||||
expect(restrictTmuxPrefixTable(`tmux -L 'codeman'`, tmux.run)).toBe(true);
|
||||
expect(tmux.calls).toContain(`tmux -L 'codeman' unbind-key -a -T prefix`);
|
||||
});
|
||||
});
|
||||
|
||||
const hasTmux = (() => {
|
||||
@@ -126,12 +152,29 @@ const hasTmux = (() => {
|
||||
}
|
||||
})();
|
||||
|
||||
/**
|
||||
* One key table as `list-keys -T <table>` prints it. Some tmux builds (a `next-3.7`
|
||||
* development build) print NOTHING there when the table holds exactly one binding,
|
||||
* which is the shape every reset in this file produces, so a blank answer is read from
|
||||
* the full listing instead, its padding folded to the `-T <table>` form.
|
||||
*/
|
||||
function listKeyTable(base: string, table: string): string {
|
||||
const run = (cmd: string): string => execSync(cmd, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
||||
const direct = run(`${base} list-keys -T ${table}`);
|
||||
if (direct.trim()) return direct;
|
||||
return run(`${base} list-keys`)
|
||||
.split('\n')
|
||||
.filter((line) => new RegExp(`^bind-key\\s+(?:-\\S+\\s+)*?-T\\s+${table}\\s`).test(line.trim()))
|
||||
.map((line) => line.trim().replace(/-T\s+(\S+)\s+/, '-T $1 '))
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
describe.skipIf(!hasTmux)('restrictTmuxPrefixTable against a real tmux (private socket)', () => {
|
||||
// Throwaway server, never Codeman's socket; -f /dev/null keeps the user's config out.
|
||||
const socket = `codeman-prefix-test-${process.pid}`;
|
||||
const tmux = `tmux -L ${socket} -f /dev/null`;
|
||||
const run = (cmd: string): string => execSync(cmd, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
||||
const keys = (table: string) => run(`${tmux} list-keys -T ${table}`);
|
||||
const keys = (table: string) => listKeyTable(tmux, table);
|
||||
let client: pty.IPty | null = null;
|
||||
|
||||
/** The session the attach client is looking at right now. */
|
||||
@@ -275,7 +318,8 @@ describe.skipIf(!hasTmux)('restrictTmuxPrefixTable against a real tmux (private
|
||||
run(`${tmux} set-option -g prefix 'C-\\'`);
|
||||
try {
|
||||
expect(restrictTmuxPrefixTable(tmux, run)).toBe(true);
|
||||
expect(keys('prefix')).toMatch(/-T prefix C-\\\\\s+send-prefix/);
|
||||
// `list-keys -T` escapes the key (`C-\\`); the full listing some builds fall back to does not.
|
||||
expect(keys('prefix')).toMatch(/-T prefix C-\\(?:\\)?\s+send-prefix/);
|
||||
expect(restrictTmuxPrefixTable(tmux, run)).toBe(false);
|
||||
} finally {
|
||||
run(`${tmux} set-option -g prefix C-b`);
|
||||
@@ -345,7 +389,7 @@ describe.skipIf(!hasTmux)('nested Docker/remote tmux prefix table (private socke
|
||||
it('after the nested reset, Ctrl+Q ), s and w leave the client, and Ctrl+Q Ctrl+Q reaches the pane', async () => {
|
||||
for (const command of nestedPrefixTableResetCommands()) run(`${tmux} ${command}`);
|
||||
|
||||
const prefix = run(`${tmux} list-keys -T prefix`)
|
||||
const prefix = listKeyTable(tmux, 'prefix')
|
||||
.split('\n')
|
||||
.filter((line) => line.trim());
|
||||
expect(prefix).toEqual([expect.stringMatching(/-T prefix C-q\s+send-prefix$/)]);
|
||||
@@ -381,7 +425,7 @@ describe.skipIf(!hasTmux)('nested Docker/remote tmux prefix table (private socke
|
||||
].join(' \\; ');
|
||||
try {
|
||||
run(`${base} -f ${config} ${chain}`);
|
||||
const prefix = run(`${base} list-keys -T prefix`)
|
||||
const prefix = listKeyTable(base, 'prefix')
|
||||
.split('\n')
|
||||
.filter((line) => line.trim());
|
||||
expect(prefix).toEqual([expect.stringMatching(/-T prefix C-q\s+send-prefix$/)]);
|
||||
|
||||
@@ -1015,3 +1015,35 @@ describe('resumeSession', () => {
|
||||
expect(recorded).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('TuiClient key-table reads on a tmux that lists a one-binding table as nothing', () => {
|
||||
// A tmux `next-3.7` build prints nothing for `list-keys -T prefix` when the table
|
||||
// holds exactly one binding, which is how Codeman leaves it. Reading that as "free"
|
||||
// would let the TUI claim a key the user has bound.
|
||||
const fullListing =
|
||||
'bind-key -T copy-mode C-a send-keys -X start-of-line\n' +
|
||||
'bind-key -T prefix C-b send-prefix\n' +
|
||||
'bind-key -T root F1 detach-client\n';
|
||||
function client(direct: string) {
|
||||
const calls: string[][] = [];
|
||||
const exec = async (_file: string, args: readonly string[]) => {
|
||||
calls.push([...args]);
|
||||
return { stdout: args.includes('-T') ? direct : fullListing, stderr: '' };
|
||||
};
|
||||
return { tui: new TuiClient({ socket: 'cod563', exec }), calls };
|
||||
}
|
||||
|
||||
it('falls back to the full listing when the table answer is blank', async () => {
|
||||
const { tui, calls } = client('');
|
||||
expect(await tui.readPrefixBinding('C-b')).toBe('send-prefix');
|
||||
expect(await tui.readPrefixBinding('F1', 'root')).toBe('detach-client');
|
||||
expect(await tui.readPrefixBinding('x')).toBeNull();
|
||||
expect(calls.some((args) => args.length === 3 && args[2] === 'list-keys')).toBe(true);
|
||||
});
|
||||
|
||||
it('keeps the table answer when there is one, with no second call', async () => {
|
||||
const { tui, calls } = client('bind-key -T prefix d detach-client\n');
|
||||
expect(await tui.readDetachKey()).toBe('d');
|
||||
expect(calls).toEqual([['-L', 'cod563', 'list-keys', '-T', 'prefix']]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user