From 15c4bf7aa069cdebae76c56b9b3abae4ca233739 Mon Sep 17 00:00:00 2001 From: Akiomi Kamakura Date: Sat, 30 Dec 2023 09:52:04 +0900 Subject: [PATCH] Add ShrinkText widget --- src/components/home.rs | 10 ++++------ src/main.rs | 1 + src/widgets.rs | 1 + src/widgets/shrink_text.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/widgets.rs create mode 100644 src/widgets/shrink_text.rs diff --git a/src/components/home.rs b/src/components/home.rs index 3c8960f1..994f3226 100644 --- a/src/components/home.rs +++ b/src/components/home.rs @@ -20,6 +20,7 @@ use crate::{ action::Action, config::{Config, KeyBindings}, text, + widgets::shrink_text::ShrinkText, }; #[derive(Default)] @@ -236,18 +237,15 @@ impl Component for Home { let zaps = self.calc_zap_amount(&ev); let content_width = area.width.saturating_sub(2); // NOTE: paddingを引いて調整している let content_height = area.height.saturating_sub(7); // NOTE: paddingと他の行を引いて調整している - let content = text::truncate_text( - &text::wrap_text(&ev.content, content_width as usize), - content_height as usize, - ); - log::info!("height: {}, content: {}", content_height, content); + let content = + ShrinkText::new(&ev.content, content_width as usize, content_height as usize); let mut text = Text::default(); text.extend(Text::styled( self.format_pubkey(ev.pubkey.to_string()), Style::default().bold(), )); - text.extend(Text::raw(content)); // TODO: wrap line + text.extend::(content.into()); text.extend(Text::styled( created_at.to_string(), Style::default().fg(Color::Gray), diff --git a/src/main.rs b/src/main.rs index dcf77541..c95f1da7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ pub mod mode; pub mod text; pub mod tui; pub mod utils; +pub mod widgets; use clap::Parser; use cli::Cli; diff --git a/src/widgets.rs b/src/widgets.rs new file mode 100644 index 00000000..2c869412 --- /dev/null +++ b/src/widgets.rs @@ -0,0 +1 @@ +pub mod shrink_text; diff --git a/src/widgets/shrink_text.rs b/src/widgets/shrink_text.rs new file mode 100644 index 00000000..0abaa101 --- /dev/null +++ b/src/widgets/shrink_text.rs @@ -0,0 +1,34 @@ +use std::borrow::Cow; + +use ratatui::text::Text; + +use crate::text; + +#[derive(Clone, Debug, Default)] +pub struct ShrinkText<'a> { + pub content: Cow<'a, str>, + pub width: usize, + pub height: usize, +} + +impl<'a> ShrinkText<'a> { + pub fn new(content: T, width: usize, height: usize) -> Self + where + T: Into>, + { + Self { + content: content.into(), + width, + height, + } + } +} + +impl<'a> From> for Text<'a> { + fn from(value: ShrinkText) -> Self { + Text::from(text::truncate_text( + &text::wrap_text(&value.content, value.width), + value.height, + )) + } +}