use crate::{
app::{Action, App, BusyState, MsgKind, VisualAddition, state::SystemPrompt},
logging,
theme::{Theme, ThemeName, ThemePickerView},
};
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use tokio::sync::mpsc::UnboundedSender;
pub(crate) enum KeyOutcome {
Continue,
Quit,
}
pub(crate) fn handle_key(
key: KeyEvent,
app: &mut App,
action_tx: &UnboundedSender<Action>,
) -> KeyOutcome {
if key.kind == KeyEventKind::Release {
return KeyOutcome::Continue;
}
if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
return KeyOutcome::Quit;
}
if app.state.show_help {
return handle_help_overlay_key(key, app, action_tx);
}
if app.state.show_theme_picker {
return handle_theme_picker_key(key, app);
}
if let Some(target) = app.state.confirming_delete.take() {
return handle_confirm_delete_key(key, app, action_tx, &target);
}
if let Some(outcome) = handle_command_help_key(key, app) {
return outcome;
}
if app.state.filter_mode {
return handle_filter_key(key, app);
}
if let Some(outcome) = handle_install_cancel_key(key, app) {
return outcome;
}
if let Some(outcome) = handle_log_panel_key(key, app) {
return outcome;
}
if let Some(prompt) = app.state.system_prompt {
return handle_system_prompt_key(key, app, action_tx, prompt);
}
handle_main_shortcut_key(key, app, action_tx)
}
fn handle_help_overlay_key(
key: KeyEvent,
app: &mut App,
action_tx: &UnboundedSender<Action>,
) -> KeyOutcome {
match key.code {
KeyCode::Char('q') => KeyOutcome::Quit,
KeyCode::Char('f') => {
let _ = action_tx.send(Action::FixPath);
KeyOutcome::Continue
}
_ => {
app.state.show_help = false;
app.state.path_fix_notice = None;
KeyOutcome::Continue
}
}
}
fn handle_theme_picker_key(key: KeyEvent, app: &mut App) -> KeyOutcome {
let in_family = matches!(app.state.theme_picker.view, ThemePickerView::Family(_));
match key.code {
KeyCode::Esc | KeyCode::Char('q') => {
if in_family {
app.picker_back();
} else {
app.picker_cancel();
}
}
KeyCode::Enter | KeyCode::Right => app.picker_enter(),
KeyCode::Left if in_family => app.picker_back(),
KeyCode::Down | KeyCode::Char('j') => app.picker_move(1),
KeyCode::Up | KeyCode::Char('k') => app.picker_move(-1),
KeyCode::Char(c) if in_family && c.is_ascii_digit() && c != '0' => {
if let ThemePickerView::Family(family) = app.state.theme_picker.view {
let i = (c as u8 - b'1') as usize;
if i < ThemeName::in_family(family).len() {
app.state.theme_picker.theme_cursor = i;
app.state.theme = Theme::for_name(app.picker_theme());
app.picker_apply();
}
}
}
_ => {}
}
KeyOutcome::Continue
}
fn handle_confirm_delete_key(
key: KeyEvent,
app: &mut App,
action_tx: &UnboundedSender<Action>,
target: &str,
) -> KeyOutcome {
match key.code {
KeyCode::Char('y' | 'Y') => {
if let Some(v) = app
.state
.versions
.iter()
.find(|x| x.raw_version == target)
.cloned()
{
let _ = action_tx.send(Action::Delete(v));
}
}
_ => {
app.set_status("Delete cancelled", MsgKind::Info);
}
}
KeyOutcome::Continue
}
fn handle_command_help_key(key: KeyEvent, app: &mut App) -> Option<KeyOutcome> {
if !app.state.show_command_help {
return None;
}
Some(match key.code {
KeyCode::Char('?') | KeyCode::Esc => {
app.close_command_help();
KeyOutcome::Continue
}
KeyCode::Char('U') => {
app.state.system_prompt = Some(SystemPrompt::Update);
app.close_command_help();
KeyOutcome::Continue
}
KeyCode::Char('X') => {
app.state.system_prompt = Some(SystemPrompt::UninstallKeep);
app.close_command_help();
KeyOutcome::Continue
}
KeyCode::Down | KeyCode::Char('j') => {
app.scroll_command_help(1);
KeyOutcome::Continue
}
KeyCode::Up | KeyCode::Char('k') => {
app.scroll_command_help(-1);
KeyOutcome::Continue
}
KeyCode::PageDown => {
app.scroll_command_help(8);
KeyOutcome::Continue
}
KeyCode::PageUp => {
app.scroll_command_help(-8);
KeyOutcome::Continue
}
KeyCode::Home | KeyCode::Char('g') => {
app.scroll_command_help(i64::MIN);
KeyOutcome::Continue
}
KeyCode::End | KeyCode::Char('G') => {
app.scroll_command_help(i64::MAX);
KeyOutcome::Continue
}
KeyCode::Char('q') => KeyOutcome::Quit,
_ => KeyOutcome::Continue,
})
}
fn handle_filter_key(key: KeyEvent, app: &mut App) -> KeyOutcome {
match key.code {
KeyCode::Esc => {
app.state.filter.clear();
app.state.filter_mode = false;
app.state.list_state.select(Some(0));
}
KeyCode::Enter => {
app.state.filter_mode = false;
app.clamp_selection();
}
KeyCode::Backspace => {
app.state.filter.pop();
app.state.list_state.select(Some(0));
}
KeyCode::Char(c) => {
app.state.filter.push(c);
app.state.list_state.select(Some(0));
}
_ => {}
}
KeyOutcome::Continue
}
fn handle_install_cancel_key(key: KeyEvent, app: &mut App) -> Option<KeyOutcome> {
let cancel_pressed = key.code == KeyCode::Esc || key.code == KeyCode::Char('c');
if app.state.cancel_install.is_none() || !cancel_pressed {
return None;
}
let version = app
.state
.busy
.as_ref()
.and_then(BusyState::target)
.unwrap_or("unknown")
.to_string();
if let Some(tx) = app.state.cancel_install.take() {
let _ = tx.send(true);
app.set_status("Cancelling installation...", MsgKind::Info);
logging::debug(&format!(
"install: cancel requested version={version} key={:?}",
key.code
));
}
Some(KeyOutcome::Continue)
}
fn handle_log_panel_key(key: KeyEvent, app: &mut App) -> Option<KeyOutcome> {
if !app.state.show_logs {
return None;
}
match key.code {
KeyCode::Char('L') => {
app.close_logs();
return Some(KeyOutcome::Continue);
}
KeyCode::Char('`') => {
app.state.log_focus = !app.state.log_focus;
return Some(KeyOutcome::Continue);
}
_ => {}
}
if !app.state.log_focus {
return None;
}
Some(match key.code {
KeyCode::Esc => {
app.state.log_focus = false;
KeyOutcome::Continue
}
KeyCode::Up | KeyCode::Char('k') => {
app.scroll_logs(1);
KeyOutcome::Continue
}
KeyCode::Down | KeyCode::Char('j') => {
app.scroll_logs(-1);
KeyOutcome::Continue
}
KeyCode::PageUp => {
app.scroll_logs(15);
KeyOutcome::Continue
}
KeyCode::PageDown => {
app.scroll_logs(-15);
KeyOutcome::Continue
}
KeyCode::Home | KeyCode::Char('g') => {
app.scroll_logs(i64::MAX);
KeyOutcome::Continue
}
KeyCode::End | KeyCode::Char('G') => {
app.scroll_logs(i64::MIN);
KeyOutcome::Continue
}
KeyCode::Char('f') => {
app.toggle_log_follow();
KeyOutcome::Continue
}
KeyCode::Char('w') => {
app.toggle_log_wrap();
KeyOutcome::Continue
}
KeyCode::Char('l') if key.modifiers.contains(KeyModifiers::CONTROL) => {
app.clear_log_display();
KeyOutcome::Continue
}
KeyCode::Char('-') => {
let anchor = logging::read_lines().len();
app.state.log_visual_additions.push(VisualAddition {
text: "-".repeat(50),
anchor,
});
app.refresh_logs();
KeyOutcome::Continue
}
KeyCode::Enter => {
let anchor = logging::read_lines().len();
app.state.log_visual_additions.push(VisualAddition {
text: String::new(),
anchor,
});
app.refresh_logs();
KeyOutcome::Continue
}
KeyCode::Char('q') => KeyOutcome::Quit,
_ => KeyOutcome::Continue,
})
}
fn handle_system_prompt_key(
key: KeyEvent,
app: &mut App,
action_tx: &UnboundedSender<Action>,
prompt: SystemPrompt,
) -> KeyOutcome {
match key.code {
KeyCode::Char('y' | 'Y') => {
match prompt {
SystemPrompt::Update => {
let _ = action_tx.send(Action::Update);
app.state.system_prompt = None;
}
SystemPrompt::UninstallKeep => {
app.state.system_prompt = Some(SystemPrompt::UninstallPurge);
}
SystemPrompt::UninstallPurge => {
let _ = action_tx.send(Action::Uninstall(true));
app.state.system_prompt = None;
}
}
}
KeyCode::Char('p' | 'P') if prompt == SystemPrompt::UninstallKeep => {
app.state.system_prompt = Some(SystemPrompt::UninstallPurge);
}
KeyCode::Char('n' | 'N') => {
match prompt {
SystemPrompt::UninstallPurge => {
let _ = action_tx.send(Action::UninstallBinaryOnly);
app.state.system_prompt = None;
}
_ => {
app.state.system_prompt = None;
}
}
}
KeyCode::Esc | KeyCode::Char('c') => {
app.state.system_prompt = None;
}
_ => {}
}
KeyOutcome::Continue
}
fn handle_main_shortcut_key(
key: KeyEvent,
app: &mut App,
action_tx: &UnboundedSender<Action>,
) -> KeyOutcome {
match key.code {
KeyCode::Char('q') => return KeyOutcome::Quit,
KeyCode::Tab => app.switch_tab(),
KeyCode::Down | KeyCode::Char('j') => app.next_item(),
KeyCode::Up | KeyCode::Char('k') => app.previous_item(),
KeyCode::Char('/') if !app.is_busy() => {
app.state.filter_mode = true;
}
KeyCode::Char('T') => {
app.open_theme_picker();
}
KeyCode::Char('L') => {
app.open_logs();
}
KeyCode::Char('?') => {
app.toggle_command_help();
}
KeyCode::Char('h') => {
if !app.state.is_shim_in_path {
app.state.show_help = true;
}
}
KeyCode::Char('r') if !app.is_busy() => {
let _ = action_tx.send(Action::Refresh);
}
KeyCode::Char('i') if !app.is_busy() => {
if let Some(v) = app.selected_version().cloned() {
if v.installed {
app.set_status(
format!("Go {} is already installed", v.raw_version),
MsgKind::Info,
);
} else {
let _ = action_tx.send(Action::Install(v));
}
}
}
KeyCode::Char('u') if !app.is_busy() => {
if let Some(v) = app.selected_version().cloned() {
if v.installed {
let _ = action_tx.send(Action::Use(v));
} else {
app.set_status("Install this version first — press i", MsgKind::Error);
}
}
}
KeyCode::Char('d') if !app.is_busy() => {
if let Some(v) = app.selected_version().cloned() {
if v.active {
app.set_status(
"Cannot delete the active version — switch first",
MsgKind::Error,
);
} else if v.installed {
app.state.confirming_delete = Some(v.raw_version.clone());
}
}
}
KeyCode::Char('U') => {
app.state.system_prompt = Some(SystemPrompt::Update);
}
KeyCode::Char('X') => {
app.state.system_prompt = Some(SystemPrompt::UninstallKeep);
}
_ => {}
}
KeyOutcome::Continue
}