Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Introduce table utils
Erik Kundt committed 2 years ago
commit a92e684308f76596cfaeefee956a7c70efd861be
parent 8ba58140c63cf1b12d0b4c059786872b6ac12475
4 files changed +29 -41
modified 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::<B, State, Action, NotificationItem>::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();
modified 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::<B, State, Action, IssueItem>::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();

modified 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::<B, State, Action, PatchItem>::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();

modified 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<S, A> 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)
+
    }
+
}