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