1use alacritty_terminal::{
2 config::{Config, Program, PtyConfig},
3 event::{Event as AlacTermEvent, EventListener, Notify},
4 event_loop::{EventLoop, Msg, Notifier},
5 grid::Scroll,
6 sync::FairMutex,
7 term::{color::Rgb as AlacRgb, SizeInfo},
8 tty::{self, setup_env},
9 Term,
10};
11
12use futures::{
13 channel::mpsc::{unbounded, UnboundedSender},
14 StreamExt,
15};
16use gpui::{
17 actions, color::Color, elements::*, impl_internal_actions, platform::CursorStyle,
18 ClipboardItem, Entity, MutableAppContext, View, ViewContext,
19};
20use project::{Project, ProjectPath};
21use settings::Settings;
22use smallvec::SmallVec;
23use std::{collections::HashMap, path::PathBuf, sync::Arc};
24use workspace::{Item, Workspace};
25
26use crate::terminal_element::{get_color_at_index, TerminalEl};
27
28//ASCII Control characters on a keyboard
29const ETX_CHAR: char = 3_u8 as char; //'End of text', the control code for 'ctrl-c'
30const TAB_CHAR: char = 9_u8 as char;
31const CARRIAGE_RETURN_CHAR: char = 13_u8 as char;
32const ESC_CHAR: char = 27_u8 as char; // == \x1b
33const DEL_CHAR: char = 127_u8 as char;
34const LEFT_SEQ: &str = "\x1b[D";
35const RIGHT_SEQ: &str = "\x1b[C";
36const UP_SEQ: &str = "\x1b[A";
37const DOWN_SEQ: &str = "\x1b[B";
38const DEFAULT_TITLE: &str = "Terminal";
39
40pub mod terminal_element;
41
42///Action for carrying the input to the PTY
43#[derive(Clone, Default, Debug, PartialEq, Eq)]
44pub struct Input(pub String);
45
46///Event to transmit the scroll from the element to the view
47#[derive(Clone, Debug, PartialEq)]
48pub struct ScrollTerminal(pub i32);
49
50actions!(
51 terminal,
52 [Sigint, Escape, Del, Return, Left, Right, Up, Down, Tab, Clear, Paste, Deploy, Quit]
53);
54impl_internal_actions!(terminal, [Input, ScrollTerminal]);
55
56///Initialize and register all of our action handlers
57pub fn init(cx: &mut MutableAppContext) {
58 cx.add_action(Terminal::deploy);
59 cx.add_action(Terminal::write_to_pty);
60 cx.add_action(Terminal::send_sigint);
61 cx.add_action(Terminal::escape);
62 cx.add_action(Terminal::quit);
63 cx.add_action(Terminal::del);
64 cx.add_action(Terminal::carriage_return); //TODO figure out how to do this properly. Should we be checking the terminal mode?
65 cx.add_action(Terminal::left);
66 cx.add_action(Terminal::right);
67 cx.add_action(Terminal::up);
68 cx.add_action(Terminal::down);
69 cx.add_action(Terminal::tab);
70 cx.add_action(Terminal::paste);
71 cx.add_action(Terminal::scroll_terminal);
72}
73
74///A translation struct for Alacritty to communicate with us from their event loop
75#[derive(Clone)]
76pub struct ZedListener(UnboundedSender<AlacTermEvent>);
77
78impl EventListener for ZedListener {
79 fn send_event(&self, event: AlacTermEvent) {
80 self.0.unbounded_send(event).ok();
81 }
82}
83
84///A terminal view, maintains the PTY's file handles and communicates with the terminal
85pub struct Terminal {
86 pty_tx: Notifier,
87 term: Arc<FairMutex<Term<ZedListener>>>,
88 title: String,
89 has_new_content: bool,
90 has_bell: bool, //Currently using iTerm bell, show bell emoji in tab until input is received
91 cur_size: SizeInfo,
92}
93
94///Upward flowing events, for changing the title and such
95pub enum Event {
96 TitleChanged,
97 CloseTerminal,
98 Activate,
99}
100
101impl Entity for Terminal {
102 type Event = Event;
103}
104
105impl Terminal {
106 ///Create a new Terminal view. This spawns a task, a thread, and opens the TTY devices
107 fn new(cx: &mut ViewContext<Self>, working_directory: Option<PathBuf>) -> Self {
108 //Spawn a task so the Alacritty EventLoop can communicate with us in a view context
109 let (events_tx, mut events_rx) = unbounded();
110 cx.spawn_weak(|this, mut cx| async move {
111 while let Some(event) = events_rx.next().await {
112 match this.upgrade(&cx) {
113 Some(handle) => {
114 handle.update(&mut cx, |this, cx| {
115 this.process_terminal_event(event, cx);
116 cx.notify();
117 });
118 }
119 None => break,
120 }
121 }
122 })
123 .detach();
124
125 let pty_config = PtyConfig {
126 shell: Some(Program::Just("zsh".to_string())),
127 working_directory,
128 hold: false,
129 };
130
131 //Does this mangle the zed Env? I'm guessing it does... do child processes have a seperate ENV?
132 let mut env: HashMap<String, String> = HashMap::new();
133 //TODO: Properly set the current locale,
134 env.insert("LC_ALL".to_string(), "en_US.UTF-8".to_string());
135
136 let config = Config {
137 pty_config: pty_config.clone(),
138 env,
139 ..Default::default()
140 };
141
142 setup_env(&config);
143
144 //The details here don't matter, the terminal will be resized on the first layout
145 //Set to something small for easier debugging
146 let size_info = SizeInfo::new(200., 100.0, 5., 5., 0., 0., false);
147
148 //Set up the terminal...
149 let term = Term::new(&config, size_info, ZedListener(events_tx.clone()));
150 let term = Arc::new(FairMutex::new(term));
151
152 //Setup the pty...
153 let pty = tty::new(&pty_config, &size_info, None).expect("Could not create tty");
154
155 //And connect them together
156 let event_loop = EventLoop::new(
157 term.clone(),
158 ZedListener(events_tx.clone()),
159 pty,
160 pty_config.hold,
161 false,
162 );
163
164 //Kick things off
165 let pty_tx = Notifier(event_loop.channel());
166 let _io_thread = event_loop.spawn();
167 Terminal {
168 title: DEFAULT_TITLE.to_string(),
169 term,
170 pty_tx,
171 has_new_content: false,
172 has_bell: false,
173 cur_size: size_info,
174 }
175 }
176
177 ///Takes events from Alacritty and translates them to behavior on this view
178 fn process_terminal_event(
179 &mut self,
180 event: alacritty_terminal::event::Event,
181 cx: &mut ViewContext<Self>,
182 ) {
183 match event {
184 AlacTermEvent::Wakeup => {
185 if !cx.is_self_focused() {
186 self.has_new_content = true; //Change tab content
187 cx.emit(Event::TitleChanged);
188 } else {
189 cx.notify()
190 }
191 }
192 AlacTermEvent::PtyWrite(out) => self.write_to_pty(&Input(out), cx),
193 AlacTermEvent::MouseCursorDirty => {
194 //Calculate new cursor style.
195 //TODO
196 //Check on correctly handling mouse events for terminals
197 cx.platform().set_cursor_style(CursorStyle::Arrow); //???
198 }
199 AlacTermEvent::Title(title) => {
200 self.title = title;
201 cx.emit(Event::TitleChanged);
202 }
203 AlacTermEvent::ResetTitle => {
204 self.title = DEFAULT_TITLE.to_string();
205 cx.emit(Event::TitleChanged);
206 }
207 AlacTermEvent::ClipboardStore(_, data) => {
208 cx.write_to_clipboard(ClipboardItem::new(data))
209 }
210 AlacTermEvent::ClipboardLoad(_, format) => self.write_to_pty(
211 &Input(format(
212 &cx.read_from_clipboard()
213 .map(|ci| ci.text().to_string())
214 .unwrap_or("".to_string()),
215 )),
216 cx,
217 ),
218 AlacTermEvent::ColorRequest(index, format) => {
219 let color = self.term.lock().colors()[index].unwrap_or_else(|| {
220 let term_style = &cx.global::<Settings>().theme.terminal;
221 match index {
222 0..=255 => to_alac_rgb(get_color_at_index(&(index as u8), term_style)),
223 //These additional values are required to match the Alacritty Colors object's behavior
224 256 => to_alac_rgb(term_style.foreground),
225 257 => to_alac_rgb(term_style.background),
226 258 => to_alac_rgb(term_style.cursor),
227 259 => to_alac_rgb(term_style.dim_black),
228 260 => to_alac_rgb(term_style.dim_red),
229 261 => to_alac_rgb(term_style.dim_green),
230 262 => to_alac_rgb(term_style.dim_yellow),
231 263 => to_alac_rgb(term_style.dim_blue),
232 264 => to_alac_rgb(term_style.dim_magenta),
233 265 => to_alac_rgb(term_style.dim_cyan),
234 266 => to_alac_rgb(term_style.dim_white),
235 267 => to_alac_rgb(term_style.bright_foreground),
236 268 => to_alac_rgb(term_style.black), //Dim Background, non-standard
237 _ => AlacRgb { r: 0, g: 0, b: 0 },
238 }
239 });
240 self.write_to_pty(&Input(format(color)), cx)
241 }
242 AlacTermEvent::CursorBlinkingChange => {
243 //TODO: Set a timer to blink the cursor on and off
244 }
245 AlacTermEvent::Bell => {
246 self.has_bell = true;
247 cx.emit(Event::TitleChanged);
248 }
249 AlacTermEvent::Exit => self.quit(&Quit, cx),
250 }
251 }
252
253 ///Resize the terminal and the PTY. This locks the terminal.
254 fn set_size(&mut self, new_size: SizeInfo) {
255 if new_size != self.cur_size {
256 self.pty_tx.0.send(Msg::Resize(new_size)).ok();
257 self.term.lock().resize(new_size);
258 self.cur_size = new_size;
259 }
260 }
261
262 ///Scroll the terminal. This locks the terminal
263 fn scroll_terminal(&mut self, scroll: &ScrollTerminal, _: &mut ViewContext<Self>) {
264 self.term.lock().scroll_display(Scroll::Delta(scroll.0));
265 }
266
267 ///Create a new Terminal in the current working directory or the user's home directory
268 fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
269 let project = workspace.project().read(cx);
270 let abs_path = project
271 .active_entry()
272 .and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
273 .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
274 .map(|wt| wt.abs_path().to_path_buf());
275
276 workspace.add_item(Box::new(cx.add_view(|cx| Terminal::new(cx, abs_path))), cx);
277 }
278
279 ///Send the shutdown message to Alacritty
280 fn shutdown_pty(&mut self) {
281 self.pty_tx.0.send(Msg::Shutdown).ok();
282 }
283
284 ///Tell Zed to close us
285 fn quit(&mut self, _: &Quit, cx: &mut ViewContext<Self>) {
286 cx.emit(Event::CloseTerminal);
287 }
288
289 ///Attempt to paste the clipboard into the terminal
290 fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
291 if let Some(item) = cx.read_from_clipboard() {
292 self.write_to_pty(&Input(item.text().to_owned()), cx);
293 }
294 }
295
296 ///Write the Input payload to the tty. This locks the terminal so we can scroll it.
297 fn write_to_pty(&mut self, input: &Input, cx: &mut ViewContext<Self>) {
298 self.write_bytes_to_pty(input.0.clone().into_bytes(), cx);
299 }
300
301 ///Write the Input payload to the tty. This locks the terminal so we can scroll it.
302 fn write_bytes_to_pty(&mut self, input: Vec<u8>, cx: &mut ViewContext<Self>) {
303 //iTerm bell behavior, bell stays until terminal is interacted with
304 self.has_bell = false;
305 cx.emit(Event::TitleChanged);
306 self.term.lock().scroll_display(Scroll::Bottom);
307 self.pty_tx.notify(input);
308 }
309
310 ///Send the `up` key
311 fn up(&mut self, _: &Up, cx: &mut ViewContext<Self>) {
312 self.write_to_pty(&Input(UP_SEQ.to_string()), cx);
313 }
314
315 ///Send the `down` key
316 fn down(&mut self, _: &Down, cx: &mut ViewContext<Self>) {
317 self.write_to_pty(&Input(DOWN_SEQ.to_string()), cx);
318 }
319
320 ///Send the `tab` key
321 fn tab(&mut self, _: &Tab, cx: &mut ViewContext<Self>) {
322 self.write_to_pty(&Input(TAB_CHAR.to_string()), cx);
323 }
324
325 ///Send `SIGINT` (`ctrl-c`)
326 fn send_sigint(&mut self, _: &Sigint, cx: &mut ViewContext<Self>) {
327 self.write_to_pty(&Input(ETX_CHAR.to_string()), cx);
328 }
329
330 ///Send the `escape` key
331 fn escape(&mut self, _: &Escape, cx: &mut ViewContext<Self>) {
332 self.write_to_pty(&Input(ESC_CHAR.to_string()), cx);
333 }
334
335 ///Send the `delete` key. TODO: Difference between this and backspace?
336 fn del(&mut self, _: &Del, cx: &mut ViewContext<Self>) {
337 // self.write_to_pty(&Input("\x1b[3~".to_string()), cx)
338 self.write_to_pty(&Input(DEL_CHAR.to_string()), cx);
339 }
340
341 ///Send a carriage return. TODO: May need to check the terminal mode.
342 fn carriage_return(&mut self, _: &Return, cx: &mut ViewContext<Self>) {
343 self.write_to_pty(&Input(CARRIAGE_RETURN_CHAR.to_string()), cx);
344 }
345
346 //Send the `left` key
347 fn left(&mut self, _: &Left, cx: &mut ViewContext<Self>) {
348 self.write_to_pty(&Input(LEFT_SEQ.to_string()), cx);
349 }
350
351 //Send the `right` key
352 fn right(&mut self, _: &Right, cx: &mut ViewContext<Self>) {
353 self.write_to_pty(&Input(RIGHT_SEQ.to_string()), cx);
354 }
355}
356
357impl Drop for Terminal {
358 fn drop(&mut self) {
359 self.shutdown_pty();
360 }
361}
362
363impl View for Terminal {
364 fn ui_name() -> &'static str {
365 "Terminal"
366 }
367
368 fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox {
369 TerminalEl::new(cx.handle()).contained().boxed()
370 }
371
372 fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
373 cx.emit(Event::Activate);
374 self.has_new_content = false;
375 }
376}
377
378impl Item for Terminal {
379 fn tab_content(&self, tab_theme: &theme::Tab, cx: &gpui::AppContext) -> ElementBox {
380 let settings = cx.global::<Settings>();
381 let search_theme = &settings.theme.search; //TODO properly integrate themes
382
383 let mut flex = Flex::row();
384
385 if self.has_bell {
386 flex.add_child(
387 Svg::new("icons/zap.svg") //TODO: Swap out for a better icon, or at least resize this
388 .with_color(tab_theme.label.text.color)
389 .constrained()
390 .with_width(search_theme.tab_icon_width)
391 .aligned()
392 .boxed(),
393 );
394 };
395
396 flex.with_child(
397 Label::new(self.title.clone(), tab_theme.label.clone())
398 .aligned()
399 .contained()
400 .with_margin_left(if self.has_bell {
401 search_theme.tab_icon_spacing
402 } else {
403 0.
404 })
405 .boxed(),
406 )
407 .boxed()
408 }
409
410 fn project_path(&self, _cx: &gpui::AppContext) -> Option<ProjectPath> {
411 None
412 }
413
414 fn project_entry_ids(&self, _cx: &gpui::AppContext) -> SmallVec<[project::ProjectEntryId; 3]> {
415 SmallVec::new()
416 }
417
418 fn is_singleton(&self, _cx: &gpui::AppContext) -> bool {
419 false
420 }
421
422 fn set_nav_history(&mut self, _: workspace::ItemNavHistory, _: &mut ViewContext<Self>) {}
423
424 fn can_save(&self, _cx: &gpui::AppContext) -> bool {
425 false
426 }
427
428 fn save(
429 &mut self,
430 _project: gpui::ModelHandle<Project>,
431 _cx: &mut ViewContext<Self>,
432 ) -> gpui::Task<gpui::anyhow::Result<()>> {
433 unreachable!("save should not have been called");
434 }
435
436 fn save_as(
437 &mut self,
438 _project: gpui::ModelHandle<Project>,
439 _abs_path: std::path::PathBuf,
440 _cx: &mut ViewContext<Self>,
441 ) -> gpui::Task<gpui::anyhow::Result<()>> {
442 unreachable!("save_as should not have been called");
443 }
444
445 fn reload(
446 &mut self,
447 _project: gpui::ModelHandle<Project>,
448 _cx: &mut ViewContext<Self>,
449 ) -> gpui::Task<gpui::anyhow::Result<()>> {
450 gpui::Task::ready(Ok(()))
451 }
452
453 fn is_dirty(&self, _: &gpui::AppContext) -> bool {
454 self.has_new_content
455 }
456
457 fn should_update_tab_on_event(event: &Self::Event) -> bool {
458 matches!(event, &Event::TitleChanged)
459 }
460
461 fn should_close_item_on_event(event: &Self::Event) -> bool {
462 matches!(event, &Event::CloseTerminal)
463 }
464
465 fn should_activate_item_on_event(event: &Self::Event) -> bool {
466 matches!(event, &Event::Activate)
467 }
468}
469
470//Convenience method for less lines
471fn to_alac_rgb(color: Color) -> AlacRgb {
472 AlacRgb {
473 r: color.r,
474 g: color.g,
475 b: color.g,
476 }
477}
478
479#[cfg(test)]
480mod tests {
481 use super::*;
482 use crate::terminal_element::{build_chunks, BuiltChunks};
483 use gpui::TestAppContext;
484
485 ///Basic integration test, can we get the terminal to show up, execute a command,
486 //and produce noticable output?
487 #[gpui::test]
488 async fn test_terminal(cx: &mut TestAppContext) {
489 let terminal = cx.add_view(Default::default(), |cx| Terminal::new(cx, None));
490
491 terminal.update(cx, |terminal, cx| {
492 terminal.write_to_pty(&Input(("expr 3 + 4".to_string()).to_string()), cx);
493 terminal.carriage_return(&Return, cx);
494 });
495
496 terminal
497 .condition(cx, |terminal, _cx| {
498 let term = terminal.term.clone();
499 let BuiltChunks { chunks, .. } = build_chunks(
500 term.lock().renderable_content().display_iter,
501 &Default::default(),
502 Default::default(),
503 );
504 let content = chunks.iter().map(|e| e.0.trim()).collect::<String>();
505 content.contains("7")
506 })
507 .await;
508 }
509}