luxe update and /update, editable question answers, and a version bump owed once per release
- luxe update and /update update Luxe with the command for however it was installed (cargo or Homebrew), run one program at a time with the terminal attached, then check that the luxe on PATH is the new version. Luxe never downloads or replaces its own binary. The start screen says "Update available · /update". - A question's typed answer edits at its caret: arrows, word jumps, Home, End and Delete work, and up and down move through a wrapped answer before leaving it. - The version check owes a bump once per release: the version on main is the next release, and only a released one (a v-tag) has to move. - Dependency updates arrive through the sync, so Dependabot and the weekly jobs no longer run here.
This commit is contained in:
84
crates/luxe-tui/src/question_prompt/answer.rs
Normal file
84
crates/luxe-tui/src/question_prompt/answer.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
//! Answer — the typed answer while it is being written: its rows, its selection, and the caret.
|
||||
//!
|
||||
//! The answer is the composer's [`crate::editor::Editor`], so it is drawn from the editor's own
|
||||
//! wrap: the rows the reader sees are the rows up and down move through.
|
||||
|
||||
use super::QuestionPromptState;
|
||||
use crate::editor::{Editor, TextLayout, VisualSegment};
|
||||
use ratatui::style::Style;
|
||||
use ratatui::text::Span;
|
||||
|
||||
/// The glyph drawn where typing lands.
|
||||
const CARET: &str = "▏";
|
||||
|
||||
/// The rows of the answer being typed that fit in `max_rows`, the caret drawn where typing lands,
|
||||
/// and whether rows above were scrolled off.
|
||||
///
|
||||
/// Each row keeps a cell for the caret, and a scrolled answer one more for the mark that says so.
|
||||
/// The width it wrapped at is recorded on the state, so up and down move through these rows.
|
||||
pub(super) fn editing_rows(
|
||||
state: &QuestionPromptState,
|
||||
room: usize,
|
||||
max_rows: usize,
|
||||
(text, selected): (Style, Style),
|
||||
) -> (Vec<Vec<Span<'static>>>, bool) {
|
||||
let editor = state.answer();
|
||||
let max_rows = max_rows.max(1);
|
||||
let (width, layout) = fit(editor, room, max_rows);
|
||||
state.set_answer_width(width);
|
||||
let caret = editor.cursor_in(&layout);
|
||||
// The window follows the caret, so the row being typed in is always on screen
|
||||
let first = caret.row.saturating_sub(max_rows - 1);
|
||||
let rows = layout.rows()[first..]
|
||||
.iter()
|
||||
.take(max_rows)
|
||||
.enumerate()
|
||||
.map(|(offset, segments)| {
|
||||
let at = (first + offset == caret.row).then_some(caret.column);
|
||||
row_spans(segments, at, text, selected)
|
||||
})
|
||||
.collect();
|
||||
(rows, first > 0)
|
||||
}
|
||||
|
||||
/// The width the answer wraps at, and its wrap: a cell short of `room` for the caret, and one more
|
||||
/// when it takes more than `max_rows` and needs the scrolled mark.
|
||||
fn fit(editor: &Editor, room: usize, max_rows: usize) -> (u16, TextLayout) {
|
||||
let cells = |width: usize| u16::try_from(width.max(1)).unwrap_or(u16::MAX);
|
||||
let width = cells(room.saturating_sub(1));
|
||||
let layout = editor.layout(width);
|
||||
if layout.row_count() <= max_rows {
|
||||
return (width, layout);
|
||||
}
|
||||
let width = cells(room.saturating_sub(2));
|
||||
(width, editor.layout(width))
|
||||
}
|
||||
|
||||
/// One row's spans, the selection lit and the caret set before the cell at column `caret`.
|
||||
fn row_spans(
|
||||
segments: &[VisualSegment],
|
||||
caret: Option<usize>,
|
||||
text: Style,
|
||||
selected: Style,
|
||||
) -> Vec<Span<'static>> {
|
||||
let mut spans = Vec::new();
|
||||
let mut column = 0usize;
|
||||
for segment in segments {
|
||||
let style = if segment.is_selected { text.patch(selected) } else { text };
|
||||
let mut run = String::new();
|
||||
for character in segment.text.chars() {
|
||||
if caret == Some(column) {
|
||||
spans.push(Span::styled(std::mem::take(&mut run), style));
|
||||
spans.push(Span::styled(CARET, text));
|
||||
}
|
||||
run.push(character);
|
||||
column += unicode_width::UnicodeWidthChar::width(character).unwrap_or(0);
|
||||
}
|
||||
spans.push(Span::styled(run, style));
|
||||
}
|
||||
if caret.is_some_and(|at| at >= column) {
|
||||
spans.push(Span::styled(CARET, text));
|
||||
}
|
||||
spans.retain(|span| !span.content.is_empty());
|
||||
spans
|
||||
}
|
||||
@@ -68,22 +68,31 @@ fn row_content(
|
||||
// truncating: an answer cut off at the right edge is one the reader cannot check
|
||||
// before sending it.
|
||||
None => {
|
||||
let body = if state.editing() {
|
||||
format!("{}▏", state.custom())
|
||||
} else if state.custom().is_empty() {
|
||||
CUSTOM_PLACEHOLDER.to_string()
|
||||
} else {
|
||||
state.custom().to_string()
|
||||
};
|
||||
let number = format!("{}. ", i + 1);
|
||||
// The text column, wherever a longer number puts it
|
||||
let text_at = indent + number.chars().count().saturating_sub(3);
|
||||
let room = text_width.saturating_sub(text_at);
|
||||
let (mut wrapped, scrolled) = custom_view(&body, room, custom_rows, state.editing());
|
||||
let (mut wrapped, scrolled) = match state.editing() {
|
||||
true => {
|
||||
let styles = (label_style, theme.selected_bg);
|
||||
super::answer::editing_rows(state, room, custom_rows, styles)
|
||||
}
|
||||
false => {
|
||||
let body = match state.custom() {
|
||||
"" => CUSTOM_PLACEHOLDER,
|
||||
typed => typed,
|
||||
};
|
||||
let rows = wrapped_rows(body, room, custom_rows);
|
||||
(
|
||||
rows.into_iter().map(|row| vec![Span::styled(row, label_style)]).collect(),
|
||||
false,
|
||||
)
|
||||
}
|
||||
};
|
||||
// An answer of nothing but spaces still owns a row: it is the one the cursor
|
||||
// can land on, and the click regions are laid out over the rows there are.
|
||||
if wrapped.is_empty() {
|
||||
wrapped.push(String::new());
|
||||
wrapped.push(Vec::new());
|
||||
}
|
||||
for (index, row) in wrapped.into_iter().enumerate() {
|
||||
let mut line = if index == 0 {
|
||||
@@ -99,7 +108,7 @@ fn row_content(
|
||||
let lead = if index == 0 { "…" } else { " " };
|
||||
line.push(Span::styled(lead, cursor_style));
|
||||
}
|
||||
line.push(Span::styled(row, label_style));
|
||||
line.extend(row);
|
||||
content.push(line);
|
||||
}
|
||||
}
|
||||
@@ -176,24 +185,3 @@ pub(super) fn wrapped_rows(text: &str, width: usize, max_rows: usize) -> Vec<Str
|
||||
}
|
||||
rows
|
||||
}
|
||||
|
||||
/// The rows of a typed answer that fit, and whether any were left above them.
|
||||
///
|
||||
/// While the row is being edited the caret is what has to stay on screen, so the window follows
|
||||
/// the tail of the answer; at rest it reads from the top like any other text, with the usual
|
||||
/// clipped-tail mark.
|
||||
fn custom_view(text: &str, width: usize, max_rows: usize, editing: bool) -> (Vec<String>, bool) {
|
||||
if !editing {
|
||||
return (wrapped_rows(text, width, max_rows), false);
|
||||
}
|
||||
if max_rows == 0 {
|
||||
return (Vec::new(), false);
|
||||
}
|
||||
let rows = wrap_rows(text, width);
|
||||
if rows.len() <= max_rows {
|
||||
return (rows, false);
|
||||
}
|
||||
// a cell short, for the mark that says there is more above
|
||||
let mut rows = wrap_rows(text, width.saturating_sub(1));
|
||||
(rows.split_off(rows.len() - max_rows), true)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! Question prompt state — which row the cursor is on, what is checked, and what a key does.
|
||||
|
||||
use crate::editor::Editor;
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
use luxe_core::question::{default_selection, QuestionOption, QuestionRequestId};
|
||||
use std::cell::Cell;
|
||||
|
||||
/// State for an in-flight question modal (one at a time — tools dispatch sequentially).
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -18,9 +20,11 @@ pub struct QuestionPromptState {
|
||||
selected: usize,
|
||||
/// Per-option checkbox state (multi-select only).
|
||||
checked: Vec<bool>,
|
||||
/// The user's free-text answer.
|
||||
custom: String,
|
||||
/// Whether the free-text row is being edited (keystrokes append to `custom`).
|
||||
/// The user's free-text answer: the composer's editor, so the caret moves and edits where it is.
|
||||
custom: Editor,
|
||||
/// Cells the answer last wrapped at on screen, so up and down move through the rows drawn.
|
||||
answer_width: Cell<u16>,
|
||||
/// Whether the free-text row is being edited (keystrokes go to `custom`).
|
||||
editing: bool,
|
||||
/// Whether the typed answer is checked (multi-select): typing checks it, Space toggles it.
|
||||
custom_checked: bool,
|
||||
@@ -53,7 +57,8 @@ impl QuestionPromptState {
|
||||
multi,
|
||||
selected,
|
||||
checked,
|
||||
custom: String::new(),
|
||||
custom: Editor::new(),
|
||||
answer_width: Cell::new(u16::MAX),
|
||||
editing: false,
|
||||
custom_checked: false,
|
||||
}
|
||||
@@ -77,16 +82,26 @@ impl QuestionPromptState {
|
||||
/// Whether row `i` is checked (multi-select), the typed answer included.
|
||||
pub fn is_checked(&self, i: usize) -> bool {
|
||||
if i == self.custom_row() {
|
||||
return self.custom_checked && !self.custom.trim().is_empty();
|
||||
return self.custom_checked && !self.custom().trim().is_empty();
|
||||
}
|
||||
self.checked.get(i).copied().unwrap_or(false)
|
||||
}
|
||||
|
||||
/// The current free-text entry.
|
||||
pub fn custom(&self) -> &str {
|
||||
self.custom.current_input()
|
||||
}
|
||||
|
||||
/// The free-text entry's editor, for drawing it with its caret and selection.
|
||||
pub(crate) fn answer(&self) -> &Editor {
|
||||
&self.custom
|
||||
}
|
||||
|
||||
/// Records the width the answer was just drawn at; up and down wrap it the same way.
|
||||
pub(super) fn set_answer_width(&self, width: u16) {
|
||||
self.answer_width.set(width);
|
||||
}
|
||||
|
||||
/// Whether the free-text row is in edit mode.
|
||||
pub fn editing(&self) -> bool {
|
||||
self.editing
|
||||
@@ -99,7 +114,7 @@ impl QuestionPromptState {
|
||||
return;
|
||||
}
|
||||
self.start_typing();
|
||||
self.custom.push_str(&text);
|
||||
self.custom.insert_text(&text);
|
||||
}
|
||||
|
||||
/// Move the cursor to `index` (a hover or a click), clamped to range.
|
||||
@@ -136,7 +151,7 @@ impl QuestionPromptState {
|
||||
/// A single answer is one answer. Text left in the typed row while picking another option used
|
||||
/// to ride along with it, which made a choice and an abandoned draft one reply.
|
||||
fn collect(&self, option: Option<usize>) -> Vec<String> {
|
||||
let custom = self.custom.trim();
|
||||
let custom = self.custom().trim();
|
||||
if !self.multi {
|
||||
return match option {
|
||||
Some(i) => vec![self.options[i].label.clone()],
|
||||
@@ -206,7 +221,7 @@ impl QuestionPromptState {
|
||||
if self.multi {
|
||||
QuestionPromptOutcome::Answer(self.collect(None))
|
||||
} else if self.selected == self.custom_row() {
|
||||
if self.custom.trim().is_empty() {
|
||||
if self.custom().trim().is_empty() {
|
||||
self.editing = true;
|
||||
QuestionPromptOutcome::Consumed
|
||||
} else {
|
||||
@@ -217,9 +232,9 @@ impl QuestionPromptState {
|
||||
}
|
||||
}
|
||||
// Start typing anywhere jumps into the free-text row.
|
||||
KeyCode::Char(c) if !ctrl => {
|
||||
KeyCode::Char(_) if !ctrl => {
|
||||
self.start_typing();
|
||||
self.custom.push(c);
|
||||
self.custom.handle_key(*key);
|
||||
QuestionPromptOutcome::Consumed
|
||||
}
|
||||
_ => QuestionPromptOutcome::Consumed,
|
||||
@@ -234,31 +249,56 @@ impl QuestionPromptState {
|
||||
QuestionPromptOutcome::Consumed
|
||||
}
|
||||
KeyCode::Char('c') if ctrl => {
|
||||
self.custom.clear();
|
||||
QuestionPromptOutcome::Consumed
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
self.custom.pop();
|
||||
self.custom.select_all();
|
||||
self.custom.delete_selection();
|
||||
QuestionPromptOutcome::Consumed
|
||||
}
|
||||
KeyCode::Up | KeyCode::Down => self.vertical_in_answer(key),
|
||||
// The answer is one reply on one line, and Tab means nothing in it
|
||||
KeyCode::Tab | KeyCode::BackTab => QuestionPromptOutcome::Consumed,
|
||||
// Every Enter, Shift+Enter's newline included, is the question's to answer
|
||||
KeyCode::Enter => {
|
||||
if self.multi {
|
||||
// Commit the text and submit everything checked plus the free text.
|
||||
self.editing = false;
|
||||
QuestionPromptOutcome::Answer(self.collect(None))
|
||||
} else if self.custom.trim().is_empty() {
|
||||
} else if self.custom().trim().is_empty() {
|
||||
self.editing = false;
|
||||
QuestionPromptOutcome::Consumed
|
||||
} else {
|
||||
QuestionPromptOutcome::Answer(self.collect(None))
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) if !ctrl => {
|
||||
self.custom.push(c);
|
||||
self.custom_checked = true;
|
||||
// Typing, deleting and every caret motion are the composer's own
|
||||
_ => {
|
||||
let before = self.custom().len();
|
||||
self.custom.handle_key(*key);
|
||||
if self.custom().len() > before {
|
||||
self.custom_checked = true;
|
||||
}
|
||||
QuestionPromptOutcome::Consumed
|
||||
}
|
||||
_ => QuestionPromptOutcome::Consumed,
|
||||
}
|
||||
}
|
||||
|
||||
/// Up and down move through the rows the answer wraps to; from its first row up, or its last
|
||||
/// row down, they leave it for the options, as the launcher hands its arrows to the list.
|
||||
/// Shift always stays in the answer, where it is selecting.
|
||||
fn vertical_in_answer(&mut self, key: &KeyEvent) -> QuestionPromptOutcome {
|
||||
self.custom.set_viewport_width(self.answer_width.get());
|
||||
let up = key.code == KeyCode::Up;
|
||||
let edge =
|
||||
if up { self.custom.cursor_on_first_row() } else { self.custom.cursor_on_last_row() };
|
||||
if edge && !key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
self.editing = false;
|
||||
if up {
|
||||
self.move_up();
|
||||
} else {
|
||||
self.move_down();
|
||||
}
|
||||
} else {
|
||||
self.custom.handle_key(*key);
|
||||
}
|
||||
QuestionPromptOutcome::Consumed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -543,3 +543,84 @@ fn the_measured_height_is_the_height_it_draws() {
|
||||
}
|
||||
assert_eq!(height(&s, &theme, 0, 20), 0, "no width, no panel");
|
||||
}
|
||||
|
||||
fn type_answer(state: &mut QuestionPromptState, text: &str) {
|
||||
for c in text.chars() {
|
||||
state.on_key(&key(KeyCode::Char(c)));
|
||||
}
|
||||
}
|
||||
|
||||
/// The typed answer edits where its caret is: the arrows move it, and typing, Backspace and Delete
|
||||
/// act there rather than at the end of the text.
|
||||
#[test]
|
||||
fn the_typed_answer_edits_at_its_caret() {
|
||||
let mut state = state(false);
|
||||
type_answer(&mut state, "helo");
|
||||
state.on_key(&key(KeyCode::Left));
|
||||
state.on_key(&key(KeyCode::Left));
|
||||
state.on_key(&key(KeyCode::Char('l')));
|
||||
assert_eq!(state.custom(), "hello");
|
||||
state.on_key(&key(KeyCode::Home));
|
||||
state.on_key(&key(KeyCode::Delete));
|
||||
assert_eq!(state.custom(), "ello", "delete takes the character after the caret");
|
||||
state.on_key(&key(KeyCode::End));
|
||||
state.on_key(&key(KeyCode::Backspace));
|
||||
assert_eq!(state.custom(), "ell");
|
||||
assert!(state.editing(), "none of that left the answer");
|
||||
}
|
||||
|
||||
/// A word jump moves through the answer the way it does in the composer.
|
||||
#[test]
|
||||
fn a_word_jump_moves_through_the_typed_answer() {
|
||||
let mut state = state(false);
|
||||
type_answer(&mut state, "one two");
|
||||
state.on_key(&KeyEvent::new(KeyCode::Left, KeyModifiers::ALT));
|
||||
type_answer(&mut state, "big ");
|
||||
assert_eq!(state.custom(), "one big two");
|
||||
}
|
||||
|
||||
/// Up and down move through the rows a long answer wraps to, and up from its first row leaves the
|
||||
/// answer for the option above, the way the launcher hands its arrows to the list.
|
||||
#[test]
|
||||
fn up_and_down_move_through_a_wrapped_answer_then_leave_it() {
|
||||
let mut state = state(false);
|
||||
type_answer(&mut state, "alpha beta gamma delta epsilon zeta eta theta iota kappa");
|
||||
// Drawn once, so the answer has wrapped at the panel's width
|
||||
render(&state, 30, 24);
|
||||
let end = state.answer().cursor();
|
||||
|
||||
state.on_key(&key(KeyCode::Up));
|
||||
assert!(state.editing() && state.answer().cursor() < end, "up reached the row above");
|
||||
state.on_key(&key(KeyCode::Down));
|
||||
assert_eq!(state.answer().cursor(), end, "and down came back");
|
||||
|
||||
// Bounded, so a regression that never leaves fails rather than hangs
|
||||
for _ in 0..10 {
|
||||
if state.editing() {
|
||||
state.on_key(&key(KeyCode::Up));
|
||||
}
|
||||
}
|
||||
assert!(!state.editing(), "up from the first row leaves the answer");
|
||||
assert_eq!(state.selected(), 1, "for the option above it");
|
||||
}
|
||||
|
||||
/// An answer short enough for one row has nowhere to go: down leaves it at once.
|
||||
#[test]
|
||||
fn down_from_a_one_row_answer_leaves_it() {
|
||||
let mut state = state(false);
|
||||
type_answer(&mut state, "short");
|
||||
render(&state, 60, 24);
|
||||
state.on_key(&key(KeyCode::Down));
|
||||
assert!(!state.editing());
|
||||
assert_eq!(state.selected(), 0, "down from the last row wraps to the first option");
|
||||
}
|
||||
|
||||
/// The caret is drawn where typing lands, not after the last character.
|
||||
#[test]
|
||||
fn the_caret_is_drawn_where_typing_lands() {
|
||||
let mut state = state(false);
|
||||
type_answer(&mut state, "abc");
|
||||
state.on_key(&key(KeyCode::Left));
|
||||
let (text, _) = render(&state, 60, 12);
|
||||
assert!(text.contains("ab▏c"), "the caret sits before the c: {text}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user