From 3a435bdd3699f1cc2472696d9e3bf6cf3805a07e Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Sat, 24 Feb 2024 00:41:57 +0100 Subject: [PATCH] lib: Switch from text to span --- src/flux/ui/span.rs | 32 ++++++++++++++++---------------- src/flux/ui/widget.rs | 12 +++++++++++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/flux/ui/span.rs b/src/flux/ui/span.rs index 02ee7d9b..4c46d01e 100644 --- a/src/flux/ui/span.rs +++ b/src/flux/ui/span.rs @@ -1,57 +1,57 @@ use ratatui::style::{Style, Stylize}; -use ratatui::text::Text; +use ratatui::text::Span; use crate::flux::ui::theme::style; -pub fn blank() -> Text<'static> { - Text::styled("", Style::default()) +pub fn blank() -> Span<'static> { + Span::styled("", Style::default()) } -pub fn default(content: String) -> Text<'static> { - Text::styled(content, Style::default()) +pub fn default(content: String) -> Span<'static> { + Span::styled(content, Style::default()) } -pub fn primary(content: String) -> Text<'static> { +pub fn primary(content: String) -> Span<'static> { default(content).style(style::cyan()) } -pub fn secondary(content: String) -> Text<'static> { +pub fn secondary(content: String) -> Span<'static> { default(content).style(style::magenta()) } -pub fn ternary(content: String) -> Text<'static> { +pub fn ternary(content: String) -> Span<'static> { default(content).style(style::blue()) } -pub fn positive(content: String) -> Text<'static> { +pub fn positive(content: String) -> Span<'static> { default(content).style(style::green()) } -pub fn negative(content: String) -> Text<'static> { +pub fn negative(content: String) -> Span<'static> { default(content).style(style::red()) } -pub fn badge(content: String) -> Text<'static> { +pub fn badge(content: String) -> Span<'static> { let content = &format!(" {content} "); default(content.to_string()).magenta().reversed() } -pub fn alias(content: String) -> Text<'static> { +pub fn alias(content: String) -> Span<'static> { secondary(content) } -pub fn labels(content: String) -> Text<'static> { +pub fn labels(content: String) -> Span<'static> { ternary(content) } -pub fn timestamp(content: String) -> Text<'static> { +pub fn timestamp(content: String) -> Span<'static> { default(content).style(style::gray().dim()) } -pub fn notification_id(content: String) -> Text<'static> { +pub fn notification_id(content: String) -> Span<'static> { default(content).style(style::gray().dim()) } -pub fn notification_type(content: String) -> Text<'static> { +pub fn notification_type(content: String) -> Span<'static> { default(content).style(style::gray().dim()) } diff --git a/src/flux/ui/widget.rs b/src/flux/ui/widget.rs index 84ecb53d..167484ff 100644 --- a/src/flux/ui/widget.rs +++ b/src/flux/ui/widget.rs @@ -1,5 +1,6 @@ pub mod container; +use std::cmp; use std::fmt::Debug; use tokio::sync::mpsc::UnboundedSender; @@ -159,6 +160,15 @@ impl Table { }); self.state.select(selected); } + + pub fn progress(&self, len: usize) -> (usize, usize) { + let step = self + .selected() + .map(|selected| selected.saturating_add(1)) + .unwrap_or_default(); + + (cmp::min(step, len), len) + } } impl Widget for Table { @@ -234,7 +244,7 @@ where frame.render_widget(block, area); let center = layout::centered_rect(area, 50, 10); - let hint = span::default("Nothing to show".to_string()) + let hint = Text::from(span::default("Nothing to show".to_string())) .centered() .light_magenta() .dim();