Merge pull request #1349 from zed-industries/connection-refactor

Terminal Connection touch up
This commit is contained in:
Mikayla Maki 2022-07-13 13:28:42 -07:00 committed by GitHub
commit 958fd9ad55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 116 additions and 67 deletions

View file

@ -409,6 +409,7 @@
"bindings": { "bindings": {
"ctrl-c": "terminal::Sigint", "ctrl-c": "terminal::Sigint",
"escape": "terminal::Escape", "escape": "terminal::Escape",
"shift-escape": "terminal::DeployModal",
"ctrl-d": "terminal::Quit", "ctrl-d": "terminal::Quit",
"backspace": "terminal::Del", "backspace": "terminal::Del",
"enter": "terminal::Return", "enter": "terminal::Return",
@ -418,7 +419,8 @@
"down": "terminal::Down", "down": "terminal::Down",
"tab": "terminal::Tab", "tab": "terminal::Tab",
"cmd-v": "terminal::Paste", "cmd-v": "terminal::Paste",
"cmd-c": "terminal::Copy" "cmd-c": "terminal::Copy",
"ctrl-l": "terminal::Clear"
} }
} }
] ]

View file

@ -1,4 +1,5 @@
use alacritty_terminal::{ use alacritty_terminal::{
ansi::{ClearMode, Handler},
config::{Config, PtyConfig}, config::{Config, PtyConfig},
event::{Event as AlacTermEvent, Notify}, event::{Event as AlacTermEvent, Notify},
event_loop::{EventLoop, Msg, Notifier}, event_loop::{EventLoop, Msg, Notifier},
@ -120,10 +121,10 @@ impl TerminalConnection {
AlacTermEvent::Wakeup => { AlacTermEvent::Wakeup => {
cx.emit(Event::Wakeup); cx.emit(Event::Wakeup);
} }
AlacTermEvent::PtyWrite(out) => self.write_to_pty(out, cx), AlacTermEvent::PtyWrite(out) => self.write_to_pty(out),
AlacTermEvent::MouseCursorDirty => { AlacTermEvent::MouseCursorDirty => {
//Calculate new cursor style. //Calculate new cursor style.
//TODO //TODO: alacritty/src/input.rs:L922-L939
//Check on correctly handling mouse events for terminals //Check on correctly handling mouse events for terminals
cx.platform().set_cursor_style(CursorStyle::Arrow); //??? cx.platform().set_cursor_style(CursorStyle::Arrow); //???
} }
@ -138,20 +139,17 @@ impl TerminalConnection {
AlacTermEvent::ClipboardStore(_, data) => { AlacTermEvent::ClipboardStore(_, data) => {
cx.write_to_clipboard(ClipboardItem::new(data)) cx.write_to_clipboard(ClipboardItem::new(data))
} }
AlacTermEvent::ClipboardLoad(_, format) => self.write_to_pty( AlacTermEvent::ClipboardLoad(_, format) => self.write_to_pty(format(
format( &cx.read_from_clipboard()
&cx.read_from_clipboard() .map(|ci| ci.text().to_string())
.map(|ci| ci.text().to_string()) .unwrap_or("".to_string()),
.unwrap_or("".to_string()), )),
),
cx,
),
AlacTermEvent::ColorRequest(index, format) => { AlacTermEvent::ColorRequest(index, format) => {
let color = self.term.lock().colors()[index].unwrap_or_else(|| { let color = self.term.lock().colors()[index].unwrap_or_else(|| {
let term_style = &cx.global::<Settings>().theme.terminal; let term_style = &cx.global::<Settings>().theme.terminal;
to_alac_rgb(get_color_at_index(&index, &term_style.colors)) to_alac_rgb(get_color_at_index(&index, &term_style.colors))
}); });
self.write_to_pty(format(color), cx) self.write_to_pty(format(color))
} }
AlacTermEvent::CursorBlinkingChange => { AlacTermEvent::CursorBlinkingChange => {
//TODO: Set a timer to blink the cursor on and off //TODO: Set a timer to blink the cursor on and off
@ -164,15 +162,26 @@ impl TerminalConnection {
} }
///Write the Input payload to the tty. This locks the terminal so we can scroll it. ///Write the Input payload to the tty. This locks the terminal so we can scroll it.
pub fn write_to_pty(&mut self, input: String, cx: &mut ModelContext<Self>) { pub fn write_to_pty(&mut self, input: String) {
self.write_bytes_to_pty(input.into_bytes(), cx); self.write_bytes_to_pty(input.into_bytes());
} }
///Write the Input payload to the tty. This locks the terminal so we can scroll it. ///Write the Input payload to the tty. This locks the terminal so we can scroll it.
fn write_bytes_to_pty(&mut self, input: Vec<u8>, _: &mut ModelContext<Self>) { fn write_bytes_to_pty(&mut self, input: Vec<u8>) {
self.term.lock().scroll_display(Scroll::Bottom); self.term.lock().scroll_display(Scroll::Bottom);
self.pty_tx.notify(input); self.pty_tx.notify(input);
} }
///Resize the terminal and the PTY. This locks the terminal.
pub fn set_size(&mut self, new_size: SizeInfo) {
self.pty_tx.0.send(Msg::Resize(new_size)).ok();
self.term.lock().resize(new_size);
}
pub fn clear(&mut self) {
self.write_to_pty("\x0c".into());
self.term.lock().clear_screen(ClearMode::Saved);
}
} }
impl Drop for TerminalConnection { impl Drop for TerminalConnection {

View file

@ -3,6 +3,7 @@ use workspace::Workspace;
use crate::{get_wd_for_workspace, DeployModal, Event, Terminal, TerminalConnection}; use crate::{get_wd_for_workspace, DeployModal, Event, Terminal, TerminalConnection};
#[derive(Debug)]
struct StoredConnection(ModelHandle<TerminalConnection>); struct StoredConnection(ModelHandle<TerminalConnection>);
pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) { pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) {
@ -24,12 +25,18 @@ pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewCon
let this = cx.add_view(|cx| Terminal::new(wd, true, cx)); let this = cx.add_view(|cx| Terminal::new(wd, true, cx));
let connection_handle = this.read(cx).connection.clone(); let connection_handle = this.read(cx).connection.clone();
cx.subscribe(&connection_handle, on_event).detach(); cx.subscribe(&connection_handle, on_event).detach();
//Set the global immediately, in case the user opens the command palette
cx.set_global::<Option<StoredConnection>>(Some(StoredConnection(
connection_handle.clone(),
)));
this this
}) { }) {
let connection = closed_terminal_handle.read(cx).connection.clone(); let connection = closed_terminal_handle.read(cx).connection.clone();
cx.set_global(Some(StoredConnection(connection))); cx.set_global(Some(StoredConnection(connection)));
} }
} }
//The problem is that the terminal modal is never re-stored.
} }
pub fn on_event( pub fn on_event(

View file

@ -1,11 +1,10 @@
pub mod color_translation; mod color_translation;
pub mod connection; pub mod connection;
mod modal; mod modal;
pub mod terminal_element; pub mod terminal_element;
use alacritty_terminal::{ use alacritty_terminal::{
event::{Event as AlacTermEvent, EventListener}, event::{Event as AlacTermEvent, EventListener},
event_loop::Msg,
grid::Scroll, grid::Scroll,
term::SizeInfo, term::SizeInfo,
}; };
@ -89,6 +88,7 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_action(Terminal::paste); cx.add_action(Terminal::paste);
cx.add_action(Terminal::scroll_terminal); cx.add_action(Terminal::scroll_terminal);
cx.add_action(Terminal::input); cx.add_action(Terminal::input);
cx.add_action(Terminal::clear);
cx.add_action(deploy_modal); cx.add_action(deploy_modal);
} }
@ -168,14 +168,6 @@ impl Terminal {
} }
} }
///Resize the terminal and the PTY. This locks the terminal.
fn set_size(&self, new_size: SizeInfo, cx: &mut MutableAppContext) {
self.connection.update(cx, |connection, _| {
connection.pty_tx.0.send(Msg::Resize(new_size)).ok();
connection.term.lock().resize(new_size);
})
}
///Scroll the terminal. This locks the terminal ///Scroll the terminal. This locks the terminal
fn scroll_terminal(&mut self, scroll: &ScrollTerminal, cx: &mut ViewContext<Self>) { fn scroll_terminal(&mut self, scroll: &ScrollTerminal, cx: &mut ViewContext<Self>) {
self.connection self.connection
@ -186,8 +178,9 @@ impl Terminal {
} }
fn input(&mut self, Input(text): &Input, cx: &mut ViewContext<Self>) { fn input(&mut self, Input(text): &Input, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(text.clone(), cx); //TODO: This is probably not encoding UTF8 correctly (see alacritty/src/input.rs:L825-837)
connection.write_to_pty(text.clone());
}); });
if self.has_bell { if self.has_bell {
@ -196,6 +189,11 @@ impl Terminal {
} }
} }
fn clear(&mut self, _: &Clear, cx: &mut ViewContext<Self>) {
self.connection
.update(cx, |connection, _| connection.clear());
}
///Create a new Terminal in the current working directory or the user's home directory ///Create a new Terminal in the current working directory or the user's home directory
fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) { fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
let wd = get_wd_for_workspace(workspace, cx); let wd = get_wd_for_workspace(workspace, cx);
@ -220,72 +218,72 @@ impl Terminal {
///Attempt to paste the clipboard into the terminal ///Attempt to paste the clipboard into the terminal
fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) { fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
if let Some(item) = cx.read_from_clipboard() { if let Some(item) = cx.read_from_clipboard() {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(item.text().to_owned(), cx); connection.write_to_pty(item.text().to_owned());
}) })
} }
} }
///Send the `up` key ///Send the `up` key
fn up(&mut self, _: &Up, cx: &mut ViewContext<Self>) { fn up(&mut self, _: &Up, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(UP_SEQ.to_string(), cx); connection.write_to_pty(UP_SEQ.to_string());
}); });
} }
///Send the `down` key ///Send the `down` key
fn down(&mut self, _: &Down, cx: &mut ViewContext<Self>) { fn down(&mut self, _: &Down, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(DOWN_SEQ.to_string(), cx); connection.write_to_pty(DOWN_SEQ.to_string());
}); });
} }
///Send the `tab` key ///Send the `tab` key
fn tab(&mut self, _: &Tab, cx: &mut ViewContext<Self>) { fn tab(&mut self, _: &Tab, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(TAB_CHAR.to_string(), cx); connection.write_to_pty(TAB_CHAR.to_string());
}); });
} }
///Send `SIGINT` (`ctrl-c`) ///Send `SIGINT` (`ctrl-c`)
fn send_sigint(&mut self, _: &Sigint, cx: &mut ViewContext<Self>) { fn send_sigint(&mut self, _: &Sigint, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(ETX_CHAR.to_string(), cx); connection.write_to_pty(ETX_CHAR.to_string());
}); });
} }
///Send the `escape` key ///Send the `escape` key
fn escape(&mut self, _: &Escape, cx: &mut ViewContext<Self>) { fn escape(&mut self, _: &Escape, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(ESC_CHAR.to_string(), cx); connection.write_to_pty(ESC_CHAR.to_string());
}); });
} }
///Send the `delete` key. TODO: Difference between this and backspace? ///Send the `delete` key. TODO: Difference between this and backspace?
fn del(&mut self, _: &Del, cx: &mut ViewContext<Self>) { fn del(&mut self, _: &Del, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(DEL_CHAR.to_string(), cx); connection.write_to_pty(DEL_CHAR.to_string());
}); });
} }
///Send a carriage return. TODO: May need to check the terminal mode. ///Send a carriage return. TODO: May need to check the terminal mode.
fn carriage_return(&mut self, _: &Return, cx: &mut ViewContext<Self>) { fn carriage_return(&mut self, _: &Return, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(CARRIAGE_RETURN_CHAR.to_string(), cx); connection.write_to_pty(CARRIAGE_RETURN_CHAR.to_string());
}); });
} }
//Send the `left` key //Send the `left` key
fn left(&mut self, _: &Left, cx: &mut ViewContext<Self>) { fn left(&mut self, _: &Left, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(LEFT_SEQ.to_string(), cx); connection.write_to_pty(LEFT_SEQ.to_string());
}); });
} }
//Send the `right` key //Send the `right` key
fn right(&mut self, _: &Right, cx: &mut ViewContext<Self>) { fn right(&mut self, _: &Right, cx: &mut ViewContext<Self>) {
self.connection.update(cx, |connection, cx| { self.connection.update(cx, |connection, _| {
connection.write_to_pty(RIGHT_SEQ.to_string(), cx); connection.write_to_pty(RIGHT_SEQ.to_string());
}); });
} }
} }
@ -296,7 +294,12 @@ impl View for Terminal {
} }
fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox { fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox {
let element = TerminalEl::new(cx.handle()).contained(); let element = {
let connection_handle = self.connection.clone().downgrade();
let view_id = cx.view_id();
TerminalEl::new(view_id, connection_handle, self.modal).contained()
};
if self.modal { if self.modal {
let settings = cx.global::<Settings>(); let settings = cx.global::<Settings>();
let container_style = settings.theme.terminal.modal_container; let container_style = settings.theme.terminal.modal_container;
@ -470,8 +473,8 @@ mod tests {
let terminal = cx.add_view(Default::default(), |cx| Terminal::new(None, false, cx)); let terminal = cx.add_view(Default::default(), |cx| Terminal::new(None, false, cx));
terminal.update(cx, |terminal, cx| { terminal.update(cx, |terminal, cx| {
terminal.connection.update(cx, |connection, cx| { terminal.connection.update(cx, |connection, _| {
connection.write_to_pty("expr 3 + 4".to_string(), cx); connection.write_to_pty("expr 3 + 4".to_string());
}); });
terminal.carriage_return(&Return, cx); terminal.carriage_return(&Return, cx);
}); });
@ -495,8 +498,8 @@ mod tests {
cx.set_condition_duration(Some(Duration::from_secs(2))); cx.set_condition_duration(Some(Duration::from_secs(2)));
terminal.update(cx, |terminal, cx| { terminal.update(cx, |terminal, cx| {
terminal.connection.update(cx, |connection, cx| { terminal.connection.update(cx, |connection, _| {
connection.write_to_pty("expr 3 + 4".to_string(), cx); connection.write_to_pty("expr 3 + 4".to_string());
}); });
terminal.carriage_return(&Return, cx); terminal.carriage_return(&Return, cx);
}); });

View file

@ -21,7 +21,7 @@ use gpui::{
json::json, json::json,
text_layout::{Line, RunStyle}, text_layout::{Line, RunStyle},
Event, FontCache, KeyDownEvent, MouseRegion, PaintContext, Quad, ScrollWheelEvent, Event, FontCache, KeyDownEvent, MouseRegion, PaintContext, Quad, ScrollWheelEvent,
SizeConstraint, TextLayoutCache, WeakViewHandle, SizeConstraint, TextLayoutCache, WeakModelHandle,
}; };
use itertools::Itertools; use itertools::Itertools;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
@ -31,7 +31,9 @@ use theme::TerminalStyle;
use std::{cmp::min, ops::Range, rc::Rc, sync::Arc}; use std::{cmp::min, ops::Range, rc::Rc, sync::Arc};
use std::{fmt::Debug, ops::Sub}; use std::{fmt::Debug, ops::Sub};
use crate::{color_translation::convert_color, ScrollTerminal, Terminal, ZedListener}; use crate::{
color_translation::convert_color, connection::TerminalConnection, ScrollTerminal, ZedListener,
};
///Scrolling is unbearably sluggish by default. Alacritty supports a configurable ///Scrolling is unbearably sluggish by default. Alacritty supports a configurable
///Scroll multiplier that is set to 3 by default. This will be removed when I ///Scroll multiplier that is set to 3 by default. This will be removed when I
@ -44,8 +46,11 @@ const ALACRITTY_SCROLL_MULTIPLIER: f32 = 3.;
const DEBUG_GRID: bool = false; const DEBUG_GRID: bool = false;
///The GPUI element that paints the terminal. ///The GPUI element that paints the terminal.
///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection?
pub struct TerminalEl { pub struct TerminalEl {
view: WeakViewHandle<Terminal>, connection: WeakModelHandle<TerminalConnection>,
view_id: usize,
modal: bool,
} }
///New type pattern so I don't mix these two up ///New type pattern so I don't mix these two up
@ -95,8 +100,16 @@ pub struct LayoutState {
} }
impl TerminalEl { impl TerminalEl {
pub fn new(view: WeakViewHandle<Terminal>) -> TerminalEl { pub fn new(
TerminalEl { view } view_id: usize,
connection: WeakModelHandle<TerminalConnection>,
modal: bool,
) -> TerminalEl {
TerminalEl {
view_id,
connection,
modal,
}
} }
} }
@ -118,22 +131,19 @@ impl Element for TerminalEl {
cx.font_cache() cx.font_cache()
.em_advance(text_style.font_id, text_style.font_size), .em_advance(text_style.font_id, text_style.font_size),
); );
let view_handle = self.view.upgrade(cx).unwrap(); let connection_handle = self.connection.upgrade(cx).unwrap();
//Tell the view our new size. Requires a mutable borrow of cx and the view //Tell the view our new size. Requires a mutable borrow of cx and the view
let cur_size = make_new_size(constraint, &cell_width, &line_height); let cur_size = make_new_size(constraint, &cell_width, &line_height);
//Note that set_size locks and mutates the terminal. //Note that set_size locks and mutates the terminal.
view_handle.update(cx.app, |view, cx| view.set_size(cur_size, cx)); connection_handle.update(cx.app, |connection, _| connection.set_size(cur_size));
//Now that we're done with the mutable portion, grab the immutable settings and view again
let view = view_handle.read(cx);
let (selection_color, terminal_theme) = { let (selection_color, terminal_theme) = {
let theme = &(cx.global::<Settings>()).theme; let theme = &(cx.global::<Settings>()).theme;
(theme.editor.selection.selection, &theme.terminal) (theme.editor.selection.selection, &theme.terminal)
}; };
let terminal_mutex = view_handle.read(cx).connection.read(cx).term.clone(); let terminal_mutex = connection_handle.read(cx).term.clone();
let term = terminal_mutex.lock(); let term = terminal_mutex.lock();
let grid = term.grid(); let grid = term.grid();
let cursor_point = grid.cursor.point; let cursor_point = grid.cursor.point;
@ -146,7 +156,7 @@ impl Element for TerminalEl {
&text_style, &text_style,
terminal_theme, terminal_theme,
cx.text_layout_cache, cx.text_layout_cache,
view.modal, self.modal,
content.selection, content.selection,
); );
@ -190,7 +200,7 @@ impl Element for TerminalEl {
}); });
drop(term); drop(term);
let background_color = if view.modal { let background_color = if self.modal {
terminal_theme.colors.modal_background terminal_theme.colors.modal_background
} else { } else {
terminal_theme.colors.background terminal_theme.colors.background
@ -229,7 +239,7 @@ impl Element for TerminalEl {
attach_mouse_handlers( attach_mouse_handlers(
origin, origin,
cur_size, cur_size,
self.view.id(), self.view_id,
&layout.terminal, &layout.terminal,
visible_bounds, visible_bounds,
cx, cx,
@ -349,6 +359,25 @@ impl Element for TerminalEl {
_paint: &mut Self::PaintState, _paint: &mut Self::PaintState,
cx: &mut gpui::EventContext, cx: &mut gpui::EventContext,
) -> bool { ) -> bool {
//The problem:
//Depending on the terminal mode, we either send an escape sequence
//OR update our own data structures.
//e.g. scrolling. If we do smooth scrolling, then we need to check if
//we own scrolling and then if so, do our scrolling thing.
//Ok, so the terminal connection should have APIs for querying it semantically
//something like `should_handle_scroll()`. This means we need a handle to the connection.
//Actually, this is the only time that this app needs to talk to the outer world.
//TODO for scrolling rework: need a way of intercepting Home/End/PageUp etc.
//Sometimes going to scroll our own internal buffer, sometimes going to send ESC
//
//Same goes for key events
//Actually, we don't use the terminal at all in dispatch_event code, the view
//Handles it all. Check how the editor implements scrolling, is it view-level
//or element level?
//Question: Can we continue dispatching to the view, so it can talk to the connection
//Or should we instead add a connection into here?
match event { match event {
Event::ScrollWheel(ScrollWheelEvent { Event::ScrollWheel(ScrollWheelEvent {
delta, position, .. delta, position, ..

View file

@ -5,7 +5,6 @@
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "styles",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {