Skip to content

Commit

Permalink
feat: sort selected mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
kawaki-san committed Dec 22, 2022
1 parent 66b8923 commit 2cb7912
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
18 changes: 17 additions & 1 deletion crates/mirro-rs/src/tui/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ pub enum Action {
ViewSortAlphabetically,
ViewSortMirrorCount,
ToggleSelect,
SelectionSortCompletionPct,
SelectionSortDelay,
SelectionSortDuration,
SelectionSortScore,
}

impl Action {
pub fn iterator() -> Iter<'static, Action> {
static ACTIONS: [Action; 12] = [
static ACTIONS: [Action; 16] = [
Action::Quit,
Action::ClosePopUp,
Action::ShowInput,
Expand All @@ -33,6 +37,10 @@ impl Action {
Action::ViewSortMirrorCount,
Action::ViewSortAlphabetically,
Action::ToggleSelect,
Action::SelectionSortCompletionPct,
Action::SelectionSortDelay,
Action::SelectionSortDuration,
Action::SelectionSortScore,
];
ACTIONS.iter()
}
Expand All @@ -51,6 +59,10 @@ impl Action {
Action::ViewSortAlphabetically => &[Key::Char('1')],
Action::ViewSortMirrorCount => &[Key::Char('2')],
Action::ToggleSelect => &[Key::Char(' ')],
Action::SelectionSortCompletionPct => &[Key::Char('5')],
Action::SelectionSortDelay => &[Key::Char('6')],
Action::SelectionSortDuration => &[Key::Char('7')],
Action::SelectionSortScore => &[Key::Char('8')],
}
}
}
Expand All @@ -70,6 +82,10 @@ impl Display for Action {
Action::ViewSortAlphabetically => "sort [country] A-Z",
Action::ViewSortMirrorCount => "sort [country] mirrors",
Action::ToggleSelect => "[de]select mirror",
Action::SelectionSortCompletionPct => "sort [selection] completion",
Action::SelectionSortDelay => "sort [selection] delay",
Action::SelectionSortDuration => "sort [selection] duration",
Action::SelectionSortScore => "sort [selection] score",
};
write!(f, "{str}")
}
Expand Down
35 changes: 34 additions & 1 deletion crates/mirro-rs/src/tui/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,35 @@ impl App {
self.focused_country();
AppReturn::Continue
}
Action::SelectionSortCompletionPct => {
self.selected_mirrors
.sort_by(|a, b| b.completion_pct.total_cmp(&a.completion_pct));
AppReturn::Continue
}
Action::SelectionSortDelay => {
self.selected_mirrors.sort_by(|a, b| {
let a = a.delay.unwrap_or_default();
let b = b.delay.unwrap_or_default();
a.partial_cmp(&b).unwrap()
});
AppReturn::Continue
}
Action::SelectionSortDuration => {
self.selected_mirrors.sort_by(|a, b| {
let a = a.duration_avg.unwrap_or_default();
let b = b.duration_avg.unwrap_or_default();
a.partial_cmp(&b).unwrap()
});
AppReturn::Continue
}
Action::SelectionSortScore => {
self.selected_mirrors.sort_by(|a, b| {
let a = a.duration_stddev.unwrap_or_default();
let b = b.duration_stddev.unwrap_or_default();
a.partial_cmp(&b).unwrap()
});
AppReturn::Continue
}
}
}
} else {
Expand Down Expand Up @@ -210,9 +239,13 @@ impl App {
Action::FilterHttps,
Action::FilterRsync,
Action::FilterSyncing,
Action::ToggleSelect,
Action::ViewSortAlphabetically,
Action::ViewSortMirrorCount,
Action::ToggleSelect,
Action::SelectionSortCompletionPct,
Action::SelectionSortDelay,
Action::SelectionSortDuration,
Action::SelectionSortScore,
]
.into();
self.show_popup = false;
Expand Down
31 changes: 10 additions & 21 deletions crates/mirro-rs/src/tui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,17 @@ fn draw_selection<'a>(app: &App) -> Table<'a> {
.map(|h| Cell::from(*h).style(Style::default()));
let headers = Row::new(header_cells);

let err = -999.0;

let items = app.selected_mirrors.iter().map(|f| {
let delay = match f.delay {
Some(d) => {
let duration = Duration::from_secs(d as u64);
let minutes = (duration.as_secs() / 60) % 60;
let hours = (duration.as_secs() / 60) / 60;
Some((hours, minutes))
}
None => None,
};
let delay = f.delay.map(|f| {
let duration = Duration::from_secs(f as u64);
let minutes = (duration.as_secs() / 60) % 60;
let hours = (duration.as_secs() / 60) / 60;
(hours, minutes)
});

let dur = match f.duration_avg {
Some(d) => format_float(d),
None => err,
};
let dur = f.duration_avg.map(format_float);

let std_dev = match f.duration_stddev {
Some(d) => format_float(d),
None => err,
};
let std_dev = f.duration_stddev.map(format_float);

let completion = f.completion_pct;

Expand Down Expand Up @@ -311,8 +300,8 @@ fn draw_selection<'a>(app: &App) -> Table<'a> {
}
None => Style::default(),
}),
Cell::from(dur.to_string()),
Cell::from(std_dev.to_string()),
Cell::from(dur.map(|f| f.to_string()).unwrap_or("-".to_string())),
Cell::from(std_dev.map(|f| f.to_string()).unwrap_or("-".to_string())),
])
});

Expand Down

0 comments on commit 2cb7912

Please sign in to comment.