Skip to content

Commit

Permalink
Add ShrinkText widget
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Dec 30, 2023
1 parent e14e929 commit 15c4bf7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
action::Action,
config::{Config, KeyBindings},
text,
widgets::shrink_text::ShrinkText,
};

#[derive(Default)]
Expand Down Expand Up @@ -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::<Text>(content.into());
text.extend(Text::styled(
created_at.to_string(),
Style::default().fg(Color::Gray),
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/widgets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod shrink_text;
34 changes: 34 additions & 0 deletions src/widgets/shrink_text.rs
Original file line number Diff line number Diff line change
@@ -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<T>(content: T, width: usize, height: usize) -> Self
where
T: Into<Cow<'a, str>>,
{
Self {
content: content.into(),
width,
height,
}
}
}

impl<'a> From<ShrinkText<'a>> for Text<'a> {
fn from(value: ShrinkText) -> Self {
Text::from(text::truncate_text(
&text::wrap_text(&value.content, value.width),
value.height,
))
}
}

0 comments on commit 15c4bf7

Please sign in to comment.