ive harnessed the harness
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102use crate::{ app::{Action, App}, text::clean,};use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers, MouseEventKind};use klbr_ipc::v2::V2ClientMsg;
pub fn handle(app: &mut App, event: Event) -> Vec<Action> { let mut actions = Vec::new(); match event { Event::Key(key) if key.kind != KeyEventKind::Release => { let ctrl = key.modifiers.contains(KeyModifiers::CONTROL); let draft = app.editor.lines().join("\n"); let matches = app.menu.matches(&draft); if !app.help && app.sessions.is_none() && !matches.is_empty() { let index = app.menu.selected.min(matches.len() - 1); match key.code { KeyCode::Esc => { app.menu.dismissed = draft; return actions; } KeyCode::Up => { app.menu.selected = index.saturating_sub(1); return actions; } KeyCode::Down => { app.menu.selected = (index + 1).min(matches.len() - 1); return actions; } KeyCode::Tab => { app.replace_draft(matches[index].0); return actions; } KeyCode::Enter if !key .modifiers .intersects(KeyModifiers::ALT | KeyModifiers::SHIFT) => { app.replace_draft(matches[index].0); return app.submit(); } _ => app.menu.selected = 0, } } match key.code { KeyCode::Char('c') if ctrl => actions.push(Action::Quit), KeyCode::Esc => actions = app.interrupt(), KeyCode::Up if app.sessions.is_some() => { app.selected = app.selected.saturating_sub(1) } KeyCode::Down if app.sessions.is_some() => { app.selected = (app.selected + 1) .min(app.sessions.as_ref().unwrap().len().saturating_sub(1)) } KeyCode::Enter if app.sessions.is_some() => { if let Some((id, _)) = app.sessions.as_ref().unwrap().get(app.selected) { actions.push(Action::Switch(id.clone())); } } KeyCode::PageUp => actions = app.scroll(-(app.viewport_height as isize)), KeyCode::PageDown => actions = app.scroll(app.viewport_height as isize), KeyCode::End if ctrl => app.anchor = None, _ if app.help || app.sessions.is_some() => {} KeyCode::Enter if key .modifiers .intersects(KeyModifiers::ALT | KeyModifiers::SHIFT) => { app.editor.insert_newline(); } KeyCode::Char('j') if ctrl => { app.editor.insert_newline(); } KeyCode::Char('u') if ctrl => { app.replace_draft(""); } KeyCode::Left if draft.is_empty() => { actions.push(Action::Send(V2ClientMsg::SessionList)) } KeyCode::Char('n') if ctrl => { actions.push(Action::Switch(uuid::Uuid::new_v4().to_string())) } KeyCode::Up | KeyCode::Down => {} KeyCode::Enter => actions = app.submit(), _ => { app.editor.input(key); } } } Event::Paste(text) if !app.help && app.sessions.is_none() => { app.editor.insert_str(clean(&text)); } Event::Mouse(mouse) => match mouse.kind { MouseEventKind::ScrollUp => actions = app.scroll(-3), MouseEventKind::ScrollDown => actions = app.scroll(3), _ => {} }, _ => {} } actions}