tui: ask popup own answer wraps, grows, supports paste/newline/ctrl-w
This commit is contained in:
1 parent
e96f4a6cb8
commit
19937d0063
2 files changed
+106
-19
No files matched your search
+101
-19
@@ -61,7 +61,21 @@ impl Choose {
|
||||
KeyCode::Backspace => {
|
||||
t.pop();
|
||||
}
|
||||
KeyCode::Char(c) => t.push(c),
|
||||
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::Enter
|
||||
if k.modifiers
|
||||
.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT) =>
|
||||
{
|
||||
t.push('\n')
|
||||
}
|
||||
KeyCode::Char(c) if !k.modifiers.contains(KeyModifiers::CONTROL) => t.push(c),
|
||||
KeyCode::Enter if !t.trim().is_empty() => {
|
||||
let mut v = picked;
|
||||
v.push(t.trim().to_string());
|
||||
@@ -96,6 +110,13 @@ impl Choose {
|
||||
Act::None
|
||||
}
|
||||
|
||||
/// Bracketed paste: append to the own answer while typing.
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
||||
fn enter(&mut self) -> Act {
|
||||
if self.is_other() || self.rows() == 0 {
|
||||
self.typing = Some(String::new());
|
||||
@@ -106,7 +127,7 @@ impl Choose {
|
||||
|
||||
pub fn help(&self) -> &'static str {
|
||||
if self.typing.is_some() {
|
||||
"enter send · esc back"
|
||||
"enter send · shift/alt-enter newline · ctrl-w word · esc back"
|
||||
} else if self.q.multi {
|
||||
"↑↓ move · space toggle · enter confirm · esc skip"
|
||||
} else {
|
||||
@@ -158,24 +179,32 @@ impl Choose {
|
||||
if self.is_other() {
|
||||
sel_line = lines.len();
|
||||
}
|
||||
lines.push(match &self.typing {
|
||||
Some(t) => Line::from(vec![
|
||||
if let Some(t) = &self.typing {
|
||||
let pre = format!("{} other: ", n + 1);
|
||||
let rows = hard_wrap(&format!("{pre}{t}▏"), inner);
|
||||
sel_line = lines.len() + rows.len() - 1;
|
||||
for (i, r) in rows.into_iter().enumerate() {
|
||||
if i == 0 {
|
||||
let k = pre.len().min(r.len());
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(r[..k].to_string(), th.accent()),
|
||||
Span::raw(r[k..].to_string()),
|
||||
]));
|
||||
} else {
|
||||
lines.push(Line::raw(r));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let st = if self.is_other() {
|
||||
th.sel()
|
||||
} else {
|
||||
th.plain()
|
||||
};
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(format!("{} ", n + 1), th.accent()),
|
||||
Span::styled("other: ", th.accent()),
|
||||
Span::raw(format!("{t}▏")),
|
||||
]),
|
||||
None => Line::from(vec![
|
||||
Span::styled(format!("{} ", n + 1), th.accent()),
|
||||
Span::styled(
|
||||
"other…",
|
||||
if self.is_other() {
|
||||
th.sel()
|
||||
} else {
|
||||
th.plain()
|
||||
},
|
||||
),
|
||||
]),
|
||||
});
|
||||
Span::styled("other…", st),
|
||||
]));
|
||||
}
|
||||
}
|
||||
let q = Paragraph::new(self.q.text.as_str())
|
||||
.wrap(Wrap { trim: false })
|
||||
@@ -211,6 +240,24 @@ impl Choose {
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap to `w` chars keeping all spaces/newlines; break at last space, else hard.
|
||||
fn hard_wrap(s: &str, w: usize) -> Vec<String> {
|
||||
let w = w.max(1);
|
||||
let mut out = vec![];
|
||||
for para in s.split('\n') {
|
||||
let mut cur: Vec<char> = vec![];
|
||||
for c in para.chars() {
|
||||
if cur.len() >= w {
|
||||
let at = cur.iter().rposition(|&x| x == ' ').map_or(w, |i| i + 1);
|
||||
out.push(cur.drain(..at).collect());
|
||||
}
|
||||
cur.push(c);
|
||||
}
|
||||
out.push(cur.into_iter().collect());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Greedy word wrap to `w` chars (a single longer word is kept whole).
|
||||
fn wrap(s: &str, w: usize) -> Vec<String> {
|
||||
let mut out: Vec<String> = vec![];
|
||||
@@ -289,6 +336,41 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_answer_wraps_and_stays_visible() {
|
||||
let mut c = q(false);
|
||||
c.key(k(KeyCode::Char('3')));
|
||||
for ch in "word ".repeat(30).chars().chain("END".chars()) {
|
||||
c.key(k(KeyCode::Char(ch)));
|
||||
}
|
||||
c.key(KeyEvent::new(KeyCode::Enter, KeyModifiers::SHIFT));
|
||||
c.paste("pasted\r\nmore");
|
||||
c.key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
|
||||
assert!(
|
||||
c.typing
|
||||
.as_deref()
|
||||
.unwrap()
|
||||
.ends_with("pasted\nmore".trim_end_matches("more"))
|
||||
);
|
||||
for ch in "tail".chars() {
|
||||
c.key(k(KeyCode::Char(ch)));
|
||||
}
|
||||
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
|
||||
term.draw(|f| c.draw(f, 23)).unwrap();
|
||||
let b = term.backend().buffer().clone();
|
||||
let all: String = (0..24)
|
||||
.map(|y| {
|
||||
(0..80)
|
||||
.map(|x| b[(x, y)].symbol().to_string())
|
||||
.collect::<String>()
|
||||
+ "\n"
|
||||
})
|
||||
.collect();
|
||||
assert!(all.contains("END") && all.contains("tail▏"), "{all}");
|
||||
assert_eq!(hard_wrap("aaaa bbbb cccc", 9), ["aaaa ", "bbbb cccc"]);
|
||||
assert_eq!(hard_wrap("abcdefgh", 3), ["abc", "def", "gh"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typing_esc_goes_back_and_renders() {
|
||||
let mut c = q(false);
|
||||
|
||||
@@ -506,6 +506,11 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
||||
}
|
||||
}
|
||||
TEvent::Mouse(m) => on_mouse(&mut app, m),
|
||||
TEvent::Paste(s) if app.choose.is_some() => {
|
||||
if let Some((c, _)) = &mut app.choose {
|
||||
c.paste(&s);
|
||||
}
|
||||
}
|
||||
TEvent::Paste(s) if app.perm.is_none() && app.choose.is_none() && app.ask.is_none() => app.ed.insert(&s),
|
||||
TEvent::Resize(..) => app.sel = None,
|
||||
_ => {}
|
||||
|
||||
Reference in new issue
Block a user