mirror of
https://seed.radicle.dev/rad:z39mP9rQAaGmERfUMPULfPUi473tY.git
synced 2026-10-30 16:30:56 +08:00
lib: Introduce table utils
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
+23
-19
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user