use std::{ops::RangeInclusive, path::PathBuf, time::Duration}; use context_menu::{ContextMenu, ContextMenuItem}; use gpui::{ actions, elements::{AnchorCorner, ChildView, ParentElement, Stack}, geometry::vector::Vector2F, impl_actions, impl_internal_actions, keymap::Keystroke, AnyViewHandle, AppContext, Element, ElementBox, Entity, ModelHandle, MutableAppContext, Task, View, ViewContext, ViewHandle, }; use serde::Deserialize; use settings::{Settings, TerminalBlink}; use smol::Timer; use terminal::{ alacritty_terminal::{ index::Point, term::{search::RegexSearch, TermMode}, }, Terminal, }; use util::ResultExt; use workspace::pane; use crate::{persistence::TERMINAL_DB, terminal_element::TerminalElement, Event}; const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500); ///Event to transmit the scroll from the element to the view #[derive(Clone, Debug, PartialEq)] pub struct ScrollTerminal(pub i32); #[derive(Clone, PartialEq)] pub struct DeployContextMenu { pub position: Vector2F, } #[derive(Clone, Default, Deserialize, PartialEq)] pub struct SendText(String); #[derive(Clone, Default, Deserialize, PartialEq)] pub struct SendKeystroke(String); actions!( terminal, [Clear, Copy, Paste, ShowCharacterPalette, SearchTest] ); impl_actions!(terminal, [SendText, SendKeystroke]); impl_internal_actions!(project_panel, [DeployContextMenu]); pub fn init(cx: &mut MutableAppContext) { //Useful terminal views cx.add_action(TerminalView::send_text); cx.add_action(TerminalView::send_keystroke); cx.add_action(TerminalView::deploy_context_menu); cx.add_action(TerminalView::copy); cx.add_action(TerminalView::paste); cx.add_action(TerminalView::clear); cx.add_action(TerminalView::show_character_palette); } ///A terminal view, maintains the PTY's file handles and communicates with the terminal pub struct TerminalView { terminal: ModelHandle, has_new_content: bool, //Currently using iTerm bell, show bell emoji in tab until input is received has_bell: bool, // Only for styling purposes. Doesn't effect behavior modal: bool, context_menu: ViewHandle, blink_state: bool, blinking_on: bool, blinking_paused: bool, blink_epoch: usize, } impl Entity for TerminalView { type Event = Event; } impl TerminalView { pub fn from_terminal( terminal: ModelHandle, modal: bool, cx: &mut ViewContext, ) -> Self { cx.observe(&terminal, |_, _, cx| cx.notify()).detach(); cx.subscribe(&terminal, |this, _, event, cx| match event { Event::Wakeup => { if !cx.is_self_focused() { this.has_new_content = true; cx.notify(); } cx.emit(Event::Wakeup); } Event::Bell => { this.has_bell = true; cx.emit(Event::Wakeup); } Event::BlinkChanged => this.blinking_on = !this.blinking_on, Event::TitleChanged => { // if let Some(foreground_info) = &terminal.read(cx).foreground_process_info { // let cwd = foreground_info.cwd.clone(); //TODO // let item_id = self.item_id; // let workspace_id = self.workspace_id; cx.background() .spawn(async move { TERMINAL_DB .save_working_directory(0, 0, PathBuf::new()) .await .log_err(); }) .detach(); // } } _ => cx.emit(*event), }) .detach(); Self { terminal, has_new_content: true, has_bell: false, modal, context_menu: cx.add_view(ContextMenu::new), blink_state: true, blinking_on: false, blinking_paused: false, blink_epoch: 0, } } pub fn handle(&self) -> ModelHandle { self.terminal.clone() } pub fn has_new_content(&self) -> bool { self.has_new_content } pub fn has_bell(&self) -> bool { self.has_bell } pub fn clear_bel(&mut self, cx: &mut ViewContext) { self.has_bell = false; cx.emit(Event::Wakeup); } pub fn deploy_context_menu(&mut self, action: &DeployContextMenu, cx: &mut ViewContext) { let menu_entries = vec![ ContextMenuItem::item("Clear", Clear), ContextMenuItem::item("Close", pane::CloseActiveItem), ]; self.context_menu.update(cx, |menu, cx| { menu.show(action.position, AnchorCorner::TopLeft, menu_entries, cx) }); cx.notify(); } fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext) { if !self .terminal .read(cx) .last_content .mode .contains(TermMode::ALT_SCREEN) { cx.show_character_palette(); } else { self.terminal.update(cx, |term, cx| { term.try_keystroke( &Keystroke::parse("ctrl-cmd-space").unwrap(), cx.global::() .terminal_overrides .option_as_meta .unwrap_or(false), ) }); } } fn clear(&mut self, _: &Clear, cx: &mut ViewContext) { self.terminal.update(cx, |term, _| term.clear()); cx.notify(); } pub fn should_show_cursor( &self, focused: bool, cx: &mut gpui::RenderContext<'_, Self>, ) -> bool { //Don't blink the cursor when not focused, blinking is disabled, or paused if !focused || !self.blinking_on || self.blinking_paused || self .terminal .read(cx) .last_content .mode .contains(TermMode::ALT_SCREEN) { return true; } let setting = { let settings = cx.global::(); settings .terminal_overrides .blinking .clone() .unwrap_or(TerminalBlink::TerminalControlled) }; match setting { //If the user requested to never blink, don't blink it. TerminalBlink::Off => true, //If the terminal is controlling it, check terminal mode TerminalBlink::TerminalControlled | TerminalBlink::On => self.blink_state, } } fn blink_cursors(&mut self, epoch: usize, cx: &mut ViewContext) { if epoch == self.blink_epoch && !self.blinking_paused { self.blink_state = !self.blink_state; cx.notify(); let epoch = self.next_blink_epoch(); cx.spawn(|this, mut cx| { let this = this.downgrade(); async move { Timer::after(CURSOR_BLINK_INTERVAL).await; if let Some(this) = this.upgrade(&cx) { this.update(&mut cx, |this, cx| this.blink_cursors(epoch, cx)); } } }) .detach(); } } pub fn pause_cursor_blinking(&mut self, cx: &mut ViewContext) { self.blink_state = true; cx.notify(); let epoch = self.next_blink_epoch(); cx.spawn(|this, mut cx| { let this = this.downgrade(); async move { Timer::after(CURSOR_BLINK_INTERVAL).await; if let Some(this) = this.upgrade(&cx) { this.update(&mut cx, |this, cx| this.resume_cursor_blinking(epoch, cx)) } } }) .detach(); } pub fn find_matches( &mut self, query: project::search::SearchQuery, cx: &mut ViewContext, ) -> Task>> { let searcher = regex_search_for_query(query); if let Some(searcher) = searcher { self.terminal .update(cx, |term, cx| term.find_matches(searcher, cx)) } else { cx.background().spawn(async { Vec::new() }) } } pub fn terminal(&self) -> &ModelHandle { &self.terminal } fn next_blink_epoch(&mut self) -> usize { self.blink_epoch += 1; self.blink_epoch } fn resume_cursor_blinking(&mut self, epoch: usize, cx: &mut ViewContext) { if epoch == self.blink_epoch { self.blinking_paused = false; self.blink_cursors(epoch, cx); } } ///Attempt to paste the clipboard into the terminal fn copy(&mut self, _: &Copy, cx: &mut ViewContext) { self.terminal.update(cx, |term, _| term.copy()) } ///Attempt to paste the clipboard into the terminal fn paste(&mut self, _: &Paste, cx: &mut ViewContext) { if let Some(item) = cx.read_from_clipboard() { self.terminal .update(cx, |terminal, _cx| terminal.paste(item.text())); } } fn send_text(&mut self, text: &SendText, cx: &mut ViewContext) { self.clear_bel(cx); self.terminal.update(cx, |term, _| { term.input(text.0.to_string()); }); } fn send_keystroke(&mut self, text: &SendKeystroke, cx: &mut ViewContext) { if let Some(keystroke) = Keystroke::parse(&text.0).log_err() { self.clear_bel(cx); self.terminal.update(cx, |term, cx| { term.try_keystroke( &keystroke, cx.global::() .terminal_overrides .option_as_meta .unwrap_or(false), ); }); } } } pub fn regex_search_for_query(query: project::search::SearchQuery) -> Option { let searcher = match query { project::search::SearchQuery::Text { query, .. } => RegexSearch::new(&query), project::search::SearchQuery::Regex { query, .. } => RegexSearch::new(&query), }; searcher.ok() } impl View for TerminalView { fn ui_name() -> &'static str { "Terminal" } fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox { let terminal_handle = self.terminal.clone().downgrade(); let self_id = cx.view_id(); let focused = cx .focused_view_id(cx.window_id()) .filter(|view_id| *view_id == self_id) .is_some(); Stack::new() .with_child( TerminalElement::new( cx.handle(), terminal_handle, focused, self.should_show_cursor(focused, cx), ) .contained() .boxed(), ) .with_child(ChildView::new(&self.context_menu, cx).boxed()) .boxed() } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { self.has_new_content = false; self.terminal.read(cx).focus_in(); self.blink_cursors(self.blink_epoch, cx); cx.notify(); } fn focus_out(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { self.terminal.update(cx, |terminal, _| { terminal.focus_out(); }); cx.notify(); } fn key_down(&mut self, event: &gpui::KeyDownEvent, cx: &mut ViewContext) -> bool { self.clear_bel(cx); self.pause_cursor_blinking(cx); self.terminal.update(cx, |term, cx| { term.try_keystroke( &event.keystroke, cx.global::() .terminal_overrides .option_as_meta .unwrap_or(false), ) }) } //IME stuff fn selected_text_range(&self, cx: &AppContext) -> Option> { if self .terminal .read(cx) .last_content .mode .contains(TermMode::ALT_SCREEN) { None } else { Some(0..0) } } fn replace_text_in_range( &mut self, _: Option>, text: &str, cx: &mut ViewContext, ) { self.terminal.update(cx, |terminal, _| { terminal.input(text.into()); }); } fn keymap_context(&self, cx: &gpui::AppContext) -> gpui::keymap::Context { let mut context = Self::default_keymap_context(); if self.modal { context.set.insert("ModalTerminal".into()); } let mode = self.terminal.read(cx).last_content.mode; context.map.insert( "screen".to_string(), (if mode.contains(TermMode::ALT_SCREEN) { "alt" } else { "normal" }) .to_string(), ); if mode.contains(TermMode::APP_CURSOR) { context.set.insert("DECCKM".to_string()); } if mode.contains(TermMode::APP_KEYPAD) { context.set.insert("DECPAM".to_string()); } //Note the ! here if !mode.contains(TermMode::APP_KEYPAD) { context.set.insert("DECPNM".to_string()); } if mode.contains(TermMode::SHOW_CURSOR) { context.set.insert("DECTCEM".to_string()); } if mode.contains(TermMode::LINE_WRAP) { context.set.insert("DECAWM".to_string()); } if mode.contains(TermMode::ORIGIN) { context.set.insert("DECOM".to_string()); } if mode.contains(TermMode::INSERT) { context.set.insert("IRM".to_string()); } //LNM is apparently the name for this. https://vt100.net/docs/vt510-rm/LNM.html if mode.contains(TermMode::LINE_FEED_NEW_LINE) { context.set.insert("LNM".to_string()); } if mode.contains(TermMode::FOCUS_IN_OUT) { context.set.insert("report_focus".to_string()); } if mode.contains(TermMode::ALTERNATE_SCROLL) { context.set.insert("alternate_scroll".to_string()); } if mode.contains(TermMode::BRACKETED_PASTE) { context.set.insert("bracketed_paste".to_string()); } if mode.intersects(TermMode::MOUSE_MODE) { context.set.insert("any_mouse_reporting".to_string()); } { let mouse_reporting = if mode.contains(TermMode::MOUSE_REPORT_CLICK) { "click" } else if mode.contains(TermMode::MOUSE_DRAG) { "drag" } else if mode.contains(TermMode::MOUSE_MOTION) { "motion" } else { "off" }; context .map .insert("mouse_reporting".to_string(), mouse_reporting.to_string()); } { let format = if mode.contains(TermMode::SGR_MOUSE) { "sgr" } else if mode.contains(TermMode::UTF8_MOUSE) { "utf8" } else { "normal" }; context .map .insert("mouse_format".to_string(), format.to_string()); } context } }