Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

hn: any key can be the prefix, set as `set -g prefix` sets it

Tim61e0a524098f
diff --git a/tui/src/config.rs b/tui/src/config.rs
index 89a2826a8..224a01f27 100644
--- a/tui/src/config.rs
+++ b/tui/src/config.rs
@@ -46,6 +46,9 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
pub struct Chord { pub code: KeyCode, pub mods: KeyModifiers }
impl Chord {
+ /// A key typed as itself — no Ctrl, ⌥ or ⌘ (Shift alone still types): `a`, `` ` ``, Enter, F12.
+ pub fn plain(&self) -> bool { !self.mods.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER) }
+
/// One spelling per key: letters lower-case with SHIFT as a modifier; a shifted symbol (`{`,
/// `?`) is its own character, without SHIFT.
pub fn normal(code: KeyCode, mods: KeyModifiers) -> Chord {
diff --git a/tui/src/input.rs b/tui/src/input.rs
index 6851912bb..6ed7c9432 100644
--- a/tui/src/input.rs
+++ b/tui/src/input.rs
@@ -131,9 +131,11 @@ fn on_key(app: &mut App, key: KeyEvent) {
app.repeat_until = None;
}
// The prefix works over the lists too (they are tmux's choose modes); only a line being typed
- // at the status line keeps it.
+ // at the status line keeps it — and a plain-key prefix (`` ` ``, Enter) is the prefix over the
+ // panes alone: in a list it is typed into the search, or chooses.
let line_edit = matches!(app.modal, Some(Modal::Prompt(_)) | Some(Modal::Confirm { .. }) | Some(Modal::Popup { .. }) | Some(Modal::Menu { .. }) | Some(Modal::NewHarness(_)));
- if !line_edit && (chord == app.keymap.prefix || Some(chord) == app.keymap.prefix2) {
+ let listed = chord.plain() && app.modal.is_some();
+ if !line_edit && !listed && (chord == app.keymap.prefix || Some(chord) == app.keymap.prefix2) {
app.status_redraws += 1;
app.prefix = true;
app.prefix_at = Some(std::time::Instant::now());
diff --git a/tui/src/modal.rs b/tui/src/modal.rs
index 0a148c5a7..e077bd9bd 100644
--- a/tui/src/modal.rs
+++ b/tui/src/modal.rs
@@ -566,8 +566,8 @@ pub const KEYBINDS: &[(&str, &str, &str)] = &[
pub fn keybind_rows(app: &App) -> Vec<Row> {
let prefix = crate::keys::name(&app.keymap.prefix);
let prefixes = [
- Row::new("prefix", "Prefix").detail(vec![span("the key before every command — Enter changes it", fg(theme::MUTED))]).right(prefix.clone()).group("Prefix"),
- Row::new("prefix2", "Second prefix").detail(vec![span("one more, for a terminal that keeps ⌘ keys", fg(theme::MUTED))])
+ Row::new("prefix", "Prefix").detail(vec![span("the key before every command", fg(theme::MUTED))]).right(prefix.clone()).group("Prefix"),
+ Row::new("prefix2", "Second prefix").detail(vec![span("another key that works as the prefix", fg(theme::MUTED))])
.right(app.keymap.prefix2.map(|c| crate::keys::name(&c)).unwrap_or_else(|| "none".into())).group("Prefix"),
];
prefixes.into_iter().chain(KEYBINDS.iter().enumerate().map(|(i, (title, runs, group))| {
@@ -878,30 +878,33 @@ mod theme_row_tests {
assert!(theme_sections(&app).iter().all(|r| r.id != "section:keys") && theme_options(&app, "keys").is_empty());
}
- /// The prefix changes in the panel: Enter, then the key — which needs Ctrl, ⌥ or ⌘ (a plain
- /// key would be lost to typing). The second prefix too; ⌫ leaves none. Both saved to tui.toml.
+ /// Any key the terminal sends becomes the prefix at once — Esc cancels, so not Esc — as `set -g
+ /// prefix` does: no binding added or changed (prefix Enter is still Commands with Enter as the
+ /// prefix). The second prefix the same; ⌫ none. Saved to tui.toml.
#[test]
- fn the_prefix_changes_to_a_key_with_a_modifier() {
+ fn any_key_becomes_the_prefix_and_no_binding_changes() {
let mut app = app();
- assert!(crate::settings::set_key(&mut app, "prefix", "").is_some_and(|s| s.contains("press")));
- for code in [KeyCode::Char('a'), KeyCode::Enter, KeyCode::Char('1')] {
- assert!(press(&mut app, code, KeyModifiers::NONE).contains("needs Ctrl"), "{code:?} refused");
- assert!(app.capturing.is_some(), "still waiting");
+ let prefix = |app: &App| crate::keys::name(&app.keymap.prefix);
+ let set = |app: &mut App, knob: &str| crate::settings::set_key(app, knob, "").unwrap_or_default();
+ let before: Vec<(String, String)> = app.keymap.prefix_table.iter().map(|b| (crate::keys::name(&b.chord), b.command.clone())).collect();
+ for (code, mods, name) in [(KeyCode::Char('a'), KeyModifiers::CONTROL, "C-a"), (KeyCode::Enter, KeyModifiers::NONE, "Enter"), (KeyCode::F(12), KeyModifiers::NONE, "F12"), (KeyCode::Char('`'), KeyModifiers::NONE, "`")] {
+ assert_eq!(set(&mut app, "prefix"), "Prefix: press a key · Esc cancels");
+ assert_eq!(press(&mut app, code, mods), format!("Prefix: {name} — saved"));
+ assert_eq!(prefix(&app), name);
+ assert!(app.capturing.is_none());
}
- assert_eq!(press(&mut app, KeyCode::Char('a'), KeyModifiers::CONTROL), "Prefix: C-a — saved to tui.toml");
- assert_eq!(crate::keys::name(&app.keymap.prefix), "C-a");
- assert!(app.capturing.is_none());
- // The second prefix, then none.
- let _ = crate::settings::set_key(&mut app, "prefix2", "");
- assert!(press(&mut app, KeyCode::Char('b'), KeyModifiers::CONTROL).starts_with("Second prefix: C-b"));
+ let after: Vec<(String, String)> = app.keymap.prefix_table.iter().map(|b| (crate::keys::name(&b.chord), b.command.clone())).collect();
+ assert_eq!(after, before, "no binding added or changed");
+ assert_eq!(runs(&app, KeyCode::Enter, KeyModifiers::NONE).as_deref(), Some("choose-command"), "prefix Enter is still Commands");
+ let _ = set(&mut app, "prefix");
+ assert_eq!(press(&mut app, KeyCode::Esc, KeyModifiers::NONE), "Unchanged");
+ assert_eq!(prefix(&app), "`");
+ assert_eq!(set(&mut app, "prefix2"), "Second prefix: press a key · ⌫ none · Esc cancels");
+ assert_eq!(press(&mut app, KeyCode::Char('b'), KeyModifiers::CONTROL), "Second prefix: C-b — saved");
assert_eq!(app.keymap.prefix2.map(|c| crate::keys::name(&c)).as_deref(), Some("C-b"));
- let _ = crate::settings::set_key(&mut app, "prefix2", "");
- assert_eq!(press(&mut app, KeyCode::Backspace, KeyModifiers::NONE), "Second prefix: none — saved to tui.toml");
+ let _ = set(&mut app, "prefix2");
+ assert_eq!(press(&mut app, KeyCode::Backspace, KeyModifiers::NONE), "Second prefix: none — saved");
assert_eq!(app.keymap.prefix2, None);
- // Esc: unchanged.
- let _ = crate::settings::set_key(&mut app, "prefix", "");
- assert_eq!(press(&mut app, KeyCode::Esc, KeyModifiers::NONE), "Unchanged");
- assert_eq!(crate::keys::name(&app.keymap.prefix), "C-a");
}
/// A key chosen is the command's: its old key given up, the command in full kept (Rename
diff --git a/tui/src/settings.rs b/tui/src/settings.rs
index 039a96bb1..97924792a 100644
--- a/tui/src/settings.rs
+++ b/tui/src/settings.rs
@@ -105,7 +105,7 @@ pub fn cursor_to(picker: &mut Picker, id: &str) { picker.select(id) }
// ── keys: the prefix and a command's key, chosen in the panel ────────────────────
/// What the next key becomes: the prefix ([second]: the second one), or [command]'s key after the
-/// prefix. [pending]: a key that runs another command, pressed once — the same key again replaces it.
+/// prefix. [pending]: a key that runs another command, pressed once — the same key again takes it.
#[derive(Clone, Debug)]
pub enum Capture {
Prefix { second: bool },
@@ -124,39 +124,37 @@ pub fn set_key(app: &mut App, knob: &str, value: &str) -> Option<String> {
app.capturing = Some(Capture::Command { command: runs.to_string(), title: title.to_string(), own: true, pending: None });
Some(format!("{}: {PRESS}", title.trim_end_matches('…')))
}
- "prefix" => { app.capturing = Some(Capture::Prefix { second: false }); Some(format!("Prefix: press the new one — Ctrl, ⌥ or ⌘ with a key · Esc cancels")) }
- "prefix2" => { app.capturing = Some(Capture::Prefix { second: true }); Some(format!("Second prefix: press it — Ctrl, ⌥ or ⌘ with a key · ⌫ none · Esc cancels")) }
+ "prefix" => { app.capturing = Some(Capture::Prefix { second: false }); Some(format!("Prefix: {PRESS}")) }
+ "prefix2" => { app.capturing = Some(Capture::Prefix { second: true }); Some("Second prefix: press a key · ⌫ none · Esc cancels".into()) }
_ => None,
}
}
-/// The prefix (or the second one, [chord] None: none) set as `set -g prefix` sets it — every hn
-/// window takes it — and written to tui.toml, so it is the prefix the next time hn starts too.
-fn apply_prefix(app: &mut App, second: bool, chord: Option<crate::keys::Chord>) -> String {
+/// `set -g prefix` (or `prefix2`; [chord] None: none) — every hn window takes it — and tui.toml's
+/// `prefix`, so it is the prefix the next time hn starts too.
+fn set_prefix_option(app: &mut App, second: bool, chord: Option<crate::keys::Chord>) -> std::io::Result<()> {
let option = if second { "prefix2" } else { "prefix" };
let name = chord.map(|c| crate::keys::name(&c)).unwrap_or_else(|| "None".to_string());
crate::commands::execute(app, &format!("set -g {option} {}", crate::tmuxconf::quote_word(&name)));
app.server_dirty = true;
- let saved = crate::config::write_top(option, chord.is_some().then_some(name.as_str()));
+ crate::config::write_top(option, chord.is_some().then_some(name.as_str()))
+}
+
+/// The prefix (or the second one, [chord] None: none) set and saved — as `set -g prefix` does in
+/// tmux: no binding added or changed (what the key ran after the prefix it still runs).
+fn apply_prefix(app: &mut App, second: bool, chord: Option<crate::keys::Chord>) -> String {
let what = if second { "Second prefix" } else { "Prefix" };
- let shown = if chord.is_some() { name } else { "none".into() };
- match saved { Ok(()) => format!("{what}: {shown} — saved to tui.toml"), Err(e) => format!("{what}: {shown} — could not write tui.toml: {e}") }
+ let shown = chord.map(|c| crate::keys::name(&c)).unwrap_or_else(|| "none".into());
+ match set_prefix_option(app, second, chord) { Ok(()) => format!("{what}: {shown} — saved"), Err(e) => format!("{what}: {shown} — could not save: {e}") }
}
-/// The key pressed for a prefix: one with Ctrl, ⌥ or ⌘ (a plain key would be lost to typing);
-/// for the second, ⌫ is none. (Said, and whether it is still waiting.)
-fn captured_prefix(app: &mut App, second: bool, key: crossterm::event::KeyEvent) -> (String, bool) {
- use crossterm::event::{KeyCode, KeyModifiers};
- let chord = crate::keys::of(&key);
- let name = crate::keys::name(&chord);
- if key.code == KeyCode::Esc && key.modifiers.is_empty() { return ("Unchanged".into(), false) }
- if second && key.code == KeyCode::Backspace && key.modifiers.is_empty() { return (apply_prefix(app, true, None), false) }
- if !key.modifiers.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER | KeyModifiers::META) {
- return (format!("{name}: a prefix needs Ctrl, ⌥ or ⌘ — like C-a · Esc cancels"), true)
- }
- if second && chord == app.keymap.prefix { return (format!("{name} is the prefix already — {AGAIN}"), true) }
- if !second && Some(chord) == app.keymap.prefix2 { return (format!("{name} is the second prefix — {AGAIN}"), true) }
- (apply_prefix(app, second, Some(chord)), false)
+/// The key pressed for a prefix: any key the terminal sends becomes it at once (Esc cancels; for
+/// the second, ⌫ is none). A key that also runs something is the person's to choose, as in tmux.
+fn captured_prefix(app: &mut App, second: bool, key: crossterm::event::KeyEvent) -> String {
+ use crossterm::event::KeyCode;
+ if key.code == KeyCode::Esc && key.modifiers.is_empty() { return "Unchanged".into() }
+ if second && key.code == KeyCode::Backspace && key.modifiers.is_empty() { return apply_prefix(app, true, None) }
+ apply_prefix(app, second, Some(crate::keys::of(&key)))
}
/// Alt-k on a command in the command panel: its key is the next key you press. A command with
@@ -179,8 +177,7 @@ pub fn captured(app: &mut App, key: crossterm::event::KeyEvent) {
let (command, title, own, pending) = match app.capturing.take() {
Some(Capture::Command { command, title, own, pending }) => (command, title, own, pending),
Some(Capture::Prefix { second }) => {
- let (said, waiting) = captured_prefix(app, second, key);
- if waiting { app.capturing = Some(Capture::Prefix { second }) }
+ let said = captured_prefix(app, second, key);
return show_captured(app, said);
}
None => return,
diff --git a/tui/src/ui.rs b/tui/src/ui.rs
index 15e785a96..dbcfb7b92 100644
--- a/tui/src/ui.rs
+++ b/tui/src/ui.rs
@@ -3084,6 +3084,28 @@ mod theme_render_tests {
// ── keys ──
+ /// A plain-key prefix (`` ` ``, Enter) is the prefix over the panes, and only there: in a list
+ /// or a line being typed, the key is the list's (typed into the search, Enter chooses). A prefix
+ /// with a modifier works over the lists, as before.
+ #[test]
+ fn a_plain_key_prefix_leaves_the_lists_their_keys() {
+ use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
+ let mut app = app();
+ app.keymap.prefix = crate::keys::parse("`").unwrap();
+ let key = |app: &mut App, code: KeyCode, mods: KeyModifiers| crate::input::handle(app, Event::Key(KeyEvent::new(code, mods)));
+ key(&mut app, KeyCode::Char('`'), KeyModifiers::NONE);
+ assert!(app.prefix, "over the panes, ` is the prefix");
+ app.prefix = false;
+ crate::commands::execute_bound(&mut app, "choose-command");
+ key(&mut app, KeyCode::Char('`'), KeyModifiers::NONE);
+ let Some(Modal::Picker { picker, .. }) = &app.modal else { panic!("closed") };
+ assert!(!app.prefix && picker.query == "`", "in a list, ` is typed: {:?}", picker.query);
+ // A prefix with a modifier still works over a list.
+ app.keymap.prefix = crate::keys::parse("C-a").unwrap();
+ key(&mut app, KeyCode::Char('a'), KeyModifiers::CONTROL);
+ assert!(app.prefix, "C-a over a list is the prefix");
+ }
+
/// Keybinds from the command list: its own panel in the same place — no preview, the
/// prefix first and fixed — where Enter on a command waits for its key, a key in use is
/// replaced on its second press, and Esc steps back to the commands.
diff --git a/tui/tests/keybinds.py b/tui/tests/keybinds.py
index b51aae61e..57fe33a92 100644
--- a/tui/tests/keybinds.py
+++ b/tui/tests/keybinds.py
@@ -112,23 +112,23 @@ try:
assert panes() == before + 1, 'C-b % still free after a restart'
print('PASS Keybinds: after a restart C-b h still splits', flush=True)
- # The prefix: Keybinds → Prefix → Enter → C-a. A plain key is refused; C-a is saved to tui.toml.
+ # The prefix, any key: Keybinds → Prefix → Enter → ` — set at once, as `set -g prefix` does.
+ # Saved to tui.toml; ` h splits, still after a restart.
keys('C-b', 'Enter'); shows('Commands')
type_text('keyb'); keys('Enter'); shows('Second prefix')
- keys('Enter'); shows('Prefix: press')
- keys('q'); shows('a prefix needs Ctrl')
- keys('C-a'); shows('Prefix: C-a')
+ keys('Enter'); shows('Prefix: press a key')
+ type_text('`'); shows('Prefix: ` — saved')
keys('Escape'); keys('Escape'); gone('Commands')
toml = TOML.read_text()
- assert 'prefix = "C-a"' in toml, toml
+ assert 'prefix = "`"' in toml and 'send-prefix' not in toml, toml
before = panes()
- keys('C-a', 'h'); wait(lambda: panes() == before + 1, 'C-a h splits')
+ type_text('`'); keys('h'); wait(lambda: panes() == before + 1, '` h splits')
hn('kill-server', ok=False); tmux('kill-server', ok=False); started = False
time.sleep(.3)
start(); started = True
before = panes()
- keys('C-a', 'h'); wait(lambda: panes() == before + 1, 'C-a h splits after a restart')
- print('PASS Keybinds: the prefix changed to C-a in the panel, saved, and still C-a after a restart', flush=True)
+ type_text('`'); keys('h'); wait(lambda: panes() == before + 1, '` h splits after a restart')
+ print('PASS Keybinds: ` as the prefix, set at once and saved, still ` after a restart', flush=True)
finally:
try: