From 73c91eb9ad7418d548cebba7ae8a57f17d22fe81 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Mon, 26 Feb 2024 10:08:34 +0100 Subject: [PATCH] inbox: Add stats to selection footer --- bin/commands/inbox/flux/select/ui.rs | 49 ++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/bin/commands/inbox/flux/select/ui.rs b/bin/commands/inbox/flux/select/ui.rs index 7998b5f1..ad9905d3 100644 --- a/bin/commands/inbox/flux/select/ui.rs +++ b/bin/commands/inbox/flux/select/ui.rs @@ -1,13 +1,16 @@ -use std::cmp; +use std::collections::HashMap; use std::vec; -use radicle::identity::Project; use tokio::sync::mpsc::UnboundedSender; use termion::event::Key; use ratatui::backend::Backend; -use ratatui::layout::{Constraint, Direction, Layout, Rect}; +use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect}; +use ratatui::style::Stylize; +use ratatui::text::Line; + +use radicle::identity::Project; use radicle_tui as tui; @@ -145,13 +148,27 @@ impl Render<()> for ListPage { struct NotificationsProps { notifications: Vec, project: Project, + stats: HashMap, } impl From<&InboxState> for NotificationsProps { fn from(state: &InboxState) -> Self { + let mut seen = 0; + let mut unseen = 0; + + for notification in &state.notifications { + if notification.seen { + seen += 1; + } else { + unseen += 1; + } + } + let stats = HashMap::from([("Seen".to_string(), seen), ("Unseen".to_string(), unseen)]); + Self { notifications: state.notifications.clone(), project: state.project.clone(), + stats, } } } @@ -260,16 +277,22 @@ impl Render<()> for Notifications { Constraint::Length(18), ]; - let progress = { - let step = self - .table - .selected() - .map(|selected| selected.saturating_add(1).to_string()) - .unwrap_or("-".to_string()); - let length = self.props.notifications.len().to_string(); + let stats = Line::from( + [ + span::positive(self.props.stats.get("Seen").unwrap_or(&0).to_string()).dim(), + span::default(" Seen".to_string()).dim(), + span::default(" | ".to_string()).dim(), + span::default(self.props.stats.get("Unseen").unwrap_or(&0).to_string()) + .magenta() + .dim(), + span::default(" Unseen".to_string()).dim(), + ] + .to_vec(), + ) + .alignment(Alignment::Right); - span::badge(format!("{}/{}", cmp::min(&step, &length), length)) - }; + let (step, len) = self.table.progress(self.props.notifications.len()); + let progress = span::progress(step, len, false); self.header.render::( frame, @@ -304,7 +327,7 @@ impl Render<()> for Notifications { cells: [ span::badge("/".to_string()).into(), String::from("").into(), - String::from("").into(), + stats.into(), progress.clone().into(), ], widths: [