mirror of
https://seed.radicle.dev/rad:z39mP9rQAaGmERfUMPULfPUi473tY.git
synced 2026-11-29 06:20:28 +08:00
lib: Introduce table utils
This commit is contained in:
+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