From bd4dbc3aa6b09b345c82cb8740739ce736a6eb91 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Fri, 5 Jun 2026 14:56:56 +0200 Subject: [PATCH] ui: make long conversation lines wrap --- src/main.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 10a204b..231ce16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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 = self.scene.conversation().iter().rev().map(|entry| { + let width = area.width.into(); + let items: Vec = 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)