Skip to content

Commit 7760257

Browse files
committed
Send input events
1 parent a1c1830 commit 7760257

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/cmd/display.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{context::Context, path::VirtualPathBuf};
22
use anyhow::Result;
33
use clap::{command, Parser};
4-
use crossterm::event::{Event, EventStream};
4+
use crossterm::event::{Event, EventStream, KeyCode, KeyEventKind};
55
use futures::{select, StreamExt};
6-
use lighthouse_client::protocol::{Frame, Model};
6+
use lighthouse_client::protocol::{Frame, InputEvent, Model};
77
use ratatui::{
88
backend::CrosstermBackend,
99
crossterm::{
@@ -44,7 +44,18 @@ pub async fn invoke(args: &[String], ctx: &mut Context) -> Result<String> {
4444
loop {
4545
select! {
4646
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,
4859
_ => {},
4960
},
5061
msg = stream.next() => match msg {
@@ -100,3 +111,30 @@ fn draw_to_canvas(frame: Frame, max_width: f64, max_height: f64, title: String)
100111
.x_bounds([0.0, max_width])
101112
.y_bounds([0.0, max_height])
102113
}
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

Comments
 (0)