Force queued messages, wrap long terminal input and questions, Ctrl+J newline in the web UI
A further Enter on the empty input (terminal client, web UI) or Ctrl+Enter with text (web UI)
interrupts the running step and delivers the queued messages at once (POST /api/sessions/{id}/queue/force).
The terminal client shows queued messages above the input, wraps the input field and the question dialog.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
679151defc
commit
e64328aa98
13 files changed
+437
-42
No files matched your search
+99
-5
@@ -1,6 +1,6 @@
|
||||
//! Multi-line input field with history.
|
||||
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use unicode_width::UnicodeWidthChar;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Editor {
|
||||
@@ -211,9 +211,103 @@ impl Editor {
|
||||
self.col = start + with.chars().count();
|
||||
}
|
||||
|
||||
/// Cursor position in display columns (for the terminal cursor).
|
||||
pub fn cursor_x(&self) -> usize {
|
||||
let line = &self.lines[self.row];
|
||||
UnicodeWidthStr::width(&line[..byte_idx(line, self.col)])
|
||||
/// The text as it is shown at `width` columns: every logical line is broken into rows, preferably after a
|
||||
/// space, otherwise inside a long word. Each row is (line index, first char, end char).
|
||||
pub fn layout(&self, width: usize) -> Vec<(usize, usize, usize)> {
|
||||
let width = width.max(1);
|
||||
let mut rows = Vec::new();
|
||||
for (i, line) in self.lines.iter().enumerate() {
|
||||
let chars: Vec<char> = line.chars().collect();
|
||||
if chars.is_empty() {
|
||||
rows.push((i, 0, 0));
|
||||
continue;
|
||||
}
|
||||
let mut start = 0;
|
||||
while start < chars.len() {
|
||||
let mut w = 0;
|
||||
let mut end = start;
|
||||
let mut last_space = None;
|
||||
while end < chars.len() {
|
||||
let cw = UnicodeWidthChar::width(chars[end]).unwrap_or(0);
|
||||
if w + cw > width && end > start {
|
||||
break;
|
||||
}
|
||||
w += cw;
|
||||
end += 1;
|
||||
if chars[end - 1] == ' ' {
|
||||
last_space = Some(end);
|
||||
}
|
||||
}
|
||||
if end < chars.len() {
|
||||
if let Some(sp) = last_space.filter(|sp| *sp > start) {
|
||||
end = sp;
|
||||
}
|
||||
}
|
||||
rows.push((i, start, end));
|
||||
start = end;
|
||||
}
|
||||
}
|
||||
rows
|
||||
}
|
||||
|
||||
/// The text of one row of `layout`.
|
||||
pub fn row_text(&self, (line, a, b): (usize, usize, usize)) -> String {
|
||||
self.lines[line].chars().skip(a).take(b - a).collect()
|
||||
}
|
||||
|
||||
/// Cursor in the wrapped view: (row index in `layout`, display column).
|
||||
pub fn cursor_in(&self, layout: &[(usize, usize, usize)]) -> (usize, usize) {
|
||||
for (n, &(line, a, b)) in layout.iter().enumerate() {
|
||||
let last = layout.get(n + 1).is_none_or(|next| next.0 != line);
|
||||
if line == self.row && self.col >= a && (self.col < b || (last && self.col <= b)) {
|
||||
let w: usize = self.lines[line].chars().skip(a).take(self.col - a).map(|c| UnicodeWidthChar::width(c).unwrap_or(0)).sum();
|
||||
return (n, w);
|
||||
}
|
||||
}
|
||||
(layout.len().saturating_sub(1), 0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn editor(text: &str) -> Editor {
|
||||
let mut e = Editor { lines: vec![String::new()], ..Default::default() };
|
||||
e.set(text);
|
||||
e
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_line_wraps_at_spaces() {
|
||||
let e = editor("aaa bbb ccc ddd");
|
||||
let rows: Vec<String> = e.layout(8).into_iter().map(|r| e.row_text(r)).collect();
|
||||
assert_eq!(rows, vec!["aaa bbb ", "ccc ddd"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_word_breaks_inside() {
|
||||
let e = editor("abcdefghij");
|
||||
let rows: Vec<String> = e.layout(4).into_iter().map(|r| e.row_text(r)).collect();
|
||||
assert_eq!(rows, vec!["abcd", "efgh", "ij"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_lines_keep_a_row_and_nothing_is_lost() {
|
||||
let e = editor("one\n\ntwo words here");
|
||||
let l = e.layout(6);
|
||||
let joined: String = l.iter().map(|r| e.row_text(*r)).collect::<Vec<_>>().join("|");
|
||||
assert_eq!(joined, "one||two |words |here");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cursor_follows_the_wrap() {
|
||||
let mut e = editor("aaa bbb ccc ddd");
|
||||
let l = e.layout(8);
|
||||
assert_eq!(e.cursor_in(&l), (1, 7)); // end of the text, second row
|
||||
e.col = 8; // before "ccc"
|
||||
assert_eq!(e.cursor_in(&l), (1, 0));
|
||||
e.col = 3;
|
||||
assert_eq!(e.cursor_in(&l), (0, 3));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user