feat: cursor movement in ask_user free-form input
- Replace bare string with Editor in choose.rs typing input - Support ←/→ (left/right arrow keys) for cursor movement - Add Home/End, ctrl-a/ctrl-e, Delete, Up/Down between lines - Keep ctrl-w (kill word), add ctrl-u (kill line) - Render cursor marker ▏ at actual position in multi-line answers - Update help text to mention cursor movement - Add test: type, move cursor, insert character mid-text Also: delete 4 comment lines from scripts/lint-comments.sh (tautology). Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a94632ea3d
commit
ec1c4fb41a
2 files changed
+51
-30
No files matched your search
+51
-26
@@ -1,3 +1,4 @@
|
||||
use crate::editor::Editor;
|
||||
use crate::theme;
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
use hx_core::Question;
|
||||
@@ -14,7 +15,7 @@ pub struct Choose {
|
||||
q: Question,
|
||||
sel: usize,
|
||||
picked: Vec<bool>,
|
||||
typing: Option<String>,
|
||||
typing: Option<Editor>,
|
||||
direct: bool,
|
||||
}
|
||||
|
||||
@@ -31,7 +32,7 @@ impl Choose {
|
||||
|
||||
pub fn input(q: Question) -> Self {
|
||||
let mut c = Self::new(q);
|
||||
c.typing = Some(String::new());
|
||||
c.typing = Some(Editor::default());
|
||||
c.direct = true;
|
||||
c
|
||||
}
|
||||
@@ -60,31 +61,34 @@ impl Choose {
|
||||
return Act::Done(None);
|
||||
}
|
||||
let picked = if self.q.multi { self.labels() } else { vec![] };
|
||||
if let Some(t) = &mut self.typing {
|
||||
if let Some(ed) = &mut self.typing {
|
||||
let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
|
||||
match k.code {
|
||||
KeyCode::Esc if self.direct => return Act::Done(None),
|
||||
KeyCode::Esc => self.typing = None,
|
||||
KeyCode::Backspace => {
|
||||
t.pop();
|
||||
}
|
||||
KeyCode::Char('w') if k.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
let n = t
|
||||
.trim_end()
|
||||
.trim_end_matches(|c: char| !c.is_whitespace())
|
||||
.len();
|
||||
t.truncate(n);
|
||||
}
|
||||
KeyCode::Char('j') if k.modifiers.contains(KeyModifiers::CONTROL) => t.push('\n'),
|
||||
KeyCode::Backspace => ed.backspace(),
|
||||
KeyCode::Delete => ed.delete(),
|
||||
KeyCode::Left => ed.left(),
|
||||
KeyCode::Right => ed.right(),
|
||||
KeyCode::Up => ed.up(),
|
||||
KeyCode::Down => ed.down(),
|
||||
KeyCode::Home => ed.home(),
|
||||
KeyCode::End => ed.end(),
|
||||
KeyCode::Char('a') if ctrl => ed.home(),
|
||||
KeyCode::Char('e') if ctrl => ed.end(),
|
||||
KeyCode::Char('u') if ctrl => ed.kill_line(),
|
||||
KeyCode::Char('w') if ctrl => ed.kill_word(),
|
||||
KeyCode::Char('j') if ctrl => ed.insert("\n"),
|
||||
KeyCode::Enter
|
||||
if k.modifiers
|
||||
.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT) =>
|
||||
{
|
||||
t.push('\n')
|
||||
ed.insert("\n")
|
||||
}
|
||||
KeyCode::Char(c) if !k.modifiers.contains(KeyModifiers::CONTROL) => t.push(c),
|
||||
KeyCode::Enter if !t.trim().is_empty() => {
|
||||
KeyCode::Char(c) if !ctrl => ed.insert(c.encode_utf8(&mut [0; 4])),
|
||||
KeyCode::Enter if !ed.buf.trim().is_empty() => {
|
||||
let mut v = picked;
|
||||
v.push(t.trim().to_string());
|
||||
v.push(ed.buf.trim().to_string());
|
||||
return Act::Done(Some(v));
|
||||
}
|
||||
_ => {}
|
||||
@@ -117,14 +121,14 @@ impl Choose {
|
||||
}
|
||||
|
||||
pub fn paste(&mut self, s: &str) {
|
||||
if let Some(t) = &mut self.typing {
|
||||
t.push_str(&s.replace("\r\n", "\n").replace('\r', "\n"));
|
||||
if let Some(ed) = &mut self.typing {
|
||||
ed.insert(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn enter(&mut self) -> Act {
|
||||
if self.is_other() || self.rows() == 0 {
|
||||
self.typing = Some(String::new());
|
||||
self.typing = Some(Editor::default());
|
||||
return Act::None;
|
||||
}
|
||||
Act::Done(Some(self.labels()))
|
||||
@@ -132,7 +136,7 @@ impl Choose {
|
||||
|
||||
pub fn help(&self) -> &'static str {
|
||||
if self.typing.is_some() {
|
||||
"enter send · shift/alt-enter newline · ctrl-w word · esc back"
|
||||
"enter send · ←→ move · shift/alt-enter newline · ctrl-w word · esc back"
|
||||
} else if self.q.multi {
|
||||
"↑↓ move · space toggle · enter confirm · esc skip"
|
||||
} else {
|
||||
@@ -182,10 +186,12 @@ impl Choose {
|
||||
if self.is_other() {
|
||||
sel_line = lines.len();
|
||||
}
|
||||
if let Some(t) = &self.typing {
|
||||
if let Some(ed) = &self.typing {
|
||||
let pre = format!("{} other: ", n + 1);
|
||||
let rows = hard_wrap(&format!("{pre}{t}▏"), inner);
|
||||
sel_line = lines.len() + rows.len() - 1;
|
||||
let (a, b) = ed.buf.split_at(ed.cur);
|
||||
let rows = hard_wrap(&format!("{pre}{a}▏{b}"), inner);
|
||||
let before = hard_wrap(&format!("{pre}{a}▏"), inner).len();
|
||||
sel_line = lines.len() + before - 1;
|
||||
for (i, r) in rows.into_iter().enumerate() {
|
||||
if i == 0 {
|
||||
let k = pre.len().min(r.len());
|
||||
@@ -331,6 +337,24 @@ mod tests {
|
||||
assert!(matches!(c.key(k(KeyCode::Esc)), Act::Done(None)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrows_move_the_cursor() {
|
||||
let mut c = Choose::input(Question {
|
||||
text: "why?".into(),
|
||||
options: vec![],
|
||||
multi: false,
|
||||
other: true,
|
||||
plan: None,
|
||||
});
|
||||
for ch in "abc".chars() {
|
||||
c.key(k(KeyCode::Char(ch)));
|
||||
}
|
||||
c.key(k(KeyCode::Left));
|
||||
c.key(k(KeyCode::Left));
|
||||
c.key(k(KeyCode::Char('X')));
|
||||
assert_eq!(done(c.key(k(KeyCode::Enter))), Some(vec!["aXbc".into()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_toggle_and_other_text() {
|
||||
let mut c = q(true);
|
||||
@@ -365,8 +389,9 @@ mod tests {
|
||||
c.key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
|
||||
assert!(
|
||||
c.typing
|
||||
.as_deref()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.buf
|
||||
.ends_with("pasted\nmore".trim_end_matches("more"))
|
||||
);
|
||||
for ch in "tail".chars() {
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
# Ratchet lint: comments are banned. Each file may hold at most the number of
|
||||
# comment lines recorded in .comment-baseline; the number can only go down.
|
||||
# scripts/lint-comments.sh check (CI)
|
||||
# scripts/lint-comments.sh --update rewrite the baseline from the tree
|
||||
set -euo pipefail
|
||||
cd "$(git rev-parse --show-toplevel)"
|
||||
|
||||
|
||||
Reference in new issue
Block a user