From b7d922fa89113050301d3f6354b08a7f12406326 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Wed, 27 Dec 2023 14:58:27 -0800 Subject: [PATCH] feat(ratatui): migrate from tui-rs to ratatui Ratatui is an actively maintained fork of tui-rs. See https://ratatui.rs --- Cargo.toml | 4 +-- src/app.rs | 4 +-- src/main.rs | 2 +- src/stateful_list.rs | 2 +- src/ui.rs | 61 +++++++++++++++++--------------------------- 5 files changed, 29 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f86faf1..732bb6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,5 +21,5 @@ opt-level = 3 [dependencies] germ = { version = "0.3.7", default-features = false, features = ["request", "ast"] } # Gemini url = "2.2.2" # URL -tui = "0.18.0" # Terminal User Interface -crossterm = "0.24.0" # Cross-platform Terminal +ratatui = "0.25.0" # Terminal User Interface +crossterm = "0.27.0" # Cross-platform Terminal diff --git a/src/app.rs b/src/app.rs index 6094f9a..82df7e3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -182,8 +182,8 @@ impl App { }); } - pub fn run( - terminal: &mut tui::Terminal, + pub fn run( + terminal: &mut ratatui::Terminal, mut app: Self, tick_rate: Duration, ) -> std::io::Result<()> { diff --git a/src/main.rs b/src/main.rs index 625f402..64eb7e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,7 +117,7 @@ Report bugs to https://github.com/gemrest/sydney/issues"#, )?; let mut terminal = - tui::Terminal::new(tui::backend::CrosstermBackend::new(stdout))?; + ratatui::Terminal::new(ratatui::backend::CrosstermBackend::new(stdout))?; let result = App::run(&mut terminal, app, std::time::Duration::from_millis(250)); diff --git a/src/stateful_list.rs b/src/stateful_list.rs index f7ec279..65c8a79 100644 --- a/src/stateful_list.rs +++ b/src/stateful_list.rs @@ -17,7 +17,7 @@ //! -use tui::widgets::ListState; +use ratatui::widgets::ListState; pub struct StatefulList { pub state: ListState, diff --git a/src/ui.rs b/src/ui.rs index 5267eb6..b34d844 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -16,19 +16,16 @@ // SPDX-License-Identifier: GPL-3.0-only use germ::ast::Node; -use tui::{ +use ratatui::{ layout::{Constraint, Direction, Layout, Rect}, style::{Color, Modifier, Style}, - text::{Span, Spans}, + text::{Line, Span}, widgets, widgets::{ListItem, Paragraph}, }; #[allow(clippy::too_many_lines)] -pub fn ui( - f: &mut tui::Frame<'_, B>, - app: &mut crate::App, -) { +pub fn ui(f: &mut ratatui::Frame<'_>, app: &mut crate::App) { let chunks = Layout::default() .direction(Direction::Vertical) .constraints( @@ -46,7 +43,7 @@ pub fn ui( .items .iter() .map(|(text_lines, _link, pre)| { - let mut spans = vec![]; + let mut lines = vec![]; for line in text_lines { let mut line = line.clone(); @@ -55,13 +52,13 @@ pub fn ui( if let Node::Text(text) = line { line = Node::PreformattedText { alt_text: None, - text: text.to_string(), + text: text.to_string(), } } } macro_rules! wrap_split { - ($text:ident, $spans:ident) => { + ($text:ident, $lines:ident) => { let wrappeds = $text .as_bytes() .chunks((app.wrap_at as usize) - 5) @@ -72,7 +69,7 @@ pub fn ui( .collect::>(); for (i, wrapped) in wrappeds.iter().enumerate() { - $spans.push(Spans::from(format!(" {}{}", wrapped, { + $lines.push(Line::from(format!(" {}{}", wrapped, { if i < wrappeds.len() - 1 && wrappeds.len() != 1 { "-" } else { @@ -84,10 +81,11 @@ pub fn ui( } match line { - germ::ast::Node::Text(text) => + germ::ast::Node::Text(text) => { if text != "sydney_abc_123" { - wrap_split!(text, spans); - }, + wrap_split!(text, lines); + } + } germ::ast::Node::Blockquote(text) => { let wrappeds = text .as_bytes() @@ -99,7 +97,7 @@ pub fn ui( .collect::>(); for (i, wrapped) in wrappeds.iter().enumerate() { - spans.push(Spans::from(vec![ + lines.push(Line::from(vec![ Span::styled(" > ", Style::default().fg(Color::LightBlue)), Span::styled( format!("{}{}", wrapped.clone(), { @@ -114,10 +112,7 @@ pub fn ui( ])); } } - germ::ast::Node::Link { - to, - text, - } => { + germ::ast::Node::Link { to, text } => { let mut span_list = vec![Span::styled(" => ", Style::default().fg(Color::LightBlue))]; @@ -129,13 +124,10 @@ pub fn ui( span_list .push(Span::styled(to, Style::default().fg(Color::LightBlue))); - spans.push(Spans::from(span_list)); + lines.push(Line::from(span_list)); } - germ::ast::Node::Heading { - text, - level, - } => { - spans.push(Spans::from(vec![ + germ::ast::Node::Heading { text, level } => { + lines.push(Line::from(vec![ Span::styled( match level { 1 => " # ", @@ -173,12 +165,9 @@ pub fn ui( span_list.push(Span::from(format!("{}\n", list_item))); } - spans.push(Spans::from(span_list)); + lines.push(Line::from(span_list)); } - germ::ast::Node::PreformattedText { - text, - alt_text, - } => { + germ::ast::Node::PreformattedText { text, alt_text } => { let mut span_list = vec![ Span::styled("``` ", Style::default().fg(Color::LightBlue)), Span::from(alt_text.unwrap_or_else(|| "".to_string())), @@ -188,15 +177,15 @@ pub fn ui( span_list.push(Span::from(text)); } - spans.push(Spans::from(span_list)); + lines.push(Line::from(span_list)); } germ::ast::Node::Whitespace => { - spans.push(Spans::from("".to_string())); + lines.push(Line::from("".to_string())); } }; } - ListItem::new(spans) + ListItem::new(lines) }) .collect(); @@ -241,9 +230,7 @@ pub fn ui( app.response_input_text.trim(), app.response_input )) - .wrap(widgets::Wrap { - trim: false - }), + .wrap(widgets::Wrap { trim: false }), block.inner(area), ); } @@ -258,9 +245,7 @@ pub fn ui( f.render_widget(widgets::Clear, area); f.render_widget(block.clone(), area); f.render_widget( - Paragraph::new(error.to_string()).wrap(widgets::Wrap { - trim: false - }), + Paragraph::new(error.to_string()).wrap(widgets::Wrap { trim: false }), block.inner(area), ); }