ui: make long conversation lines wrap

This commit is contained in:
2026-06-05 14:56:56 +02:00
parent af61b3bd8a
commit bd4dbc3aa6
+25 -7
View File
@@ -115,17 +115,35 @@ impl App {
}
}
fn format_line<'a>(text: &str, prefix: &'a str, prefix_style: Style, max_width: usize) -> Text<'a> {
let avail_width = max_width - prefix.len();
let indent = " ".repeat(prefix.len());
let wrap_options = textwrap::Options::new(avail_width).initial_indent(prefix).subsequent_indent(&indent);
let wrapped: Vec<Line> = textwrap::wrap(text, wrap_options)
.iter()
.enumerate()
.map(|(idx, s)| {
if idx == 0 {
Line::from_iter([Span::from(prefix).style(prefix_style), Span::from(s[prefix.len()..].to_string())])
} else {
Line::from(s.to_string())
}
}).collect();
Text::from_iter(wrapped)
}
fn draw_conversation(&mut self, frame: &mut Frame, area: Rect) {
let items: Vec<Line> = self.scene.conversation().iter().rev().map(|entry| {
let width = area.width.into();
let items: Vec<Text> = self.scene.conversation().iter().rev().map(|entry| {
match entry {
ConversationEntry::User(text) => Line::from_iter([Span::from("Argee: ").style(ratatui::style::Color::Magenta), Span::from(text)]),
ConversationEntry::Eva(text) => Line::from_iter([Span::from("Eva: ").style(ratatui::style::Color::Cyan), Span::from(text)]),
ConversationEntry::ShipComputer(text) => Line::from_iter([Span::from("Ship Computer: ").style(ratatui::style::Color::Green), Span::from(text)]),
ConversationEntry::StageDirection(text) => Line::from_iter([text]).style(ratatui::style::Color::Yellow),
ConversationEntry::SystemMessage(text) => Line::from_iter([text]).style(ratatui::style::Color::DarkGray)
ConversationEntry::User(text) => Self::format_line(text, "Argee: ", Style::new().fg(style::Color::Magenta), width),
ConversationEntry::Eva(text) => Self::format_line(text, "Eva: ", Style::new().fg(style::Color::Cyan), width),
ConversationEntry::ShipComputer(text) => Self::format_line(text, "Ship Computer: ", Style::new().fg(style::Color::Green), width),
ConversationEntry::StageDirection(text) => Self::format_line(text, "", Style::new(), width).style(ratatui::style::Color::Yellow),
ConversationEntry::SystemMessage(text) => Self::format_line(text, "", Style::new(), width).style(ratatui::style::Color::DarkGray)
}
}).collect();
// FIXME: We need to somehow make long list items wrap. https://github.com/ratatui/ratatui/issues/128#issuecomment-1613918499
// TODO: Would be nice to be able to scroll a longer conversation with the scroll wheel, or with page up/down
frame.render_stateful_widget(
List::new(items)