Refactor terminal connection into a model which can be copied between terminal views
Refactor terminal modal code to use TerminalConnection model handle so we aren't storing TerminalViews in the globals Adjust INSTANCE_BUFFER_SIZE in renderer to handle pathological terminal renders Co-authored-by: mikayla.c.maki@gmail.com
This commit is contained in:
parent
a82e56918e
commit
8d34fe7e94
9 changed files with 377 additions and 259 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -4886,6 +4886,7 @@ dependencies = [
|
||||||
"itertools",
|
"itertools",
|
||||||
"mio-extras",
|
"mio-extras",
|
||||||
"ordered-float",
|
"ordered-float",
|
||||||
|
"parking_lot 0.11.2",
|
||||||
"project",
|
"project",
|
||||||
"settings",
|
"settings",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
|
|
|
@ -16,7 +16,7 @@ use std::{collections::HashMap, ffi::c_void, iter::Peekable, mem, sync::Arc, vec
|
||||||
|
|
||||||
const SHADERS_METALLIB: &'static [u8] =
|
const SHADERS_METALLIB: &'static [u8] =
|
||||||
include_bytes!(concat!(env!("OUT_DIR"), "/shaders.metallib"));
|
include_bytes!(concat!(env!("OUT_DIR"), "/shaders.metallib"));
|
||||||
const INSTANCE_BUFFER_SIZE: usize = 1024 * 1024; // This is an arbitrary decision. There's probably a more optimal value.
|
const INSTANCE_BUFFER_SIZE: usize = 8192 * 1024; // This is an arbitrary decision. There's probably a more optimal value.
|
||||||
|
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
sprite_cache: SpriteCache,
|
sprite_cache: SpriteCache,
|
||||||
|
|
|
@ -22,7 +22,7 @@ futures = "0.3"
|
||||||
ordered-float = "2.1.1"
|
ordered-float = "2.1.1"
|
||||||
itertools = "0.10"
|
itertools = "0.10"
|
||||||
dirs = "4.0.0"
|
dirs = "4.0.0"
|
||||||
|
parking_lot = "0.11"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
gpui = { path = "../gpui", features = ["test-support"] }
|
gpui = { path = "../gpui", features = ["test-support"] }
|
||||||
|
|
186
crates/terminal/src/connection.rs
Normal file
186
crates/terminal/src/connection.rs
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
use alacritty_terminal::{
|
||||||
|
config::{Config, PtyConfig},
|
||||||
|
event::{Event as AlacTermEvent, Notify},
|
||||||
|
event_loop::{EventLoop, Msg, Notifier},
|
||||||
|
grid::Scroll,
|
||||||
|
sync::FairMutex,
|
||||||
|
term::SizeInfo,
|
||||||
|
tty::{self, setup_env},
|
||||||
|
Term,
|
||||||
|
};
|
||||||
|
use futures::{channel::mpsc::unbounded, StreamExt};
|
||||||
|
use settings::Settings;
|
||||||
|
use std::{collections::HashMap, path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
|
use gpui::{ClipboardItem, CursorStyle, Entity, ModelContext};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
color_translation::{get_color_at_index, to_alac_rgb},
|
||||||
|
ZedListener,
|
||||||
|
};
|
||||||
|
|
||||||
|
const DEFAULT_TITLE: &str = "Terminal";
|
||||||
|
|
||||||
|
///Upward flowing events, for changing the title and such
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub enum Event {
|
||||||
|
TitleChanged,
|
||||||
|
CloseTerminal,
|
||||||
|
Activate,
|
||||||
|
Wakeup,
|
||||||
|
Bell,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TerminalConnection {
|
||||||
|
pub pty_tx: Notifier,
|
||||||
|
pub term: Arc<FairMutex<Term<ZedListener>>>,
|
||||||
|
pub title: String,
|
||||||
|
pub associated_directory: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TerminalConnection {
|
||||||
|
pub fn new(
|
||||||
|
working_directory: Option<PathBuf>,
|
||||||
|
initial_size: SizeInfo,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> TerminalConnection {
|
||||||
|
let pty_config = PtyConfig {
|
||||||
|
shell: None, //Use the users default shell
|
||||||
|
working_directory: working_directory.clone(),
|
||||||
|
hold: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut env: HashMap<String, String> = HashMap::new();
|
||||||
|
//TODO: Properly set the current locale,
|
||||||
|
env.insert("LC_ALL".to_string(), "en_US.UTF-8".to_string());
|
||||||
|
|
||||||
|
let config = Config {
|
||||||
|
pty_config: pty_config.clone(),
|
||||||
|
env,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
setup_env(&config);
|
||||||
|
|
||||||
|
//Spawn a task so the Alacritty EventLoop can communicate with us in a view context
|
||||||
|
let (events_tx, mut events_rx) = unbounded();
|
||||||
|
|
||||||
|
//Set up the terminal...
|
||||||
|
let term = Term::new(&config, initial_size, ZedListener(events_tx.clone()));
|
||||||
|
let term = Arc::new(FairMutex::new(term));
|
||||||
|
|
||||||
|
//Setup the pty...
|
||||||
|
let pty = tty::new(&pty_config, &initial_size, None).expect("Could not create tty");
|
||||||
|
|
||||||
|
//And connect them together
|
||||||
|
let event_loop = EventLoop::new(
|
||||||
|
term.clone(),
|
||||||
|
ZedListener(events_tx.clone()),
|
||||||
|
pty,
|
||||||
|
pty_config.hold,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
//Kick things off
|
||||||
|
let pty_tx = event_loop.channel();
|
||||||
|
let _io_thread = event_loop.spawn();
|
||||||
|
|
||||||
|
cx.spawn_weak(|this, mut cx| async move {
|
||||||
|
//Listen for terminal events
|
||||||
|
while let Some(event) = events_rx.next().await {
|
||||||
|
match this.upgrade(&cx) {
|
||||||
|
Some(this) => {
|
||||||
|
this.update(&mut cx, |this, cx| {
|
||||||
|
this.process_terminal_event(event, cx);
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
None => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
TerminalConnection {
|
||||||
|
pty_tx: Notifier(pty_tx),
|
||||||
|
term,
|
||||||
|
title: DEFAULT_TITLE.to_string(),
|
||||||
|
associated_directory: working_directory,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///Takes events from Alacritty and translates them to behavior on this view
|
||||||
|
fn process_terminal_event(
|
||||||
|
&mut self,
|
||||||
|
event: alacritty_terminal::event::Event,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) {
|
||||||
|
match dbg!(event) {
|
||||||
|
// TODO: Handle is_self_focused in subscription on terminal view
|
||||||
|
AlacTermEvent::Wakeup => {
|
||||||
|
cx.emit(Event::Wakeup);
|
||||||
|
}
|
||||||
|
AlacTermEvent::PtyWrite(out) => self.write_to_pty(out, cx),
|
||||||
|
AlacTermEvent::MouseCursorDirty => {
|
||||||
|
//Calculate new cursor style.
|
||||||
|
//TODO
|
||||||
|
//Check on correctly handling mouse events for terminals
|
||||||
|
cx.platform().set_cursor_style(CursorStyle::Arrow); //???
|
||||||
|
}
|
||||||
|
AlacTermEvent::Title(title) => {
|
||||||
|
self.title = title;
|
||||||
|
cx.emit(Event::TitleChanged);
|
||||||
|
}
|
||||||
|
AlacTermEvent::ResetTitle => {
|
||||||
|
self.title = DEFAULT_TITLE.to_string();
|
||||||
|
cx.emit(Event::TitleChanged);
|
||||||
|
}
|
||||||
|
AlacTermEvent::ClipboardStore(_, data) => {
|
||||||
|
cx.write_to_clipboard(ClipboardItem::new(data))
|
||||||
|
}
|
||||||
|
AlacTermEvent::ClipboardLoad(_, format) => self.write_to_pty(
|
||||||
|
format(
|
||||||
|
&cx.read_from_clipboard()
|
||||||
|
.map(|ci| ci.text().to_string())
|
||||||
|
.unwrap_or("".to_string()),
|
||||||
|
),
|
||||||
|
cx,
|
||||||
|
),
|
||||||
|
AlacTermEvent::ColorRequest(index, format) => {
|
||||||
|
let color = self.term.lock().colors()[index].unwrap_or_else(|| {
|
||||||
|
let term_style = &cx.global::<Settings>().theme.terminal;
|
||||||
|
to_alac_rgb(get_color_at_index(&index, &term_style.colors))
|
||||||
|
});
|
||||||
|
self.write_to_pty(format(color), cx)
|
||||||
|
}
|
||||||
|
AlacTermEvent::CursorBlinkingChange => {
|
||||||
|
//TODO: Set a timer to blink the cursor on and off
|
||||||
|
}
|
||||||
|
AlacTermEvent::Bell => {
|
||||||
|
cx.emit(Event::Bell);
|
||||||
|
}
|
||||||
|
AlacTermEvent::Exit => cx.emit(Event::CloseTerminal),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///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>) {
|
||||||
|
self.write_bytes_to_pty(input.into_bytes(), cx);
|
||||||
|
}
|
||||||
|
|
||||||
|
///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>) {
|
||||||
|
self.term.lock().scroll_display(Scroll::Bottom);
|
||||||
|
self.pty_tx.notify(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TerminalConnection {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.pty_tx.0.send(Msg::Shutdown).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Entity for TerminalConnection {
|
||||||
|
type Event = Event;
|
||||||
|
}
|
|
@ -1,10 +0,0 @@
|
||||||
use gpui::geometry::rect::RectF;
|
|
||||||
|
|
||||||
pub fn paint_layer<F>(cx: &mut gpui::PaintContext, clip_bounds: Option<RectF>, f: F)
|
|
||||||
where
|
|
||||||
F: FnOnce(&mut gpui::PaintContext) -> (),
|
|
||||||
{
|
|
||||||
cx.scene.push_layer(clip_bounds);
|
|
||||||
f(cx);
|
|
||||||
cx.scene.pop_layer()
|
|
||||||
}
|
|
|
@ -1,12 +1,26 @@
|
||||||
use gpui::{ViewContext, ViewHandle};
|
use gpui::{ModelHandle, ViewContext, ViewHandle};
|
||||||
use workspace::Workspace;
|
use workspace::Workspace;
|
||||||
|
|
||||||
use crate::{get_working_directory, DeployModal, Event, Terminal};
|
use crate::{get_working_directory, DeployModal, Event, Terminal, 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>) {
|
||||||
if let Some(stored_terminal) = cx.default_global::<Option<ViewHandle<Terminal>>>().clone() {
|
// Pull the terminal connection out of the global if it has been stored
|
||||||
workspace.toggle_modal(cx, |_, _| stored_terminal);
|
let possible_connection = cx
|
||||||
|
.update_default_global::<Option<ModelHandle<TerminalConnection>>, _, _>(
|
||||||
|
|possible_connection, _| possible_connection.take(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some(stored_connection) = possible_connection {
|
||||||
|
println!("Found stored connection");
|
||||||
|
// Create a view from the stored connection
|
||||||
|
workspace.toggle_modal(cx, |_, cx| {
|
||||||
|
cx.add_view(|cx| Terminal::from_connection(stored_connection, true, cx))
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
|
println!("No global connection :(");
|
||||||
|
// No connection was stored, create a new terminal
|
||||||
|
if let Some(closed_terminal_handle) = workspace.toggle_modal(cx, |workspace, cx| {
|
||||||
|
println!("Creating new terminal connection");
|
||||||
let project = workspace.project().read(cx);
|
let project = workspace.project().read(cx);
|
||||||
let abs_path = project
|
let abs_path = project
|
||||||
.active_entry()
|
.active_entry()
|
||||||
|
@ -14,18 +28,21 @@ pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewCon
|
||||||
.and_then(|worktree_handle| worktree_handle.read(cx).as_local())
|
.and_then(|worktree_handle| worktree_handle.read(cx).as_local())
|
||||||
.and_then(get_working_directory);
|
.and_then(get_working_directory);
|
||||||
|
|
||||||
let displaced_modal = workspace.toggle_modal(cx, |_, cx| {
|
let this = cx.add_view(|cx| Terminal::new(abs_path, true, cx));
|
||||||
let this = cx.add_view(|cx| Terminal::new(cx, abs_path, true));
|
let connection_handle = this.read(cx).connection.clone();
|
||||||
cx.subscribe(&this, on_event).detach();
|
cx.subscribe(&connection_handle, on_event).detach();
|
||||||
this
|
this
|
||||||
});
|
}) {
|
||||||
cx.set_global(displaced_modal);
|
println!("Pulled connection from modal and stored in global");
|
||||||
|
let connection = closed_terminal_handle.read(cx).connection.clone();
|
||||||
|
cx.set_global(Some(connection));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_event(
|
pub fn on_event(
|
||||||
workspace: &mut Workspace,
|
workspace: &mut Workspace,
|
||||||
_: ViewHandle<Terminal>,
|
_: ModelHandle<TerminalConnection>,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
cx: &mut ViewContext<Workspace>,
|
cx: &mut ViewContext<Workspace>,
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -1,33 +1,29 @@
|
||||||
pub mod color_translation;
|
pub mod color_translation;
|
||||||
pub mod gpui_func_tools;
|
pub mod connection;
|
||||||
mod modal;
|
mod modal;
|
||||||
pub mod terminal_element;
|
pub mod terminal_element;
|
||||||
|
|
||||||
use alacritty_terminal::{
|
use alacritty_terminal::{
|
||||||
config::{Config, PtyConfig},
|
event::{Event as AlacTermEvent, EventListener},
|
||||||
event::{Event as AlacTermEvent, EventListener, Notify},
|
event_loop::Msg,
|
||||||
event_loop::{EventLoop, Msg, Notifier},
|
|
||||||
grid::Scroll,
|
grid::Scroll,
|
||||||
sync::FairMutex,
|
|
||||||
term::SizeInfo,
|
term::SizeInfo,
|
||||||
tty::{self, setup_env},
|
|
||||||
Term,
|
|
||||||
};
|
};
|
||||||
use color_translation::{get_color_at_index, to_alac_rgb};
|
|
||||||
|
use connection::{Event, TerminalConnection};
|
||||||
use dirs::home_dir;
|
use dirs::home_dir;
|
||||||
use futures::{
|
use editor::Input;
|
||||||
channel::mpsc::{unbounded, UnboundedSender},
|
use futures::channel::mpsc::UnboundedSender;
|
||||||
StreamExt,
|
|
||||||
};
|
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, elements::*, impl_internal_actions, platform::CursorStyle, ClipboardItem, Entity,
|
actions, elements::*, impl_internal_actions, ClipboardItem, Entity, ModelHandle,
|
||||||
MutableAppContext, View, ViewContext,
|
MutableAppContext, View, ViewContext,
|
||||||
};
|
};
|
||||||
use modal::deploy_modal;
|
use modal::deploy_modal;
|
||||||
|
|
||||||
use project::{LocalWorktree, Project, ProjectPath};
|
use project::{LocalWorktree, Project, ProjectPath};
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::{collections::HashMap, path::PathBuf, sync::Arc};
|
use std::path::PathBuf;
|
||||||
use workspace::{Item, Workspace};
|
use workspace::{Item, Workspace};
|
||||||
|
|
||||||
use crate::terminal_element::TerminalEl;
|
use crate::terminal_element::TerminalEl;
|
||||||
|
@ -42,16 +38,11 @@ const LEFT_SEQ: &str = "\x1b[D";
|
||||||
const RIGHT_SEQ: &str = "\x1b[C";
|
const RIGHT_SEQ: &str = "\x1b[C";
|
||||||
const UP_SEQ: &str = "\x1b[A";
|
const UP_SEQ: &str = "\x1b[A";
|
||||||
const DOWN_SEQ: &str = "\x1b[B";
|
const DOWN_SEQ: &str = "\x1b[B";
|
||||||
const DEFAULT_TITLE: &str = "Terminal";
|
|
||||||
const DEBUG_TERMINAL_WIDTH: f32 = 1000.; //This needs to be wide enough that the prompt can fill the whole space.
|
const DEBUG_TERMINAL_WIDTH: f32 = 1000.; //This needs to be wide enough that the prompt can fill the whole space.
|
||||||
const DEBUG_TERMINAL_HEIGHT: f32 = 200.;
|
const DEBUG_TERMINAL_HEIGHT: f32 = 200.;
|
||||||
const DEBUG_CELL_WIDTH: f32 = 5.;
|
const DEBUG_CELL_WIDTH: f32 = 5.;
|
||||||
const DEBUG_LINE_HEIGHT: f32 = 5.;
|
const DEBUG_LINE_HEIGHT: f32 = 5.;
|
||||||
|
|
||||||
///Action for carrying the input to the PTY
|
|
||||||
#[derive(Clone, Default, Debug, PartialEq, Eq)]
|
|
||||||
pub struct Input(pub String);
|
|
||||||
|
|
||||||
///Event to transmit the scroll from the element to the view
|
///Event to transmit the scroll from the element to the view
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct ScrollTerminal(pub i32);
|
pub struct ScrollTerminal(pub i32);
|
||||||
|
@ -76,12 +67,11 @@ actions!(
|
||||||
DeployModal,
|
DeployModal,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
impl_internal_actions!(terminal, [Input, ScrollTerminal]);
|
impl_internal_actions!(terminal, [ScrollTerminal]);
|
||||||
|
|
||||||
///Initialize and register all of our action handlers
|
///Initialize and register all of our action handlers
|
||||||
pub fn init(cx: &mut MutableAppContext) {
|
pub fn init(cx: &mut MutableAppContext) {
|
||||||
cx.add_action(Terminal::deploy);
|
cx.add_action(Terminal::deploy);
|
||||||
cx.add_action(Terminal::write_to_pty);
|
|
||||||
cx.add_action(Terminal::send_sigint);
|
cx.add_action(Terminal::send_sigint);
|
||||||
cx.add_action(Terminal::escape);
|
cx.add_action(Terminal::escape);
|
||||||
cx.add_action(Terminal::quit);
|
cx.add_action(Terminal::quit);
|
||||||
|
@ -95,6 +85,7 @@ pub fn init(cx: &mut MutableAppContext) {
|
||||||
cx.add_action(Terminal::copy);
|
cx.add_action(Terminal::copy);
|
||||||
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(deploy_modal);
|
cx.add_action(deploy_modal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,21 +101,12 @@ impl EventListener for ZedListener {
|
||||||
|
|
||||||
///A terminal view, maintains the PTY's file handles and communicates with the terminal
|
///A terminal view, maintains the PTY's file handles and communicates with the terminal
|
||||||
pub struct Terminal {
|
pub struct Terminal {
|
||||||
pty_tx: Notifier,
|
connection: ModelHandle<TerminalConnection>,
|
||||||
term: Arc<FairMutex<Term<ZedListener>>>,
|
|
||||||
title: String,
|
|
||||||
has_new_content: bool,
|
has_new_content: bool,
|
||||||
has_bell: bool, //Currently using iTerm bell, show bell emoji in tab until input is received
|
//Currently using iTerm bell, show bell emoji in tab until input is received
|
||||||
cur_size: SizeInfo,
|
has_bell: bool,
|
||||||
|
// Only for styling purposes. Doesn't effect behavior
|
||||||
modal: bool,
|
modal: bool,
|
||||||
associated_directory: Option<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
///Upward flowing events, for changing the title and such
|
|
||||||
pub enum Event {
|
|
||||||
TitleChanged,
|
|
||||||
CloseTerminal,
|
|
||||||
Activate,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Entity for Terminal {
|
impl Entity for Terminal {
|
||||||
|
@ -133,42 +115,7 @@ impl Entity for Terminal {
|
||||||
|
|
||||||
impl Terminal {
|
impl Terminal {
|
||||||
///Create a new Terminal view. This spawns a task, a thread, and opens the TTY devices
|
///Create a new Terminal view. This spawns a task, a thread, and opens the TTY devices
|
||||||
fn new(cx: &mut ViewContext<Self>, working_directory: Option<PathBuf>, modal: bool) -> Self {
|
fn new(working_directory: Option<PathBuf>, modal: bool, cx: &mut ViewContext<Self>) -> Self {
|
||||||
//Spawn a task so the Alacritty EventLoop can communicate with us in a view context
|
|
||||||
let (events_tx, mut events_rx) = unbounded();
|
|
||||||
cx.spawn_weak(|this, mut cx| async move {
|
|
||||||
while let Some(event) = events_rx.next().await {
|
|
||||||
match this.upgrade(&cx) {
|
|
||||||
Some(handle) => {
|
|
||||||
handle.update(&mut cx, |this, cx| {
|
|
||||||
this.process_terminal_event(event, cx);
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
None => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
let pty_config = PtyConfig {
|
|
||||||
shell: None, //Use the users default shell
|
|
||||||
working_directory: working_directory.clone(),
|
|
||||||
hold: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut env: HashMap<String, String> = HashMap::new();
|
|
||||||
//TODO: Properly set the current locale,
|
|
||||||
env.insert("LC_ALL".to_string(), "en_US.UTF-8".to_string());
|
|
||||||
|
|
||||||
let config = Config {
|
|
||||||
pty_config: pty_config.clone(),
|
|
||||||
env,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
setup_env(&config);
|
|
||||||
|
|
||||||
//The details here don't matter, the terminal will be resized on the first layout
|
//The details here don't matter, the terminal will be resized on the first layout
|
||||||
let size_info = SizeInfo::new(
|
let size_info = SizeInfo::new(
|
||||||
DEBUG_TERMINAL_WIDTH,
|
DEBUG_TERMINAL_WIDTH,
|
||||||
|
@ -180,108 +127,69 @@ impl Terminal {
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
//Set up the terminal...
|
let connection =
|
||||||
let term = Term::new(&config, size_info, ZedListener(events_tx.clone()));
|
cx.add_model(|cx| TerminalConnection::new(working_directory, size_info, cx));
|
||||||
let term = Arc::new(FairMutex::new(term));
|
|
||||||
|
|
||||||
//Setup the pty...
|
Terminal::from_connection(connection, modal, cx)
|
||||||
let pty = tty::new(&pty_config, &size_info, None).expect("Could not create tty");
|
|
||||||
|
|
||||||
//And connect them together
|
|
||||||
let event_loop = EventLoop::new(
|
|
||||||
term.clone(),
|
|
||||||
ZedListener(events_tx.clone()),
|
|
||||||
pty,
|
|
||||||
pty_config.hold,
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
//Kick things off
|
|
||||||
let pty_tx = Notifier(event_loop.channel());
|
|
||||||
let _io_thread = event_loop.spawn();
|
|
||||||
Terminal {
|
|
||||||
title: DEFAULT_TITLE.to_string(),
|
|
||||||
term,
|
|
||||||
pty_tx,
|
|
||||||
has_new_content: false,
|
|
||||||
has_bell: false,
|
|
||||||
cur_size: size_info,
|
|
||||||
modal,
|
|
||||||
associated_directory: working_directory,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///Takes events from Alacritty and translates them to behavior on this view
|
fn from_connection(
|
||||||
fn process_terminal_event(
|
connection: ModelHandle<TerminalConnection>,
|
||||||
&mut self,
|
modal: bool,
|
||||||
event: alacritty_terminal::event::Event,
|
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) {
|
) -> Terminal {
|
||||||
match event {
|
cx.observe(&connection, |_, _, cx| cx.notify()).detach();
|
||||||
AlacTermEvent::Wakeup => {
|
cx.subscribe(&connection, |this, _, event, cx| match dbg!(event) {
|
||||||
if !cx.is_self_focused() {
|
Event::Wakeup => {
|
||||||
self.has_new_content = true; //Change tab content
|
if cx.is_self_focused() {
|
||||||
cx.emit(Event::TitleChanged);
|
|
||||||
} else {
|
|
||||||
cx.notify()
|
cx.notify()
|
||||||
}
|
} else {
|
||||||
}
|
this.has_new_content = true;
|
||||||
AlacTermEvent::PtyWrite(out) => self.write_to_pty(&Input(out), cx),
|
|
||||||
AlacTermEvent::MouseCursorDirty => {
|
|
||||||
//Calculate new cursor style.
|
|
||||||
//TODO
|
|
||||||
//Check on correctly handling mouse events for terminals
|
|
||||||
cx.platform().set_cursor_style(CursorStyle::Arrow); //???
|
|
||||||
}
|
|
||||||
AlacTermEvent::Title(title) => {
|
|
||||||
self.title = title;
|
|
||||||
cx.emit(Event::TitleChanged);
|
cx.emit(Event::TitleChanged);
|
||||||
}
|
}
|
||||||
AlacTermEvent::ResetTitle => {
|
}
|
||||||
self.title = DEFAULT_TITLE.to_string();
|
Event::Bell => {
|
||||||
|
this.has_bell = true;
|
||||||
cx.emit(Event::TitleChanged);
|
cx.emit(Event::TitleChanged);
|
||||||
}
|
}
|
||||||
AlacTermEvent::ClipboardStore(_, data) => {
|
_ => cx.emit(*event),
|
||||||
cx.write_to_clipboard(ClipboardItem::new(data))
|
})
|
||||||
}
|
.detach();
|
||||||
AlacTermEvent::ClipboardLoad(_, format) => self.write_to_pty(
|
|
||||||
&Input(format(
|
Terminal {
|
||||||
&cx.read_from_clipboard()
|
connection,
|
||||||
.map(|ci| ci.text().to_string())
|
has_new_content: true,
|
||||||
.unwrap_or("".to_string()),
|
has_bell: false,
|
||||||
)),
|
modal,
|
||||||
cx,
|
|
||||||
),
|
|
||||||
AlacTermEvent::ColorRequest(index, format) => {
|
|
||||||
let color = self.term.lock().colors()[index].unwrap_or_else(|| {
|
|
||||||
let term_style = &cx.global::<Settings>().theme.terminal;
|
|
||||||
to_alac_rgb(get_color_at_index(&index, &term_style.colors))
|
|
||||||
});
|
|
||||||
self.write_to_pty(&Input(format(color)), cx)
|
|
||||||
}
|
|
||||||
AlacTermEvent::CursorBlinkingChange => {
|
|
||||||
//TODO: Set a timer to blink the cursor on and off
|
|
||||||
}
|
|
||||||
AlacTermEvent::Bell => {
|
|
||||||
self.has_bell = true;
|
|
||||||
cx.emit(Event::TitleChanged);
|
|
||||||
}
|
|
||||||
AlacTermEvent::Exit => self.quit(&Quit, cx),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///Resize the terminal and the PTY. This locks the terminal.
|
///Resize the terminal and the PTY. This locks the terminal.
|
||||||
fn set_size(&mut self, new_size: SizeInfo) {
|
fn set_size(&self, new_size: SizeInfo, cx: &mut MutableAppContext) {
|
||||||
if new_size != self.cur_size {
|
self.connection.update(cx, |connection, _| {
|
||||||
self.pty_tx.0.send(Msg::Resize(new_size)).ok();
|
connection.pty_tx.0.send(Msg::Resize(new_size)).ok();
|
||||||
self.term.lock().resize(new_size);
|
connection.term.lock().resize(new_size);
|
||||||
self.cur_size = new_size;
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///Scroll the terminal. This locks the terminal
|
///Scroll the terminal. This locks the terminal
|
||||||
fn scroll_terminal(&mut self, scroll: &ScrollTerminal, _: &mut ViewContext<Self>) {
|
fn scroll_terminal(&mut self, scroll: &ScrollTerminal, cx: &mut ViewContext<Self>) {
|
||||||
self.term.lock().scroll_display(Scroll::Delta(scroll.0));
|
self.connection
|
||||||
|
.read(cx)
|
||||||
|
.term
|
||||||
|
.lock()
|
||||||
|
.scroll_display(Scroll::Delta(scroll.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn input(&mut self, Input(text): &Input, cx: &mut ViewContext<Self>) {
|
||||||
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(text.clone(), cx);
|
||||||
|
});
|
||||||
|
|
||||||
|
if self.has_bell {
|
||||||
|
self.has_bell = false;
|
||||||
|
cx.emit(Event::TitleChanged);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///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
|
||||||
|
@ -295,16 +203,11 @@ impl Terminal {
|
||||||
.and_then(get_working_directory);
|
.and_then(get_working_directory);
|
||||||
|
|
||||||
workspace.add_item(
|
workspace.add_item(
|
||||||
Box::new(cx.add_view(|cx| Terminal::new(cx, abs_path, false))),
|
Box::new(cx.add_view(|cx| Terminal::new(abs_path, false, cx))),
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
///Send the shutdown message to Alacritty
|
|
||||||
fn shutdown_pty(&mut self) {
|
|
||||||
self.pty_tx.0.send(Msg::Shutdown).ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
///Tell Zed to close us
|
///Tell Zed to close us
|
||||||
fn quit(&mut self, _: &Quit, cx: &mut ViewContext<Self>) {
|
fn quit(&mut self, _: &Quit, cx: &mut ViewContext<Self>) {
|
||||||
cx.emit(Event::CloseTerminal);
|
cx.emit(Event::CloseTerminal);
|
||||||
|
@ -312,7 +215,7 @@ impl Terminal {
|
||||||
|
|
||||||
///Attempt to paste the clipboard into the terminal
|
///Attempt to paste the clipboard into the terminal
|
||||||
fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) {
|
fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) {
|
||||||
let term = self.term.lock();
|
let term = self.connection.read(cx).term.lock();
|
||||||
let copy_text = term.selection_to_string();
|
let copy_text = term.selection_to_string();
|
||||||
match copy_text {
|
match copy_text {
|
||||||
Some(s) => cx.write_to_clipboard(ClipboardItem::new(s)),
|
Some(s) => cx.write_to_clipboard(ClipboardItem::new(s)),
|
||||||
|
@ -323,74 +226,73 @@ 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.write_to_pty(&Input(item.text().to_owned()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(item.text().to_owned(), cx);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///Write the Input payload to the tty. This locks the terminal so we can scroll it.
|
|
||||||
fn write_to_pty(&mut self, input: &Input, cx: &mut ViewContext<Self>) {
|
|
||||||
self.write_bytes_to_pty(input.0.clone().into_bytes(), cx);
|
|
||||||
}
|
|
||||||
|
|
||||||
///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>, cx: &mut ViewContext<Self>) {
|
|
||||||
//iTerm bell behavior, bell stays until terminal is interacted with
|
|
||||||
self.has_bell = false;
|
|
||||||
cx.emit(Event::TitleChanged);
|
|
||||||
self.term.lock().scroll_display(Scroll::Bottom);
|
|
||||||
self.pty_tx.notify(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
///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.write_to_pty(&Input(UP_SEQ.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(UP_SEQ.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
///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.write_to_pty(&Input(DOWN_SEQ.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(DOWN_SEQ.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
///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.write_to_pty(&Input(TAB_CHAR.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(TAB_CHAR.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
///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.write_to_pty(&Input(ETX_CHAR.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(ETX_CHAR.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
///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.write_to_pty(&Input(ESC_CHAR.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(ESC_CHAR.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
///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.write_to_pty(&Input("\x1b[3~".to_string()), cx)
|
self.connection.update(cx, |connection, cx| {
|
||||||
self.write_to_pty(&Input(DEL_CHAR.to_string()), cx);
|
connection.write_to_pty(DEL_CHAR.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
///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.write_to_pty(&Input(CARRIAGE_RETURN_CHAR.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(CARRIAGE_RETURN_CHAR.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//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.write_to_pty(&Input(LEFT_SEQ.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty(LEFT_SEQ.to_string(), cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//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.write_to_pty(&Input(RIGHT_SEQ.to_string()), cx);
|
self.connection.update(cx, |connection, cx| {
|
||||||
}
|
connection.write_to_pty(RIGHT_SEQ.to_string(), cx);
|
||||||
}
|
});
|
||||||
|
|
||||||
impl Drop for Terminal {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.shutdown_pty();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,7 +343,10 @@ impl Item for Terminal {
|
||||||
};
|
};
|
||||||
|
|
||||||
flex.with_child(
|
flex.with_child(
|
||||||
Label::new(self.title.clone(), tab_theme.label.clone())
|
Label::new(
|
||||||
|
self.connection.read(cx).title.clone(),
|
||||||
|
tab_theme.label.clone(),
|
||||||
|
)
|
||||||
.aligned()
|
.aligned()
|
||||||
.contained()
|
.contained()
|
||||||
.with_margin_left(if self.has_bell {
|
.with_margin_left(if self.has_bell {
|
||||||
|
@ -458,7 +363,11 @@ impl Item for Terminal {
|
||||||
//From what I can tell, there's no way to tell the current working
|
//From what I can tell, there's no way to tell the current working
|
||||||
//Directory of the terminal from outside the terminal. There might be
|
//Directory of the terminal from outside the terminal. There might be
|
||||||
//solutions to this, but they are non-trivial and require more IPC
|
//solutions to this, but they are non-trivial and require more IPC
|
||||||
Some(Terminal::new(cx, self.associated_directory.clone(), false))
|
Some(Terminal::new(
|
||||||
|
self.connection.read(cx).associated_directory.clone(),
|
||||||
|
false,
|
||||||
|
cx,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn project_path(&self, _cx: &gpui::AppContext) -> Option<ProjectPath> {
|
fn project_path(&self, _cx: &gpui::AppContext) -> Option<ProjectPath> {
|
||||||
|
@ -540,22 +449,28 @@ mod tests {
|
||||||
use gpui::TestAppContext;
|
use gpui::TestAppContext;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use project::{FakeFs, Fs, RealFs, RemoveOptions, Worktree};
|
use project::{FakeFs, Fs, RealFs, RemoveOptions, Worktree};
|
||||||
use std::{path::Path, sync::atomic::AtomicUsize, time::Duration};
|
use std::{
|
||||||
|
path::Path,
|
||||||
|
sync::{atomic::AtomicUsize, Arc},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
///Basic integration test, can we get the terminal to show up, execute a command,
|
///Basic integration test, can we get the terminal to show up, execute a command,
|
||||||
//and produce noticable output?
|
//and produce noticable output?
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
async fn test_terminal(cx: &mut TestAppContext) {
|
async fn test_terminal(cx: &mut TestAppContext) {
|
||||||
let terminal = cx.add_view(Default::default(), |cx| Terminal::new(cx, None, false));
|
let terminal = cx.add_view(Default::default(), |cx| Terminal::new(None, false, cx));
|
||||||
cx.set_condition_duration(Duration::from_secs(2));
|
cx.set_condition_duration(Duration::from_secs(2));
|
||||||
terminal.update(cx, |terminal, cx| {
|
terminal.update(cx, |terminal, cx| {
|
||||||
terminal.write_to_pty(&Input(("expr 3 + 4".to_string()).to_string()), cx);
|
terminal.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty("expr 3 + 4".to_string(), cx);
|
||||||
|
});
|
||||||
terminal.carriage_return(&Return, cx);
|
terminal.carriage_return(&Return, cx);
|
||||||
});
|
});
|
||||||
|
|
||||||
terminal
|
terminal
|
||||||
.condition(cx, |terminal, _cx| {
|
.condition(cx, |terminal, cx| {
|
||||||
let term = terminal.term.clone();
|
let term = terminal.connection.read(cx).term.clone();
|
||||||
let content = grid_as_str(term.lock().renderable_content().display_iter);
|
let content = grid_as_str(term.lock().renderable_content().display_iter);
|
||||||
dbg!(&content);
|
dbg!(&content);
|
||||||
content.contains("7")
|
content.contains("7")
|
||||||
|
@ -647,24 +562,34 @@ mod tests {
|
||||||
///If this test is failing for you, check that DEBUG_TERMINAL_WIDTH is wide enough to fit your entire command prompt!
|
///If this test is failing for you, check that DEBUG_TERMINAL_WIDTH is wide enough to fit your entire command prompt!
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
async fn test_copy(cx: &mut TestAppContext) {
|
async fn test_copy(cx: &mut TestAppContext) {
|
||||||
let terminal = cx.add_view(Default::default(), |cx| Terminal::new(cx, None, false));
|
let mut result_line: i32 = 0;
|
||||||
|
let terminal = cx.add_view(Default::default(), |cx| Terminal::new(None, false, cx));
|
||||||
cx.set_condition_duration(Duration::from_secs(2));
|
cx.set_condition_duration(Duration::from_secs(2));
|
||||||
|
|
||||||
terminal.update(cx, |terminal, cx| {
|
terminal.update(cx, |terminal, cx| {
|
||||||
terminal.write_to_pty(&Input(("expr 3 + 4".to_string()).to_string()), cx);
|
terminal.connection.update(cx, |connection, cx| {
|
||||||
|
connection.write_to_pty("expr 3 + 4".to_string(), cx);
|
||||||
|
});
|
||||||
terminal.carriage_return(&Return, cx);
|
terminal.carriage_return(&Return, cx);
|
||||||
});
|
});
|
||||||
|
|
||||||
terminal
|
terminal
|
||||||
.condition(cx, |terminal, _cx| {
|
.condition(cx, |terminal, cx| {
|
||||||
let term = terminal.term.clone();
|
let term = terminal.connection.read(cx).term.clone();
|
||||||
let content = grid_as_str(term.lock().renderable_content().display_iter);
|
let content = grid_as_str(term.lock().renderable_content().display_iter);
|
||||||
content.contains("7")
|
|
||||||
|
if content.contains("7") {
|
||||||
|
let idx = content.chars().position(|c| c == '7').unwrap();
|
||||||
|
result_line = content.chars().take(idx).filter(|c| *c == '\n').count() as i32;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
terminal.update(cx, |terminal, cx| {
|
terminal.update(cx, |terminal, cx| {
|
||||||
let mut term = terminal.term.lock();
|
let mut term = terminal.connection.read(cx).term.lock();
|
||||||
term.selection = Some(Selection::new(
|
term.selection = Some(Selection::new(
|
||||||
SelectionType::Semantic,
|
SelectionType::Semantic,
|
||||||
Point::new(Line(2), Column(0)),
|
Point::new(Line(2), Column(0)),
|
||||||
|
|
|
@ -9,7 +9,7 @@ use alacritty_terminal::{
|
||||||
},
|
},
|
||||||
Term,
|
Term,
|
||||||
};
|
};
|
||||||
use editor::{Cursor, CursorShape, HighlightedRange, HighlightedRangeLine};
|
use editor::{Cursor, CursorShape, HighlightedRange, HighlightedRangeLine, Input};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
color::Color,
|
color::Color,
|
||||||
elements::*,
|
elements::*,
|
||||||
|
@ -31,10 +31,7 @@ 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::{
|
use crate::{color_translation::convert_color, ScrollTerminal, Terminal, ZedListener};
|
||||||
color_translation::convert_color, gpui_func_tools::paint_layer, Input, ScrollTerminal,
|
|
||||||
Terminal, 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
|
||||||
|
@ -126,7 +123,7 @@ impl Element for TerminalEl {
|
||||||
//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));
|
view_handle.update(cx.app, |view, cx| view.set_size(cur_size, cx));
|
||||||
|
|
||||||
//Now that we're done with the mutable portion, grab the immutable settings and view again
|
//Now that we're done with the mutable portion, grab the immutable settings and view again
|
||||||
let view = view_handle.read(cx);
|
let view = view_handle.read(cx);
|
||||||
|
@ -135,7 +132,8 @@ impl Element for TerminalEl {
|
||||||
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).term.clone();
|
|
||||||
|
let terminal_mutex = view_handle.read(cx).connection.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;
|
||||||
|
@ -220,10 +218,11 @@ impl Element for TerminalEl {
|
||||||
layout: &mut Self::LayoutState,
|
layout: &mut Self::LayoutState,
|
||||||
cx: &mut gpui::PaintContext,
|
cx: &mut gpui::PaintContext,
|
||||||
) -> Self::PaintState {
|
) -> Self::PaintState {
|
||||||
|
println!("Painted a terminal element");
|
||||||
//Setup element stuff
|
//Setup element stuff
|
||||||
let clip_bounds = Some(visible_bounds);
|
let clip_bounds = Some(visible_bounds);
|
||||||
|
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
cx.paint_layer(clip_bounds, |cx| {
|
||||||
let cur_size = layout.cur_size.clone();
|
let cur_size = layout.cur_size.clone();
|
||||||
let origin = bounds.origin() + vec2f(layout.em_width.0, 0.);
|
let origin = bounds.origin() + vec2f(layout.em_width.0, 0.);
|
||||||
|
|
||||||
|
@ -237,7 +236,7 @@ impl Element for TerminalEl {
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
|
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
cx.paint_layer(clip_bounds, |cx| {
|
||||||
//Start with a background color
|
//Start with a background color
|
||||||
cx.scene.push_quad(Quad {
|
cx.scene.push_quad(Quad {
|
||||||
bounds: RectF::new(bounds.origin(), bounds.size()),
|
bounds: RectF::new(bounds.origin(), bounds.size()),
|
||||||
|
@ -266,7 +265,7 @@ impl Element for TerminalEl {
|
||||||
});
|
});
|
||||||
|
|
||||||
//Draw Selection
|
//Draw Selection
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
cx.paint_layer(clip_bounds, |cx| {
|
||||||
let mut highlight_y = None;
|
let mut highlight_y = None;
|
||||||
let highlight_lines = layout
|
let highlight_lines = layout
|
||||||
.layout_lines
|
.layout_lines
|
||||||
|
@ -305,8 +304,7 @@ impl Element for TerminalEl {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//Draw text
|
cx.paint_layer(clip_bounds, |cx| {
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
|
||||||
for layout_line in &layout.layout_lines {
|
for layout_line in &layout.layout_lines {
|
||||||
for layout_cell in &layout_line.cells {
|
for layout_cell in &layout_line.cells {
|
||||||
let point = layout_cell.point;
|
let point = layout_cell.point;
|
||||||
|
@ -329,16 +327,16 @@ impl Element for TerminalEl {
|
||||||
|
|
||||||
//Draw cursor
|
//Draw cursor
|
||||||
if let Some(cursor) = &layout.cursor {
|
if let Some(cursor) = &layout.cursor {
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
cx.paint_layer(clip_bounds, |cx| {
|
||||||
cursor.paint(origin, cx);
|
cursor.paint(origin, cx);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
if DEBUG_GRID {
|
if DEBUG_GRID {
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
cx.paint_layer(clip_bounds, |cx| {
|
||||||
draw_debug_grid(bounds, layout, cx);
|
draw_debug_grid(bounds, layout, cx);
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
1
styles/package-lock.json
generated
1
styles/package-lock.json
generated
|
@ -5,6 +5,7 @@
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
|
"name": "styles",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue