diff --git a/bin/commands/inbox/select/ui.rs b/bin/commands/inbox/select/ui.rs index e123c4a6..3d02fdb4 100644 --- a/bin/commands/inbox/select/ui.rs +++ b/bin/commands/inbox/select/ui.rs @@ -15,10 +15,10 @@ use radicle_tui as tui; use tui::ui::items::{NotificationItem, NotificationItemFilter, NotificationState}; use tui::ui::span; -use tui::ui::widget; use tui::ui::widget::container::{Footer, FooterProps, Header, HeaderProps}; use tui::ui::widget::input::{TextField, TextFieldProps}; use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::{self, TableUtils}; use tui::ui::widget::{ Column, EventCallback, Shortcuts, ShortcutsProps, Table, TableProps, UpdateCallback, View, Widget, @@ -390,11 +390,7 @@ impl<'a, B: Backend> Notifications<'a, B> { let progress = selected .map(|selected| { - Table::::progress( - selected, - props.notifications.len(), - props.page_size, - ) + TableUtils::progress(selected, props.notifications.len(), props.page_size) }) .unwrap_or_default(); let progress = span::default(format!("{}%", progress)).dim(); diff --git a/bin/commands/issue/select/ui.rs b/bin/commands/issue/select/ui.rs index 230014ab..036130b7 100644 --- a/bin/commands/issue/select/ui.rs +++ b/bin/commands/issue/select/ui.rs @@ -17,10 +17,10 @@ use radicle_tui as tui; use tui::ui::items::{IssueItem, IssueItemFilter}; use tui::ui::span; -use tui::ui::widget; use tui::ui::widget::container::{Footer, FooterProps, Header, HeaderProps}; use tui::ui::widget::input::{TextField, TextFieldProps}; use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::{self, TableUtils}; use tui::ui::widget::{ Column, EventCallback, Shortcuts, ShortcutsProps, Table, TableProps, UpdateCallback, View, Widget, @@ -402,13 +402,7 @@ impl<'a, B: Backend> Issues<'a, B> { ); let progress = selected - .map(|selected| { - Table::::progress( - selected, - props.issues.len(), - props.page_size, - ) - }) + .map(|selected| TableUtils::progress(selected, props.issues.len(), props.page_size)) .unwrap_or_default(); let progress = span::default(format!("{}%", progress)).dim(); diff --git a/bin/commands/patch/select/ui.rs b/bin/commands/patch/select/ui.rs index 6500fc5d..ce3b0bd7 100644 --- a/bin/commands/patch/select/ui.rs +++ b/bin/commands/patch/select/ui.rs @@ -18,10 +18,10 @@ use radicle_tui as tui; use tui::ui::items::{PatchItem, PatchItemFilter}; use tui::ui::span; -use tui::ui::widget; use tui::ui::widget::container::{Footer, FooterProps, Header, HeaderProps}; use tui::ui::widget::input::{TextField, TextFieldProps}; use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::{self, TableUtils}; use tui::ui::widget::{ Column, EventCallback, Shortcuts, ShortcutsProps, Table, TableProps, UpdateCallback, View, Widget, @@ -431,13 +431,7 @@ impl<'a, B: Backend> Patches<'a, B> { ); let progress = selected - .map(|selected| { - Table::::progress( - selected, - props.patches.len(), - props.page_size, - ) - }) + .map(|selected| TableUtils::progress(selected, props.patches.len(), props.page_size)) .unwrap_or_default(); let progress = span::default(format!("{}%", progress)).dim(); diff --git a/src/ui/widget.rs b/src/ui/widget.rs index e81248be..f433845f 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -365,25 +365,6 @@ where fn end(&mut self, len: usize) { self.state.select(Some(len.saturating_sub(1))); } - - pub fn progress(selected: usize, len: usize, page_size: usize) -> usize { - let step = selected; - let page_size = page_size as f64; - let len = len as f64; - - let lines = page_size + step.saturating_sub(page_size as usize) as f64; - let progress = (lines / len * 100.0).ceil(); - - if progress > 97.0 { - Self::map_range((0.0, progress), (0.0, 100.0), progress) as usize - } else { - progress as usize - } - } - - fn map_range(from: (f64, f64), to: (f64, f64), value: f64) -> f64 { - to.0 + (value - from.0) * (to.1 - to.0) / (from.1 - from.0) - } } impl<'a: 'static, B, S, A, R> View for Table<'a, B, S, A, R> @@ -550,3 +531,26 @@ where } } } + +pub struct TableUtils {} + +impl TableUtils { + pub fn progress(selected: usize, len: usize, page_size: usize) -> usize { + let step = selected; + let page_size = page_size as f64; + let len = len as f64; + + let lines = page_size + step.saturating_sub(page_size as usize) as f64; + let progress = (lines / len * 100.0).ceil(); + + if progress > 97.0 { + Self::map_range((0.0, progress), (0.0, 100.0), progress) as usize + } else { + progress as usize + } + } + + fn map_range(from: (f64, f64), to: (f64, f64), value: f64) -> f64 { + to.0 + (value - from.0) * (to.1 - to.0) / (from.1 - from.0) + } +}