From 1ef920b19fc7576071d03cd9d8ff53accbc8d1b9 Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Wed, 13 Oct 2021 22:11:36 +0100 Subject: [PATCH] termainl pty send --- core/src/proxy.rs | 10 +++++++ core/src/tab.rs | 2 +- core/src/terminal.rs | 63 ++++++++++++++++++++++++++++++++++++++++--- proxy/src/dispatch.rs | 9 +++++++ proxy/src/terminal.rs | 1 + 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/core/src/proxy.rs b/core/src/proxy.rs index b369afa5..942f65ee 100644 --- a/core/src/proxy.rs +++ b/core/src/proxy.rs @@ -155,6 +155,16 @@ pub fn new_terminal(&self, term_id: TermId, width: usize, height: usize) { ) } + pub fn terminal_insert(&self, term_id: TermId, s: &str) { + self.peer.lock().as_ref().unwrap().send_rpc_notification( + "terminal_insert", + &json!({ + "term_id": term_id, + "content": s, + }), + ) + } + pub fn terminal_resize(&self, term_id: TermId, width: usize, height: usize) { self.peer.lock().as_ref().unwrap().send_rpc_notification( "terminal_resize", diff --git a/core/src/tab.rs b/core/src/tab.rs index 8b1c0755..db64e550 100644 --- a/core/src/tab.rs +++ b/core/src/tab.rs @@ -231,7 +231,7 @@ fn event( .terminals .get_mut(id) .unwrap(); - terminal.content = content.to_owned(); + Arc::make_mut(terminal).content = content.to_owned(); ctx.set_handled(); } LapceUICommand::UpdateDiffFiles(files) => { diff --git a/core/src/terminal.rs b/core/src/terminal.rs index 066330dd..7eb1fcd5 100644 --- a/core/src/terminal.rs +++ b/core/src/terminal.rs @@ -11,15 +11,17 @@ use crate::{ config::LapceTheme, data::LapceTabData, + keypress::KeyPressFocus, proxy::{LapceProxy, TerminalContent}, split::LapceSplitNew, + state::Mode, }; #[derive(Clone)] pub struct TerminalSplitData { pub widget_id: WidgetId, pub split_id: WidgetId, - pub terminals: im::HashMap, + pub terminals: im::HashMap>, } impl TerminalSplitData { @@ -27,7 +29,7 @@ pub fn new(proxy: Arc) -> Self { let split_id = WidgetId::next(); let mut terminals = im::HashMap::new(); - let terminal = LapceTerminalData::new(proxy); + let terminal = Arc::new(LapceTerminalData::new(proxy)); terminals.insert(terminal.id, terminal); Self { @@ -38,6 +40,34 @@ pub fn new(proxy: Arc) -> Self { } } +pub struct LapceTerminalViewData { + terminal: Arc, + proxy: Arc, +} + +impl KeyPressFocus for LapceTerminalViewData { + fn get_mode(&self) -> Mode { + Mode::Insert + } + + fn check_condition(&self, condition: &str) -> bool { + false + } + + fn run_command( + &mut self, + ctx: &mut EventCtx, + command: &crate::command::LapceCommand, + count: Option, + env: &Env, + ) { + } + + fn insert(&mut self, ctx: &mut EventCtx, c: &str) { + self.proxy.terminal_insert(self.terminal.id, c); + } +} + #[derive(Clone)] pub struct LapceTerminalData { id: TermId, @@ -48,7 +78,7 @@ impl LapceTerminalData { pub fn new(proxy: Arc) -> Self { let id = TermId::next(); std::thread::spawn(move || { - proxy.new_terminal(id, 1, 1); + proxy.new_terminal(id, 50, 20); }); Self { id, @@ -169,6 +199,33 @@ fn event( data: &mut LapceTabData, env: &Env, ) { + let old_terminal_data = + data.terminal.terminals.get(&self.term_id).unwrap().clone(); + let mut term_data = LapceTerminalViewData { + terminal: old_terminal_data.clone(), + proxy: data.proxy.clone(), + }; + match event { + Event::MouseDown(mouse_event) => { + ctx.request_focus(); + } + Event::KeyDown(key_event) => { + let mut keypress = data.keypress.clone(); + Arc::make_mut(&mut keypress).key_down( + ctx, + key_event, + &mut term_data, + env, + ); + data.keypress = keypress.clone(); + } + _ => (), + } + if !term_data.terminal.same(&old_terminal_data) { + Arc::make_mut(&mut data.terminal) + .terminals + .insert(term_data.terminal.id, term_data.terminal.clone()); + } } fn lifecycle( diff --git a/proxy/src/dispatch.rs b/proxy/src/dispatch.rs index 5e407982..36fe2d45 100644 --- a/proxy/src/dispatch.rs +++ b/proxy/src/dispatch.rs @@ -124,6 +124,10 @@ pub enum Notification { height: usize, term_id: TermId, }, + TerminalInsert { + term_id: TermId, + content: String, + }, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -413,6 +417,11 @@ fn handle_notification(&self, rpc: Notification) { self.lsp.lock().update(buffer, &content_change, buffer.rev); } } + Notification::TerminalInsert { term_id, content } => { + if let Some(terminal) = self.terminals.lock().get(&term_id) { + terminal.insert(content); + } + } Notification::TerminalResize { term_id, width, diff --git a/proxy/src/terminal.rs b/proxy/src/terminal.rs index 4cbb3382..2b2e542e 100644 --- a/proxy/src/terminal.rs +++ b/proxy/src/terminal.rs @@ -124,6 +124,7 @@ fn run(&self, receiver: Receiver, notifier: Notifier) { Event::ClipboardLoad(_, _) => {} Event::ColorRequest(_, _) => {} Event::PtyWrite(s) => { + eprintln!("pty write {}", s); notifier.notify(s.into_bytes()); } Event::CursorBlinkingChange(_) => {}