use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use crate::app::ReplApp;
const IDLE_HINT: &str = "Ask anything, or /help for commands";
const BUSY_HINT: &str = "↑ to cancel";
const BUSY_LABEL: &str = "[thinking...]";
const CANCELLING_LABEL: &str = "[cancelling...]";
const NEWLINE_GLYPH: char = '⏎';
pub fn draw_input(f: &mut ratatui::Frame, app: &ReplApp, area: Rect) {
let (line, cursor_col_offset) = compose_line(app, area.width as usize);
let p = Paragraph::new(line);
f.render_widget(p, area);
let cursor_col = area.x + cursor_col_offset as u16;
let cursor_row = area.y;
f.set_cursor_position((cursor_col, cursor_row));
}
fn compose_line(app: &ReplApp, width: usize) -> (Line<'static>, usize) {
let prompt = format!("{}> ", app.label);
let prompt_width = prompt.chars().count();
let mut spans: Vec<Span<'static>> = vec![Span::raw(prompt.clone())];
if app.input_buf.is_empty() {
let placeholder = if app.accepts_submit() {
IDLE_HINT
} else {
BUSY_HINT
};
spans.push(Span::styled(
placeholder.to_string(),
Style::default()
.add_modifier(Modifier::DIM)
.add_modifier(Modifier::ITALIC),
));
} else {
spans.push(Span::raw(
app.input_buf.replace('\n', &NEWLINE_GLYPH.to_string()),
));
}
let cursor_col = prompt_width + app.input_buf[..app.cursor_pos].chars().count();
if app.cancelling || app.busy {
let label = if app.cancelling {
CANCELLING_LABEL
} else {
BUSY_LABEL
};
let used = prompt_width + app.input_buf.chars().count();
let label_w = label.chars().count();
if width > used + label_w + 1 {
let pad = width - used - label_w;
spans.push(Span::raw(" ".repeat(pad)));
spans.push(Span::styled(
label,
Style::default().add_modifier(Modifier::DIM),
));
}
}
(Line::from(spans), cursor_col)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app::ReplApp;
const WIDE: usize = 120;
fn line_text(line: &Line<'_>) -> String {
line.spans.iter().map(|s| s.content.as_ref()).collect()
}
#[test]
fn draw_input_shows_idle_placeholder_when_empty() {
let app = ReplApp::new("demo", "u");
let (line, cursor) = compose_line(&app, WIDE);
assert!(line_text(&line).contains(IDLE_HINT));
assert_eq!(cursor, "demo> ".chars().count());
}
#[test]
fn draw_input_shows_busy_placeholder_when_empty_and_busy() {
let mut app = ReplApp::new("demo", "u");
app.busy = true;
let (line, _) = compose_line(&app, WIDE);
assert!(line_text(&line).contains(BUSY_HINT));
}
#[test]
fn draw_input_renders_typed_buffer_verbatim() {
let mut app = ReplApp::new("demo", "u");
app.set_input("hello".to_string());
let (line, cursor) = compose_line(&app, WIDE);
assert!(line_text(&line).contains("hello"));
assert_eq!(cursor, "demo> hello".chars().count());
}
#[test]
fn draw_input_shows_queued_newlines_as_a_glyph() {
let mut app = ReplApp::new("demo", "u");
app.busy = true;
app.set_input("echo one\necho two".to_string());
let (line, cursor) = compose_line(&app, WIDE);
let text = line_text(&line);
assert!(!text.contains('\n'), "the composer is one row: {text:?}");
assert!(
text.contains("echo one⏎echo two"),
"the line break must render as a glyph: {text:?}"
);
assert_eq!(
app.input_buf, "echo one\necho two",
"rendering must not mutate the buffer"
);
assert_eq!(cursor, "demo> echo one\necho two".chars().count());
}
#[test]
fn draw_input_cursor_offset_respects_multibyte_chars() {
let mut app = ReplApp::new("demo", "u");
app.set_input("héllo".to_string());
app.cursor_pos = "h".len() + "é".len(); let (_, cursor) = compose_line(&app, WIDE);
assert_eq!(cursor, 8);
}
#[test]
fn draw_input_shows_busy_label_while_typing_with_nonempty_buffer() {
let mut app = ReplApp::new("demo", "u");
app.busy = true;
app.set_input("still typing".to_string());
let (line, _) = compose_line(&app, WIDE);
let text = line_text(&line);
assert!(
text.contains("still typing"),
"typed text must survive: {text}"
);
assert!(
text.contains(BUSY_LABEL),
"busy label must render even with a non-empty buffer: {text}"
);
}
#[test]
fn draw_input_shows_cancelling_label_instead_of_thinking() {
let mut app = ReplApp::new("demo", "u");
app.busy = true;
app.cancelling = true;
let (line, _) = compose_line(&app, WIDE);
let text = line_text(&line);
assert!(text.contains(CANCELLING_LABEL), "{text}");
assert!(
!text.contains(BUSY_LABEL),
"a cancelling run is not thinking: {text}"
);
}
#[test]
fn draw_input_omits_busy_label_when_idle() {
let mut app = ReplApp::new("demo", "u");
app.set_input("hello".to_string());
app.busy = false;
let (line, _) = compose_line(&app, WIDE);
assert!(!line_text(&line).contains(BUSY_LABEL));
}
#[test]
fn draw_input_omits_busy_label_when_too_narrow() {
let mut app = ReplApp::new("demo", "u");
app.busy = true;
app.set_input("hello".to_string());
let (line, _) = compose_line(&app, 10);
assert!(!line_text(&line).contains(BUSY_LABEL));
}
}