|
1 | 1 | use crate::{context::Context, path::VirtualPathBuf}; |
2 | 2 | use anyhow::Result; |
3 | 3 | use clap::{command, Parser}; |
4 | | -use crossterm::event::{Event, EventStream}; |
| 4 | +use crossterm::event::{Event, EventStream, KeyCode, KeyEventKind}; |
5 | 5 | use futures::{select, StreamExt}; |
6 | | -use lighthouse_client::protocol::{Frame, Model}; |
| 6 | +use lighthouse_client::protocol::{Frame, InputEvent, Model}; |
7 | 7 | use ratatui::{ |
8 | 8 | backend::CrosstermBackend, |
9 | 9 | crossterm::{ |
@@ -44,7 +44,18 @@ pub async fn invoke(args: &[String], ctx: &mut Context) -> Result<String> { |
44 | 44 | loop { |
45 | 45 | select! { |
46 | 46 | msg = reader.next() => match msg { |
47 | | - None | Some(Err(_)) | Some(Ok(Event::Key(_)))=> break, |
| 47 | + Some(Ok(Event::Key(e))) => match e.code { |
| 48 | + KeyCode::Char('q') => break, |
| 49 | + _ => if let Some(code) = key_code_to_js(e.code) { |
| 50 | + ctx.lh.put(&path.as_lh_vec(), Model::InputEvent(InputEvent { |
| 51 | + source: 0, |
| 52 | + key: Some(code), |
| 53 | + button: None, |
| 54 | + is_down: matches!(e.kind, KeyEventKind::Press | KeyEventKind::Repeat), |
| 55 | + })).await?; |
| 56 | + } |
| 57 | + }, |
| 58 | + None | Some(Err(_)) => break, |
48 | 59 | _ => {}, |
49 | 60 | }, |
50 | 61 | msg = stream.next() => match msg { |
@@ -100,3 +111,30 @@ fn draw_to_canvas(frame: Frame, max_width: f64, max_height: f64, title: String) |
100 | 111 | .x_bounds([0.0, max_width]) |
101 | 112 | .y_bounds([0.0, max_height]) |
102 | 113 | } |
| 114 | + |
| 115 | +fn key_code_to_js(key_code: KeyCode) -> Option<i32> { |
| 116 | + match key_code { |
| 117 | + KeyCode::Backspace => Some(8), |
| 118 | + KeyCode::Enter => Some(13), |
| 119 | + KeyCode::Left => Some(37), |
| 120 | + KeyCode::Right => Some(39), |
| 121 | + KeyCode::Up => Some(38), |
| 122 | + KeyCode::Down => Some(40), |
| 123 | + KeyCode::Home => Some(36), |
| 124 | + KeyCode::End => Some(35), |
| 125 | + KeyCode::PageUp => Some(33), |
| 126 | + KeyCode::PageDown => Some(34), |
| 127 | + KeyCode::Tab => Some(9), |
| 128 | + KeyCode::Delete => Some(46), |
| 129 | + KeyCode::Insert => Some(45), |
| 130 | + KeyCode::F(n) => Some(111 + n as i32), |
| 131 | + KeyCode::Char(c) => Some(c as i32 + '0' as i32), |
| 132 | + KeyCode::Esc => Some(27), |
| 133 | + KeyCode::CapsLock => Some(20), |
| 134 | + KeyCode::ScrollLock => Some(145), |
| 135 | + KeyCode::NumLock => Some(144), |
| 136 | + KeyCode::PrintScreen => Some(44), |
| 137 | + KeyCode::Pause => Some(19), |
| 138 | + _ => None, |
| 139 | + } |
| 140 | +} |
0 commit comments