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 crate::theme;
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
use hx_core::Question;
|
use hx_core::Question;
|
||||||
@@ -14,7 +15,7 @@ pub struct Choose {
|
|||||||
q: Question,
|
q: Question,
|
||||||
sel: usize,
|
sel: usize,
|
||||||
picked: Vec<bool>,
|
picked: Vec<bool>,
|
||||||
typing: Option<String>,
|
typing: Option<Editor>,
|
||||||
direct: bool,
|
direct: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ impl Choose {
|
|||||||
|
|
||||||
pub fn input(q: Question) -> Self {
|
pub fn input(q: Question) -> Self {
|
||||||
let mut c = Self::new(q);
|
let mut c = Self::new(q);
|
||||||
c.typing = Some(String::new());
|
c.typing = Some(Editor::default());
|
||||||
c.direct = true;
|
c.direct = true;
|
||||||
c
|
c
|
||||||
}
|
}
|
||||||
@@ -60,31 +61,34 @@ impl Choose {
|
|||||||
return Act::Done(None);
|
return Act::Done(None);
|
||||||
}
|
}
|
||||||
let picked = if self.q.multi { self.labels() } else { vec![] };
|
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 {
|
match k.code {
|
||||||
KeyCode::Esc if self.direct => return Act::Done(None),
|
KeyCode::Esc if self.direct => return Act::Done(None),
|
||||||
KeyCode::Esc => self.typing = None,
|
KeyCode::Esc => self.typing = None,
|
||||||
KeyCode::Backspace => {
|
KeyCode::Backspace => ed.backspace(),
|
||||||
t.pop();
|
KeyCode::Delete => ed.delete(),
|
||||||
}
|
KeyCode::Left => ed.left(),
|
||||||
KeyCode::Char('w') if k.modifiers.contains(KeyModifiers::CONTROL) => {
|
KeyCode::Right => ed.right(),
|
||||||
let n = t
|
KeyCode::Up => ed.up(),
|
||||||
.trim_end()
|
KeyCode::Down => ed.down(),
|
||||||
.trim_end_matches(|c: char| !c.is_whitespace())
|
KeyCode::Home => ed.home(),
|
||||||
.len();
|
KeyCode::End => ed.end(),
|
||||||
t.truncate(n);
|
KeyCode::Char('a') if ctrl => ed.home(),
|
||||||
}
|
KeyCode::Char('e') if ctrl => ed.end(),
|
||||||
KeyCode::Char('j') if k.modifiers.contains(KeyModifiers::CONTROL) => t.push('\n'),
|
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
|
KeyCode::Enter
|
||||||
if k.modifiers
|
if k.modifiers
|
||||||
.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT) =>
|
.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT) =>
|
||||||
{
|
{
|
||||||
t.push('\n')
|
ed.insert("\n")
|
||||||
}
|
}
|
||||||
KeyCode::Char(c) if !k.modifiers.contains(KeyModifiers::CONTROL) => t.push(c),
|
KeyCode::Char(c) if !ctrl => ed.insert(c.encode_utf8(&mut [0; 4])),
|
||||||
KeyCode::Enter if !t.trim().is_empty() => {
|
KeyCode::Enter if !ed.buf.trim().is_empty() => {
|
||||||
let mut v = picked;
|
let mut v = picked;
|
||||||
v.push(t.trim().to_string());
|
v.push(ed.buf.trim().to_string());
|
||||||
return Act::Done(Some(v));
|
return Act::Done(Some(v));
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -117,14 +121,14 @@ impl Choose {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn paste(&mut self, s: &str) {
|
pub fn paste(&mut self, s: &str) {
|
||||||
if let Some(t) = &mut self.typing {
|
if let Some(ed) = &mut self.typing {
|
||||||
t.push_str(&s.replace("\r\n", "\n").replace('\r', "\n"));
|
ed.insert(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter(&mut self) -> Act {
|
fn enter(&mut self) -> Act {
|
||||||
if self.is_other() || self.rows() == 0 {
|
if self.is_other() || self.rows() == 0 {
|
||||||
self.typing = Some(String::new());
|
self.typing = Some(Editor::default());
|
||||||
return Act::None;
|
return Act::None;
|
||||||
}
|
}
|
||||||
Act::Done(Some(self.labels()))
|
Act::Done(Some(self.labels()))
|
||||||
@@ -132,7 +136,7 @@ impl Choose {
|
|||||||
|
|
||||||
pub fn help(&self) -> &'static str {
|
pub fn help(&self) -> &'static str {
|
||||||
if self.typing.is_some() {
|
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 {
|
} else if self.q.multi {
|
||||||
"↑↓ move · space toggle · enter confirm · esc skip"
|
"↑↓ move · space toggle · enter confirm · esc skip"
|
||||||
} else {
|
} else {
|
||||||
@@ -182,10 +186,12 @@ impl Choose {
|
|||||||
if self.is_other() {
|
if self.is_other() {
|
||||||
sel_line = lines.len();
|
sel_line = lines.len();
|
||||||
}
|
}
|
||||||
if let Some(t) = &self.typing {
|
if let Some(ed) = &self.typing {
|
||||||
let pre = format!("{} other: ", n + 1);
|
let pre = format!("{} other: ", n + 1);
|
||||||
let rows = hard_wrap(&format!("{pre}{t}▏"), inner);
|
let (a, b) = ed.buf.split_at(ed.cur);
|
||||||
sel_line = lines.len() + rows.len() - 1;
|
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() {
|
for (i, r) in rows.into_iter().enumerate() {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
let k = pre.len().min(r.len());
|
let k = pre.len().min(r.len());
|
||||||
@@ -331,6 +337,24 @@ mod tests {
|
|||||||
assert!(matches!(c.key(k(KeyCode::Esc)), Act::Done(None)));
|
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]
|
#[test]
|
||||||
fn multi_toggle_and_other_text() {
|
fn multi_toggle_and_other_text() {
|
||||||
let mut c = q(true);
|
let mut c = q(true);
|
||||||
@@ -365,8 +389,9 @@ mod tests {
|
|||||||
c.key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
|
c.key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
|
||||||
assert!(
|
assert!(
|
||||||
c.typing
|
c.typing
|
||||||
.as_deref()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
.buf
|
||||||
.ends_with("pasted\nmore".trim_end_matches("more"))
|
.ends_with("pasted\nmore".trim_end_matches("more"))
|
||||||
);
|
);
|
||||||
for ch in "tail".chars() {
|
for ch in "tail".chars() {
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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
|
set -euo pipefail
|
||||||
cd "$(git rev-parse --show-toplevel)"
|
cd "$(git rev-parse --show-toplevel)"
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user