Files
hx/crates/hx-tui/src/popup.rs
T
johannes eaa6e22fbd
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s
Deepen Agent, compaction, switching and TUI modules
- Agent: make session/usage/provider/model/system/max_tokens/compact_at
  private; add set_provider, set_model, replace_session, fork, clear,
  fit_window. Fixes stale context-token reading after resume/new session.
- hx-core::compact: ContextMeter, plan/apply and estimate_tokens split out
  of agent.rs, testable without provider or session.
- hx-core::switch: Switcher shared by TUI and RPC for model/provider
  switches; RPC set_model now also refits the compaction threshold.
- hx-tui: status.rs (footer, /usage as pure functions), popup.rs, and
  Picker::key returning an Act.
- tests/tui_smoke.py: resolve the temp dir (macOS /var symlink) and match
  the resume via the OSC window title instead of split screen text.
2026-10-02 00:10:41 +02:00

113 lines
3.5 KiB
Rust

//! Completion popup state and its keys: a [`Completion`] plus the selected row.
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use hx_core::command::Completion;
/// What the app should do after a key reached the popup.
#[derive(Debug, PartialEq, Eq)]
pub enum Act {
/// The popup used the key (moved the selection).
Consumed,
/// Close the popup; the key is spent.
Close,
/// Insert the selected candidate into the editor.
Accept,
/// Not a popup key: the popup closes and the key goes to the editor as usual.
Pass,
}
pub struct Popup {
pub comp: Completion,
pub sel: usize,
}
impl Popup {
/// `None` when there is nothing to offer.
pub fn new(comp: Completion) -> Option<Self> {
(!comp.items.is_empty()).then_some(Self { comp, sel: 0 })
}
pub fn selected_label(&self) -> &str {
&self.comp.items[self.sel].label
}
/// `buf`/`cur` are the editor line and cursor: Enter accepts a partly typed token,
/// but on an empty or already complete one it passes through and submits.
pub fn key(&mut self, k: KeyEvent, buf: &str, cur: usize) -> Act {
let n = self.comp.items.len();
match k.code {
KeyCode::Up => self.sel = (self.sel + n - 1) % n,
KeyCode::Down => self.sel = (self.sel + 1) % n,
KeyCode::Esc => return Act::Close,
KeyCode::Tab => return Act::Accept,
KeyCode::Enter
if !k
.modifiers
.intersects(KeyModifiers::ALT | KeyModifiers::SHIFT)
&& !matches!(&buf[self.comp.start..cur], "" | "/" | "@")
&& buf[self.comp.start..cur] != *self.selected_label() =>
{
return Act::Accept;
}
_ => return Act::Pass,
}
Act::Consumed
}
}
#[cfg(test)]
mod tests {
use super::*;
use hx_core::command::Candidate;
fn pop() -> Popup {
let item = |l: &str| Candidate {
label: l.into(),
detail: String::new(),
};
Popup::new(Completion {
start: 0,
items: vec![item("/clear"), item("/compact")],
})
.unwrap()
}
fn press(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::NONE)
}
#[test]
fn empty_completion_has_no_popup() {
assert!(Popup::new(Completion::default()).is_none());
}
#[test]
fn arrows_wrap() {
let mut p = pop();
assert_eq!(p.key(press(KeyCode::Up), "/c", 2), Act::Consumed);
assert_eq!(p.selected_label(), "/compact");
assert_eq!(p.key(press(KeyCode::Down), "/c", 2), Act::Consumed);
assert_eq!(p.selected_label(), "/clear");
}
#[test]
fn tab_accepts_esc_closes_others_pass() {
let mut p = pop();
assert_eq!(p.key(press(KeyCode::Tab), "/c", 2), Act::Accept);
assert_eq!(p.key(press(KeyCode::Esc), "/c", 2), Act::Close);
assert_eq!(p.key(press(KeyCode::Char('x')), "/c", 2), Act::Pass);
}
#[test]
fn enter_accepts_only_a_partial_token() {
let mut p = pop();
assert_eq!(p.key(press(KeyCode::Enter), "/c", 2), Act::Accept);
// already complete, or nothing typed: submit
assert_eq!(p.key(press(KeyCode::Enter), "/clear", 6), Act::Pass);
assert_eq!(p.key(press(KeyCode::Enter), "/", 1), Act::Pass);
// alt/shift-enter is a newline
let alt = KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT);
assert_eq!(p.key(alt, "/c", 2), Act::Pass);
}
}