Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ colored = "2.1.0"
crossterm = { version = "0.27.0", features = ["event-stream"] }
dotenvy = "0.15"
futures = "0.3.30"
lighthouse-client = "3.4.0"
lighthouse-client = "5.0.1"
multipeek = "0.1.2"
once_cell = "1.20.3"
ratatui = "0.27.0"
ref-cast = "1.0"
rustyline = "14.0"
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "time", "fs"] }
url = "2.5.0"
uuid = { version = "1.15.1", features = ["v4"] }
5 changes: 5 additions & 0 deletions src/client_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use once_cell::sync::Lazy;
use uuid::Uuid;

/// This client's unique id.
pub const CLIENT_ID: Lazy<Uuid> = Lazy::new(|| Uuid::new_v4());
67 changes: 54 additions & 13 deletions src/cmd/display.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{context::Context, path::VirtualPathBuf};
use crate::{client_id::CLIENT_ID, context::Context, path::VirtualPathBuf};
use anyhow::Result;
use clap::{command, Parser};
use crossterm::event::{Event, EventStream, KeyCode, KeyEventKind};
use futures::{select, StreamExt};
use lighthouse_client::protocol::{Frame, InputEvent, Model, LIGHTHOUSE_COLS, LIGHTHOUSE_ROWS};
use lighthouse_client::protocol::{EventSource, Frame, InputEvent, KeyEvent, KeyModifiers, LegacyInputEvent, Model, LIGHTHOUSE_COLS, LIGHTHOUSE_ROWS};
use ratatui::{
backend::CrosstermBackend,
crossterm::{
Expand All @@ -25,6 +25,9 @@ const QUIT_KEY: char = 'q';
#[derive(Parser)]
#[command(bin_name = "display")]
struct Args {
#[arg(short, long, action, help = "Generate legacy input events")]
legacy: bool,

#[arg(
default_value = ".",
help = "The resource to display as an image stream"
Expand All @@ -48,14 +51,29 @@ pub async fn invoke(args: &[String], ctx: &mut Context) -> Result<String> {
msg = reader.next() => match msg {
Some(Ok(Event::Key(e))) => match e.code {
KeyCode::Char(QUIT_KEY) => break,
_ => if let Some(code) = key_code_to_js(e.code) {
ctx.lh.put(&path.as_lh_vec(), Model::InputEvent(InputEvent {
source: 0,
key: Some(code),
button: None,
is_down: matches!(e.kind, KeyEventKind::Press | KeyEventKind::Repeat),
})).await?;
}
_ => {
let down = matches!(e.kind, KeyEventKind::Press | KeyEventKind::Repeat);
if args.legacy {
if let Some(code) = key_code_to_js_key_code(e.code) {
ctx.lh.put(&path.as_lh_vec(), Model::InputEvent(LegacyInputEvent {
source: 0,
key: Some(code),
button: None,
is_down: down,
})).await?;
}
} else {
if let Some(code) = key_code_to_js_code(e.code) {
ctx.lh.put_input(InputEvent::Key(KeyEvent {
source: EventSource::String(format!("limo:{}", *CLIENT_ID)),
code,
down,
repeat: matches!(e.kind, KeyEventKind::Repeat),
modifiers: KeyModifiers::default(),
})).await?;
}
}
},
},
None | Some(Err(_)) => break,
_ => {},
Expand All @@ -78,8 +96,6 @@ pub async fn invoke(args: &[String], ctx: &mut Context) -> Result<String> {
stdout().execute(LeaveAlternateScreen)?;
disable_raw_mode()?;

ctx.lh.stop(&path.as_lh_vec()).await?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this stop is no longer needed, since stopping now happens implicitly when the stream is dropped (with the same request id as the initial request).


Ok(String::new())
}

Expand Down Expand Up @@ -151,7 +167,7 @@ impl Shape for Display {
}
}

fn key_code_to_js(key_code: KeyCode) -> Option<i32> {
fn key_code_to_js_key_code(key_code: KeyCode) -> Option<i32> {
match key_code {
KeyCode::Backspace => Some(8),
KeyCode::Enter => Some(13),
Expand All @@ -177,3 +193,28 @@ fn key_code_to_js(key_code: KeyCode) -> Option<i32> {
_ => None,
}
}

fn key_code_to_js_code(key_code: KeyCode) -> Option<String> {
match key_code {
KeyCode::Backspace => Some("Backspace".into()),
KeyCode::Enter => Some("Enter".into()),
KeyCode::Left => Some("ArrowLeft".into()),
KeyCode::Right => Some("ArrowRight".into()),
KeyCode::Up => Some("ArrowUp".into()),
KeyCode::Down => Some("ArrowDown".into()),
KeyCode::Home => Some("Home".into()),
KeyCode::End => Some("End".into()),
KeyCode::PageUp => Some("PageUp".into()),
KeyCode::PageDown => Some("PageDown".into()),
KeyCode::Tab => Some("Tab".into()),
KeyCode::Delete => Some("Delete".into()),
KeyCode::Insert => Some("Insert".into()),
KeyCode::F(n) => Some(format!("F{n}")),
KeyCode::Char(c) if c.is_ascii_digit() => Some(format!("Digit{c}")),
KeyCode::Char(c) => Some(format!("Key{c}")),
KeyCode::Esc => Some("Escape".into()),
KeyCode::CapsLock => Some("CapsLock".into()),
KeyCode::NumLock => Some("NumLock".into()),
_ => None,
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cmd;
mod client_id;
mod context;
mod line;
mod path;
Expand Down