This commit is contained in:
Robin Appelman 2024-07-25 21:45:36 +02:00
commit a07deebbd6
3 changed files with 27 additions and 72 deletions

View file

@ -30,6 +30,6 @@ pub fn footer(app: &App, page: UiPage) -> Table {
fn help(page: UiPage) -> &'static str { fn help(page: UiPage) -> &'static str {
match page { match page {
UiPage::MatchList => "«Q» Exit - «Enter» Select", UiPage::MatchList => "«Q» Exit - «Enter» Select",
UiPage::Match | UiPage::All | UiPage::Unmatched => "«Q» Exit - «Esc» Back", UiPage::Match => "«Q» Exit - «Esc» Back",
} }
} }

View file

@ -92,29 +92,13 @@ fn ui(frame: &mut Frame, app: &App, state: &mut UiState) {
frame.render_widget(footer(app, page), layout[2]); frame.render_widget(footer(app, page), layout[2]);
} }
UiState::Match { UiState::Match {
selected: index, result,
table_state, table_state,
..
} => { } => {
let log_match = &app.matches[*index]; let selected_group = &result.grouped[table_state.selected().unwrap_or_default()];
let selected_group = &log_match.grouped[table_state.selected().unwrap_or_default()];
frame.render_widget(UiHistogram::new(&selected_group.histogram), layout[0]); frame.render_widget(UiHistogram::new(&selected_group.histogram), layout[0]);
frame.render_stateful_widget(grouped_lines(app, log_match), layout[1], table_state); frame.render_stateful_widget(grouped_lines(app, result), layout[1], table_state);
frame.render_widget(footer(app, page), layout[2]);
}
UiState::All { table_state } => {
let selected_group = &app.all.grouped[table_state.selected().unwrap_or_default()];
frame.render_widget(UiHistogram::new(&selected_group.histogram), layout[0]);
frame.render_stateful_widget(grouped_lines(app, &app.all), layout[1], table_state);
frame.render_widget(footer(app, page), layout[2]);
}
UiState::Unmatched { table_state } => {
let selected_group = &app.unmatched.grouped[table_state.selected().unwrap_or_default()];
frame.render_widget(UiHistogram::new(&selected_group.histogram), layout[0]);
frame.render_stateful_widget(
grouped_lines(app, &app.unmatched),
layout[1],
table_state,
);
frame.render_widget(footer(app, page), layout[2]); frame.render_widget(footer(app, page), layout[2]);
} }
} }

View file

@ -1,26 +1,21 @@
use crate::app::App; use crate::app::{App, LogMatch};
use ratatui::widgets::TableState; use ratatui::widgets::TableState;
use table_state::TableStateExt; use table_state::TableStateExt;
#[derive(Clone, Debug)] #[derive(Clone)]
pub enum UiState { pub enum UiState<'a> {
MatchList { MatchList {
table_state: TableState, table_state: TableState,
}, },
Match { Match {
selected: usize, result: &'a LogMatch,
table_state: TableState,
},
All {
table_state: TableState,
},
Unmatched {
table_state: TableState, table_state: TableState,
previous: Box<UiState<'a>>,
}, },
Quit, Quit,
} }
impl Default for UiState { impl Default for UiState<'_> {
fn default() -> Self { fn default() -> Self {
let mut table_state = TableState::default(); let mut table_state = TableState::default();
table_state.select(Some(0)); table_state.select(Some(0));
@ -28,13 +23,11 @@ impl Default for UiState {
} }
} }
impl UiState { impl<'a> UiState<'a> {
pub fn page(&self) -> UiPage { pub fn page(&self) -> UiPage {
match self { match self {
UiState::Quit | UiState::MatchList { .. } => UiPage::MatchList, UiState::Quit | UiState::MatchList { .. } => UiPage::MatchList,
UiState::Match { .. } => UiPage::Match, UiState::Match { .. } => UiPage::Match,
UiState::All { .. } => UiPage::All,
UiState::Unmatched { .. } => UiPage::Unmatched,
} }
} }
@ -42,8 +35,6 @@ impl UiState {
match self { match self {
UiState::MatchList { table_state } => Some(table_state), UiState::MatchList { table_state } => Some(table_state),
UiState::Match { table_state, .. } => Some(table_state), UiState::Match { table_state, .. } => Some(table_state),
UiState::All { table_state } => Some(table_state),
UiState::Unmatched { table_state } => Some(table_state),
UiState::Quit => None, UiState::Quit => None,
} }
} }
@ -51,14 +42,12 @@ impl UiState {
fn table_count(&self, app: &App) -> usize { fn table_count(&self, app: &App) -> usize {
match self { match self {
UiState::MatchList { .. } => app.match_lines(), UiState::MatchList { .. } => app.match_lines(),
UiState::Match { selected, .. } => app.matches[*selected].grouped.len(), UiState::Match { result, .. } => result.grouped.len(),
UiState::All { .. } => app.all.grouped.len(),
UiState::Unmatched { .. } => app.unmatched.grouped.len(),
UiState::Quit => 0, UiState::Quit => 0,
} }
} }
pub fn process(self, event: UiEvent, app: &App) -> UiState { pub fn process(self, event: UiEvent, app: &'a App) -> UiState {
match (self, event) { match (self, event) {
(UiState::Quit, _) => UiState::Quit, (UiState::Quit, _) => UiState::Quit,
(_, UiEvent::Quit) => UiState::Quit, (_, UiEvent::Quit) => UiState::Quit,
@ -77,41 +66,25 @@ impl UiState {
} }
state state
} }
(UiState::MatchList { table_state }, UiEvent::Select) => { (mut prev @ UiState::MatchList { .. }, UiEvent::Select) => {
let selected = table_state.selected().unwrap_or(0); let selected = prev.table_state().unwrap().selected().unwrap_or(0);
let mut table_state = TableState::default(); let mut table_state = TableState::default();
table_state.select(Some(0)); table_state.select(Some(0));
if selected == 0 {
UiState::All { table_state } let result = if selected == 0 {
&app.all
} else if selected == app.match_lines() - 1 { } else if selected == app.match_lines() - 1 {
UiState::Unmatched { table_state } &app.unmatched
} else { } else {
UiState::Match { &app.matches[selected - 1]
selected: selected - 1, };
table_state, UiState::Match {
} result,
table_state,
previous: Box::new(prev),
} }
} }
( (UiState::Match { previous, .. }, UiEvent::Back) => *previous,
UiState::Match {
selected: index, ..
},
UiEvent::Back,
) => {
let mut table_state = TableState::default();
table_state.select(Some(index + 1));
UiState::MatchList { table_state }
}
(UiState::All { .. }, UiEvent::Back) => {
let mut table_state = TableState::default();
table_state.select(Some(0));
UiState::MatchList { table_state }
}
(UiState::Unmatched { .. }, UiEvent::Back) => {
let mut table_state = TableState::default();
table_state.select(Some(app.match_lines() - 1));
UiState::MatchList { table_state }
}
(state, _) => state, (state, _) => state,
} }
} }
@ -128,8 +101,6 @@ pub enum UiEvent {
pub enum UiPage { pub enum UiPage {
MatchList, MatchList,
Match, Match,
All,
Unmatched,
} }
mod table_state { mod table_state {