use crate::cli::tui::state::TuiState;
use crate::cli::tui::traits::{HandleInput, IsActive};
use crate::cli::tui::ui::LayoutSection;
use ratatui::crossterm::event;
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
use std::io;
pub fn handle_input(state: &mut TuiState) -> io::Result<bool> {
if event::poll(std::time::Duration::from_millis(50))? {
if let Event::Key(key) = event::read()? {
if key.kind != KeyEventKind::Press {
return Ok(false);
}
match state.focused {
LayoutSection::Filter | LayoutSection::Logging => {
if handle_widget_input(state, key) {
return Ok(false);
}
}
LayoutSection::Main => {
if key.code == KeyCode::Char('p') {
state.processing = !state.processing;
return Ok(false);
}
if handle_section_input(state, key) {
return Ok(false);
}
if handle_main_menu_input(state, key) {
return Ok(true);
}
}
}
}
}
Ok(false)
}
fn handle_section_input(state: &mut TuiState, key: KeyEvent) -> bool {
for (i, section) in state.sections.iter_mut().enumerate() {
if i != state.selected {
continue;
}
let handled = section.handle_input(key);
if handled {
state.interacting = Some(i);
return true;
}
state.interacting = None;
}
false
}
fn handle_widget_input(state: &mut TuiState, key: KeyEvent) -> bool {
if key.kind == KeyEventKind::Press {
if state.filter_widget.inputting {
state.filter_widget.input(key);
return true;
} else if state.logs_widget.focused {
state.logs_widget.input(key);
return true;
}
}
false
}
fn handle_main_menu_input(state: &mut TuiState, key: KeyEvent) -> bool {
match key.code {
KeyCode::Char('q') => return true,
KeyCode::Up => {
if state.selected > 0 {
state.selected -= 1;
}
}
KeyCode::Down => {
if state.selected < state.sections.len() - 1 {
state.selected += 1;
}
}
KeyCode::Char(' ') => {
let active_state = state.sections[state.selected].is_active();
state.sections[state.selected].set_active(!active_state);
}
KeyCode::Char(c) if c.is_numeric() => {
for (i, _) in state.sections.iter().enumerate() {
if c == char::from_digit((i + 1) as u32, 10).unwrap() {
state.selected = i;
break;
}
}
}
_ => {}
}
state.filter_widget.input(key);
state.logs_widget.input(key);
false
}