Visual Studio Code, rebuilt in Rust
Rust
//! The Terminal view (terminalView.ts `TerminalViewPane` with terminalTabbedView.ts): the groups of terminals,
//! side by side when split, with the tabs list at their right once there are two terminals.
pub(super) mod grid;
#[cfg(test)]
mod tests;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;
use futures::StreamExt;
use futures::channel::mpsc::UnboundedReceiver;
use ghostty_vt::{Config, CursorShape, CursorStyle, Scroll, SelectionType, Side, TermMode};
use gpui::{Bounds, ClipboardItem, Context, Div, FocusHandle, FontWeight, Keystroke, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, ScrollDelta, ScrollWheelEvent, SharedString, Size, Window, canvas, div, prelude::*, px};
use config::configuration::Overrides;
use config::service::Target;
use serde_json::{Value, json};
use self::grid::{CellMetrics, GUTTER, Look, SCROLLBAR_WIDTH, SCROLLBAR_WIDTH_MODERN};
use super::Workbench;
use super::panel::PanelView;
use super::task_service::WaitOnExit;
use crate::commands::Outcome;
use crate::keybindings::Resolution;
use crate::main_thread_quick_open::Owner;
use crate::menus::{MenuAction, MenuId};
use crate::quick_input::{Button, Entry, Event, Item, Kind, QuickInput, Severity};
use crate::terminal::colors::TerminalColors;
use crate::terminal::environment_collection::EnvironmentCollections;
use crate::terminal::keys::{self, KeyResult};
use crate::terminal::mouse::{self, MouseAction};
use crate::terminal::profiles::{self, Profile};
use crate::terminal::pty::PtyEvent;
use crate::terminal::skip_shell::DEFAULT_COMMANDS_TO_SKIP_SHELL;
use crate::terminal::{ShellLaunchConfig, TerminalInstance, create_environment};
/// `TerminalExitReason`.
pub(super) const EXIT_REASON_PROCESS: u8 = 2;
pub(super) const EXIT_REASON_USER: u8 = 3;
pub(super) const EXIT_REASON_EXTENSION: u8 = 4;
/// terminal.ts `TERMINAL_VIEW_ID`.
pub const TERMINAL_VIEW_ID: &str = "terminal";
/// terminalInstance.ts `_setupTitlePolling`: node-pty's process title is read every 200 ms on macOS.
const TITLE_POLL_INTERVAL: Duration = Duration::from_millis(200);
/// xterm.js CursorBlinkStateManager `BLINK_INTERVAL`.
const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(600);
/// terminalTabsList.ts `TerminalTabsListSizes`.
const TAB_HEIGHT: f32 = 22.;
const TABS_DEFAULT_WIDTH: f32 = 120.;
const TABS_ACTIONBAR_MINIMUM_WIDTH: f32 = 105.;
/// monaco's `ScrollableElement` wheel sensitivity: px per wheel unit.
const SCROLL_WHEEL_SENSITIVITY: f32 = 50.;
pub struct TerminalView {
pub(super) focus: FocusHandle,
pub(super) instances: Vec<TerminalInstance>,
/// terminalService.ts `createDetachedTerminal`: read-only terminals without a process, in no group or list.
pub(super) detached: Vec<TerminalInstance>,
/// terminalGroupService.ts groups: each holds instance ids, laid out side by side.
pub(super) groups: Vec<Group>,
pub(super) active_group: usize,
/// terminalEditorService.ts instances: the terminals the editor area shows, in no group.
pub(super) editor_instances: Vec<u32>,
/// The restored terminal editors whose process starts once a window is there (`reviveInput`).
pub(super) revives: Vec<super::terminal_editor::TerminalRevive>,
/// `terminalService.activeInstance` when it is an editor's terminal, else the active group's pane is.
pub(super) active_editor: Option<u32>,
next_id: u32,
pub(super) metrics: Option<CellMetrics>,
/// Where each instance's pane painted, for its mouse events.
pub(super) pane_bounds: HashMap<u32, Bounds<Pixels>>,
hovered: Option<u32>,
/// The instance a left button drag selects in.
selecting: Option<u32>,
/// The wheel's fraction of a line not scrolled yet.
scroll_remainder: f32,
title_polling: bool,
/// The focused cursor blinks (`cursorBlinking` or a program's DECSCUSR) and is in its hidden phase.
cursor_blinking: bool,
cursor_blink_off: bool,
cursor_blink_running: bool,
/// The Cmd+trackpad zoom's fraction of a step not applied yet.
pub(super) zoom_remainder: f32,
/// Set once a terminal was created: `terminalHasBeenCreated`.
has_been_created: bool,
pub(super) find: super::terminal_find::TerminalFind,
/// What the extension host was told (mainThreadTerminalService.ts): the active terminal and the events it asked for.
pub(super) host: HostTerminals,
pub(super) suggest: super::terminal_suggest::TerminalSuggest,
pub(super) history: super::terminal_history::TerminalHistories,
/// The instance whose sticky scroll overlay the pointer is over.
pub(super) sticky_hovered: Option<u32>,
/// The command guide under the pointer: its instance and buffer rows.
pub(super) command_guide: Option<(u32, std::ops::Range<i64>)>,
/// The shown OSC 99 notifications: instance, the handler's key, the notification's key.
pub(super) osc99_notifications: Vec<(u32, u64, u64)>,
/// The last resize: its instance, `cols x rows` and the timer that hides it.
pub(super) resize_overlay: Option<(u32, String, gpui::Task<()>)>,
pub(super) quick_fixes: super::terminal_quick_fix::TerminalQuickFixes,
/// The link under the pointer and whether the link modifier is down.
pub(super) hovered_link: Option<super::terminal_links::HoveredLink>,
/// The link whose hover shows: a click on the hover's link activates it (terminalLinkManager.ts `_showHover`).
pub(super) link_in_hover: Option<super::terminal_links::TerminalLink>,
pub(super) link_modifier: bool,
pub(super) extension_links: super::terminal_links::ExtensionLinks,
/// The instances whose initial hint a key or a click dismissed for good.
pub(super) initial_hint_dismissed: std::collections::HashSet<u32>,
/// Where the title's "..." painted, the anchor of its menu.
more_actions_bounds: std::rc::Rc<std::cell::Cell<Bounds<Pixels>>>,
}
#[derive(Default)]
pub(super) struct HostTerminals {
pub active: Option<u32>,
/// `$startSendingDataEvents`: process data goes to the host (`window.onDidWriteTerminalData`).
pub data_events: bool,
/// `$startSendingCommandEvents`: finished commands go to the host (`onDidExecuteTerminalCommand`).
pub command_events: bool,
pub environment_collections: EnvironmentCollections,
pub reported: HashMap<u32, super::terminal_host::Reported>,
/// `$registerProfileProvider`: the extension and profile id of each provider.
pub profile_providers: Vec<(String, String)>,
}
pub(super) struct Group {
pub(super) instances: Vec<u32>,
pub(super) active: usize,
}
/// terminal.ts `TerminalLocation`: a new instance joins a group of the panel, or the editor area opens it.
pub(super) enum TerminalLocation {
Panel { split_from: Option<u32> },
Editor,
}
/// A row of the profile picker.
#[derive(Clone)]
pub enum ProfilePick {
Shell(Profile),
Contributed(ContributedProfile),
}
impl ProfilePick {
/// `profileName`: what `terminal.integrated.defaultProfile.osx` names the profile by.
fn name(&self) -> &str {
match self {
ProfilePick::Shell(profile) => &profile.name,
ProfilePick::Contributed(profile) => &profile.title,
}
}
}
/// `IExtensionTerminalProfile`: an entry of an extension's `contributes.terminal.profiles`.
#[derive(Clone, Debug, PartialEq)]
pub struct ContributedProfile {
pub extension_id: String,
pub id: String,
pub title: String,
pub icon: Option<String>,
pub color: Option<String>,
}
/// What a terminal quick input does when it is accepted.
#[derive(Clone)]
pub enum TerminalInput {
/// `TerminalProfileQuickpick`: create a terminal with the picked profile, in the editor area with `in_editor`, or
/// make it the default.
Profiles { profiles: Vec<ProfilePick>, set_default: bool, in_editor: bool },
/// `renameWithQuickPick` for an instance id.
Rename(u32),
/// `changeColor`'s picker: the standard color keys by handle, then Reset to default.
Color { instance: u32, colors: Vec<&'static str> },
/// `changeIcon`'s `IconPicker` quick widget.
Icon,
/// The "Configure Terminal Profile" button's name input: the profile to copy and the names already configured.
ProfileName { profile: Profile, existing: Vec<String> },
/// Send Sequence's input box.
SendSequence,
/// Write Data to Terminal's input box.
WriteData,
/// Send Signal's picker, then its manual entry.
Signal,
ManualSignal,
/// Run Recent Command and Go to Recent Directory: what each pick runs, in which terminal.
RunRecent { kind: super::terminal_history::RecentKind, picks: Vec<super::terminal_history::RecentPick>, fuzzy: bool, instance: u32 },
/// The command decorations' Toggle Visibility picker.
DecorationVisibility,
}
impl TerminalView {
pub fn new(cx: &mut gpui::App) -> Self {
TerminalView {
focus: cx.focus_handle().tab_stop(true),
instances: Vec::new(),
detached: Vec::new(),
groups: Vec::new(),
active_group: 0,
editor_instances: Vec::new(),
revives: Vec::new(),
active_editor: None,
next_id: 1,
metrics: None,
pane_bounds: HashMap::new(),
hovered: None,
selecting: None,
scroll_remainder: 0.,
title_polling: false,
cursor_blinking: false,
cursor_blink_off: false,
cursor_blink_running: false,
zoom_remainder: 0.,
find: super::terminal_find::TerminalFind::new(cx),
has_been_created: false,
host: HostTerminals::default(),
suggest: Default::default(),
history: Default::default(),
sticky_hovered: None,
command_guide: None,
osc99_notifications: Vec::new(),
resize_overlay: None,
quick_fixes: Default::default(),
hovered_link: None,
link_in_hover: None,
link_modifier: false,
extension_links: Default::default(),
initial_hint_dismissed: Default::default(),
more_actions_bounds: Default::default(),
}
}
pub(super) fn active_instance_id(&self) -> Option<u32> {
self.active_editor.or_else(|| self.group_active_instance_id())
}
/// `terminalGroupService.activeInstance`: the active group's pane.
pub(super) fn group_active_instance_id(&self) -> Option<u32> {
let group = self.groups.get(self.active_group)?;
group.instances.get(group.active).copied()
}
pub(super) fn reserve_id(&mut self) -> u32 {
self.next_id += 1;
self.next_id - 1
}
pub(super) fn instance(&self, id: u32) -> Option<&TerminalInstance> {
self.instances.iter().chain(&self.detached).find(|instance| instance.id == id)
}
pub(super) fn instance_mut(&mut self, id: u32) -> Option<&mut TerminalInstance> {
self.instances.iter_mut().chain(&mut self.detached).find(|instance| instance.id == id)
}
/// A detached terminal of `rows` by `cols`, its id counting down from `u32::MAX` so the instance ids of the
/// terminals stay as upstream's.
pub(super) fn create_detached(&mut self, cols: u16, rows: u16, config: Config) -> u32 {
let id = u32::MAX - self.detached.len() as u32;
let launch = crate::terminal::ShellLaunchConfig { extension_pty: true, hide_from_user: true, ..Default::default() };
self.detached.push(TerminalInstance::new(id, launch, cols, rows, config).0);
id
}
/// The pane's cell: testResultsOutput.ts attaches a detached terminal with `enableGpu: false`, so its
/// DomRenderer cell is the unrounded advance.
pub(super) fn pane_metrics(&self, id: u32) -> Option<CellMetrics> {
let metrics = self.metrics.clone()?;
if !self.detached.iter().any(|instance| instance.id == id) {
return Some(metrics);
}
Some(CellMetrics { cell_width: metrics.dom_cell_width, ..metrics })
}
pub(super) fn active_instance_mut(&mut self) -> Option<&mut TerminalInstance> {
let id = self.active_instance_id()?;
self.instance_mut(id)
}
/// The instance ids of each group, in order.
pub(super) fn group_instances(&self) -> Vec<Vec<u32>> {
self.groups.iter().map(|group| group.instances.clone()).collect()
}
/// `showBackgroundTerminal`: a terminal hidden from the user joins the view as its own group.
pub(super) fn show_background(&mut self, id: u32) {
if self.instance(id).is_some() && !self.groups.iter().any(|group| group.instances.contains(&id)) {
self.groups.push(Group { instances: vec![id], active: 0 });
}
}
/// `setActiveInstance`: the group holding `id` and its pane become active.
pub(super) fn set_active(&mut self, id: u32) {
self.active_editor = self.editor_instances.contains(&id).then_some(id);
for (group_index, group) in self.groups.iter_mut().enumerate() {
if let Some(index) = group.instances.iter().position(|&instance| instance == id) {
group.active = index;
self.active_group = group_index;
}
}
}
/// `terminalGroupService.removeInstance`: the pane leaves its group, and an emptied group goes.
pub(super) fn remove_from_groups(&mut self, id: u32) {
for group in &mut self.groups {
if let Some(index) = group.instances.iter().position(|&instance| instance == id) {
group.instances.remove(index);
group.active = group.active.min(group.instances.len().saturating_sub(1));
}
}
if let Some(empty) = self.groups.iter().position(|group| group.instances.is_empty()) {
self.groups.remove(empty);
if self.active_group >= empty && self.active_group > 0 {
self.active_group -= 1;
}
}
}
/// `terminalGroupService.createGroup(instance)`: the instance becomes the active group of its own.
pub(super) fn add_group(&mut self, id: u32) {
self.groups.push(Group { instances: vec![id], active: 0 });
self.active_group = self.groups.len() - 1;
}
/// terminalTabbedView.ts `_shouldShowTabs`.
fn should_show_tabs(&self, enabled: bool, hide_condition: &str) -> bool {
enabled
&& match hide_condition {
"never" => true,
"singleTerminal" => self.groups.iter().map(|group| group.instances.len()).sum::<usize>() > 1,
"singleGroup" => self.groups.len() > 1,
_ => false,
}
}
}
impl Workbench {
pub(super) fn register_terminal_actions(&mut self) {
self.commands.register("workbench.action.terminal.toggleTerminal", |workbench, _, window, cx| {
if workbench.terminal_view_visible() && workbench.terminal_view.focus.is_focused(window) {
workbench.close_terminal_view(cx);
} else {
workbench.open_terminal_view(window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.focus", |workbench, _, window, cx| {
workbench.open_terminal_view(window, cx);
Outcome::done()
});
self.commands.register("workbench.action.terminal.new", |workbench, _, window, cx| {
workbench.create_terminal(None, false, window, cx);
Outcome::done()
});
self.commands.register("workbench.action.terminal.split", |workbench, _, window, cx| {
workbench.create_terminal(None, true, window, cx);
Outcome::done()
});
self.commands.register("workbench.action.terminal.newWithProfile", |workbench, args, window, cx| {
// `{ profileName, location? }` creates the named profile from `availableProfiles` (no contributed ones).
if let Some(name) = args.first().and_then(|args| args.get("profileName")) {
let Some(profile) = workbench.terminal_profiles().into_iter().find(|profile| *name == profile.name) else {
return Outcome::error(format!("Could not find terminal profile \"{}\"", name.as_str().unwrap_or_default()));
};
if args[0]["location"] == "editor" {
workbench.create_terminal_editor(Some(profile), super::terminal_editor::EditorTarget::Active, window, cx);
} else {
workbench.create_terminal(Some(profile), false, window, cx);
}
return Outcome::done();
}
workbench.show_terminal_profiles(false, super::terminal_editor::profile_location_is_editor(&args), window, cx);
Outcome::done()
});
self.commands.register("workbench.action.terminal.selectDefaultShell", |workbench, _, window, cx| {
workbench.show_terminal_profiles(true, false, window, cx);
Outcome::done()
});
self.commands.register("workbench.action.terminal.kill", |workbench, _, window, cx| {
if let Some(id) = workbench.terminal_view.active_instance_id() {
workbench.kill_terminal(id, window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.killViewOrEditor", |workbench, _, window, cx| {
if let Some(id) = workbench.terminal_view.active_instance_id() {
workbench.kill_terminal(id, window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.killEditor", |workbench, _, window, cx| {
if let Some(id) = workbench.terminal_view.active_instance_id() {
workbench.kill_terminal(id, window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.killAll", |workbench, _, window, cx| {
let ids: Vec<u32> = workbench.terminal_view.instances.iter().map(|instance| instance.id).collect();
for id in ids {
workbench.kill_terminal(id, window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.rename", |workbench, _, window, cx| {
if let Some(id) = workbench.terminal_view.active_instance_id() {
workbench.show_terminal_rename(id, window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.changeColor", |workbench, _, window, cx| {
if let Some(id) = workbench.terminal_view.active_instance_id() {
workbench.show_terminal_color_picker(id, window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.changeIcon", |workbench, _, window, cx| {
if let Some(id) = workbench.terminal_view.active_instance_id() {
workbench.show_terminal_icon_picker(id, window, cx);
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.sizeToContentWidth", |workbench, _, window, cx| {
let Some(id) = workbench.terminal_view.active_instance_id() else { return Outcome::done() };
let char_width = workbench.terminal_metrics(window).cell_width;
if let Some(instance) = workbench.terminal_view.instance_mut(id) {
instance.toggle_size_to_content_width(char_width);
}
workbench.terminal_view.focus.focus(window, cx);
cx.notify();
Outcome::done()
});
// terminalActions.ts: the *ActiveTab commands act on the tab list's selection, which is the active row here.
for (tab_command, command) in [
("workbench.action.terminal.splitActiveTab", "workbench.action.terminal.split"),
("workbench.action.terminal.renameActiveTab", "workbench.action.terminal.rename"),
("workbench.action.terminal.changeIconActiveTab", "workbench.action.terminal.changeIcon"),
("workbench.action.terminal.changeColorActiveTab", "workbench.action.terminal.changeColor"),
("workbench.action.terminal.killActiveTab", "workbench.action.terminal.kill"),
] {
self.commands.register(tab_command, move |workbench, args, window, cx| workbench.execute_command(command, args, window, cx));
}
self.commands.register("workbench.action.terminal.clear", |workbench, _, _, cx| {
if let Some(instance) = workbench.terminal_view.active_instance_mut() {
instance.clear();
}
cx.notify();
Outcome::done()
});
self.commands.register("workbench.action.terminal.runSelectedText", |workbench, _, window, cx| {
workbench.run_selected_text_in_terminal(window, cx);
Outcome::done()
});
self.commands.register("workbench.action.terminal.runActiveFile", |workbench, _, window, cx| {
workbench.run_active_file_in_terminal(window, cx);
Outcome::done()
});
self.commands.register("workbench.action.terminal.copySelection", |workbench, _, _, cx| {
if let Some(text) = workbench.terminal_view.active_instance_mut().and_then(|instance| instance.selection_text()) {
cx.write_to_clipboard(ClipboardItem::new_string(text));
}
Outcome::done()
});
self.commands.register("workbench.action.terminal.paste", |workbench, _, _, cx| {
let text = cx.read_from_clipboard().and_then(|item| item.text());
if let (Some(text), Some(instance)) = (text, workbench.terminal_view.active_instance_mut()) {
instance.paste(&text);
}
cx.notify();
Outcome::done()
});
self.commands.register("workbench.action.terminal.selectAll", |workbench, _, _, cx| {
if let Some(instance) = workbench.terminal_view.active_instance_mut() {
instance.select_all();
}
workbench.sync_terminals_to_host();
cx.notify();
Outcome::done()
});
self.commands.register("workbench.action.terminal.clearSelection", |workbench, _, _, cx| {
if let Some(instance) = workbench.terminal_view.active_instance_mut() {
instance.term.clear_selection();
}
workbench.sync_terminals_to_host();
cx.notify();
Outcome::done()
});
self.commands.register("workbench.action.terminal.sendSequence", |workbench, args, _, cx| {
let text = args.first().and_then(|args| args["text"].as_str()).map(str::to_owned);
if let (Some(text), Some(instance)) = (text, workbench.terminal_view.active_instance_mut()) {
instance.input(&text);
}
cx.notify();
Outcome::done()
});
for (command, scroll) in [
("workbench.action.terminal.scrollUp", Scroll::Delta(1)),
("workbench.action.terminal.scrollDown", Scroll::Delta(-1)),
("workbench.action.terminal.scrollUpPage", Scroll::PageUp),
("workbench.action.terminal.scrollDownPage", Scroll::PageDown),
("workbench.action.terminal.scrollToTop", Scroll::Top),
("workbench.action.terminal.scrollToBottom", Scroll::Bottom),
] {
self.commands.register(command, move |workbench, _, _, cx| {
if let Some(instance) = workbench.terminal_view.active_instance_mut() {
instance.scroll(scroll);
}
cx.notify();
Outcome::done()
});
}
for (command, step) in [("workbench.action.terminal.focusNext", 1), ("workbench.action.terminal.focusPrevious", -1)] {
self.commands.register(command, move |workbench, _, window, cx| {
let view = &mut workbench.terminal_view;
if !view.groups.is_empty() {
view.active_group = (view.active_group as isize + step).rem_euclid(view.groups.len() as isize) as usize;
}
workbench.open_terminal_view(window, cx);
Outcome::done()
});
}
for (command, step) in [("workbench.action.terminal.focusNextPane", 1), ("workbench.action.terminal.focusPreviousPane", -1)] {
self.commands.register(command, move |workbench, _, window, cx| {
if let Some(group) = workbench.terminal_view.groups.get_mut(workbench.terminal_view.active_group) {
group.active = (group.active as isize + step).rem_euclid(group.instances.len() as isize) as usize;
}
workbench.open_terminal_view(window, cx);
Outcome::done()
});
}
self.register_external_terminal_actions();
self.register_terminal_contrib_actions();
self.register_terminal_suggest_actions();
self.register_terminal_quick_fix_actions();
self.register_terminal_initial_hint_actions();
self.register_terminal_history_actions();
self.register_terminal_sticky_scroll_actions();
self.register_terminal_environment_actions();
self.register_terminal_decoration_actions();
self.register_terminal_find_actions();
}
/// `isViewVisible(TERMINAL_VIEW_ID)`: the panel shows the Terminal view.
pub(crate) fn terminal_view_visible(&self) -> bool {
self.panel_view_visible(TERMINAL_VIEW_ID)
}
/// terminal.contribution.ts registers the Terminal view container in the panel.
pub(super) fn register_terminal_panel_view(&mut self) {
self.register_panel_view(PanelView {
id: TERMINAL_VIEW_ID,
view_id: "terminal",
title: "Terminal",
open: |workbench, window, cx| drop(workbench.execute_command("workbench.action.terminal.focus", Vec::new(), window, cx)),
render: |workbench, window, cx| {
// terminalView.ts `_initializeTerminal`: the view made visible without terminals creates one.
if workbench.terminal_view.groups.is_empty() {
workbench.create_terminal(None, false, window, cx);
}
workbench.terminal_body(window, cx).into_any_element()
},
actions: |workbench, _, cx| workbench.terminal_title_actions(cx).into_any_element(),
visible: |_| true,
badge: |_| None,
});
}
/// `terminalGroupService.showPanel(true)`: shows the view, creating a terminal when there is none.
pub(super) fn open_terminal_view(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.terminal_view.groups.is_empty() {
self.create_terminal(None, false, window, cx);
return;
}
self.show_terminal_tab(window, cx);
}
pub(super) fn show_terminal_tab(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.open_panel_view(TERMINAL_VIEW_ID, cx);
self.terminal_view.active_editor = None;
window.focus(&self.terminal_view.focus, cx);
self.sync_terminals_to_host();
}
/// `viewsService.closeView(TERMINAL_VIEW_ID)`: the terminals keep running.
pub(super) fn close_terminal_view(&mut self, cx: &mut Context<Self>) {
self.close_panel_view(TERMINAL_VIEW_ID, cx);
}
/// `createTerminal`: a new group, or a pane after the active one with `split`; the split's cwd is its parent's
/// (`terminal.integrated.splitCwd` inherited).
pub(super) fn create_terminal(&mut self, profile: Option<Profile>, split: bool, window: &mut Window, cx: &mut Context<Self>) {
self.create_terminal_with_cwd(profile, split, None, window, cx);
}
/// `createTerminal({ config: { cwd } })` of externalTerminal.contribution.ts `openInIntegratedTerminal`.
pub(super) fn create_terminal_in(&mut self, cwd: PathBuf, window: &mut Window, cx: &mut Context<Self>) {
self.create_terminal_with_cwd(None, false, Some(cwd), window, cx);
}
fn create_terminal_with_cwd(&mut self, profile: Option<Profile>, split: bool, cwd: Option<PathBuf>, window: &mut Window, cx: &mut Context<Self>) {
if profile.is_none()
&& let Some(contributed) = self.contributed_default_profile()
{
// TerminalLocation.Panel
let location = if split && self.terminal_view.active_instance_id().is_some() { json!({ "splitActiveTerminal": true }) } else { json!(1) };
return self.create_contributed_terminal(contributed, location, cwd, window, cx);
}
let profile = profile.unwrap_or_else(|| self.default_terminal_profile());
let parent = self.terminal_view.active_instance_id().filter(|_| split);
let parent_cwd = match parent {
Some(id) if self.setting("terminal.integrated.splitCwd") == "inherited" => self.terminal_view.instance(id).and_then(|parent| parent.cwd.clone()),
_ => None,
};
let cwd = cwd.or(parent_cwd.map(PathBuf::from)).unwrap_or_else(|| self.terminal_initial_cwd());
let launch = self.shell_launch_config(&profile, cwd);
self.add_terminal(launch, parent, window, cx);
self.show_terminal_tab(window, cx);
}
/// terminalService.ts `createTerminal` and terminalInstance.ts's process start: the instance joins a new group,
/// or `split_from`'s group after it, unless it is hidden from the user; the extension host hears of it.
pub(super) fn add_terminal(&mut self, launch: ShellLaunchConfig, split_from: Option<u32>, window: &mut Window, cx: &mut Context<Self>) -> u32 {
self.add_terminal_at(launch, TerminalLocation::Panel { split_from }, window, cx)
}
pub(super) fn add_terminal_at(&mut self, launch: ShellLaunchConfig, location: TerminalLocation, window: &mut Window, cx: &mut Context<Self>) -> u32 {
let id = self.terminal_view.reserve_id();
let view = &self.terminal_view;
let size = view.active_instance_id().and_then(|id| view.instance(id)).map_or((80, 30), |instance| (instance.term.columns() as u16, instance.term.screen_lines() as u16));
self.add_terminal_with_id(id, launch, size, location, window, cx);
id
}
pub(super) fn add_terminal_with_id(&mut self, id: u32, mut launch: ShellLaunchConfig, (cols, rows): (u16, u16), location: TerminalLocation, window: &mut Window, cx: &mut Context<Self>) {
self.prepare_terminal_launch(&mut launch);
let hidden = launch.hide_from_user;
let (mut instance, receiver) = TerminalInstance::new(id, launch, cols, rows, self.terminal_config());
if let Some(message) = instance.spawn_error.clone() {
let colors = TerminalColors::new(&self.theme);
instance.write_message(&message, &colors);
}
Self::connect_terminal_to_host(&self.host, &mut instance);
let view = &mut self.terminal_view;
view.instances.push(instance);
view.has_been_created = true;
if let TerminalLocation::Panel { split_from } = location
&& !hidden
{
let group = split_from.and_then(|parent| view.groups.iter().position(|group| group.instances.contains(&parent)));
match group {
Some(index) => {
let group = &mut view.groups[index];
let parent = group.instances.iter().position(|&instance| Some(instance) == split_from).expect("the group holds the parent");
group.instances.insert(parent + 1, id);
group.active = parent + 1;
view.active_group = index;
}
None => {
view.groups.push(Group { instances: vec![id], active: 0 });
view.active_group = view.groups.len() - 1;
}
}
}
if let Some(receiver) = receiver {
self.pump_terminal_events(id, receiver, window, cx);
}
self.terminal_opened(id, cx);
self.start_title_poll(cx);
self.start_cursor_blink(cx);
cx.notify();
}
/// terminalInstance.ts `reuseTerminal` of a task terminal: `launch` runs in it, after `initial_text`.
pub(super) fn reuse_terminal(&mut self, id: u32, mut launch: ShellLaunchConfig, initial_text: &str, window: &mut Window, cx: &mut Context<Self>) {
self.prepare_terminal_launch(&mut launch);
let colors = TerminalColors::new(&self.theme);
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
instance.scroll(Scroll::Bottom);
let receiver = instance.reuse(launch, initial_text, &colors);
if let Some(message) = instance.spawn_error.clone() {
instance.write_message(&message, &colors);
}
Self::connect_terminal_to_host(&self.host, instance);
if let Some(pid) = instance.pid {
self.host.send("ExtHostTerminalService", "$acceptTerminalProcessId", vec![json!(id), json!(pid)]);
}
if let Some(receiver) = receiver {
self.pump_terminal_events(id, receiver, window, cx);
}
self.start_extension_terminal(id, cx);
cx.notify();
}
/// `getDefaultProfile` over the detected and configured profiles.
pub(super) fn default_terminal_profile(&self) -> Profile {
let profiles = self.terminal_profiles();
let default_name = self.setting("terminal.integrated.defaultProfile.osx");
let env: HashMap<String, String> = std::env::vars().collect();
profiles::default_profile(&profiles, default_name.as_str(), &profiles::system_shell(&env))
}
fn terminal_profiles(&self) -> Vec<Profile> {
let shells = std::fs::read_to_string("/etc/shells").unwrap_or_default();
profiles::detect(&self.setting("terminal.integrated.profiles.osx"), &shells, std::env::var("PATH").ok().as_deref())
}
/// terminalProfileService.ts `contributedProfiles`: `contributes.terminal.profiles` with a string icon or none
/// (`hasValidTerminalIcon`), without the titles `terminal.integrated.profiles.osx` sets to null or a profile of
/// the user's takes.
pub(super) fn contributed_terminal_profiles(&self) -> Vec<ContributedProfile> {
let configured = self.setting("terminal.integrated.profiles.osx");
let available = self.terminal_profiles();
let mut contributed = Vec::new();
for extension in &self.extensions {
for profile in extension.description["contributes"]["terminal"]["profiles"].as_array().into_iter().flatten() {
let (Some(id), Some(title)) = (profile["id"].as_str(), profile["title"].as_str()) else { continue };
if !(profile["icon"].is_null() || profile["icon"].is_string()) || configured.get(title) == Some(&Value::Null) || available.iter().any(|profile| profile.name == title) {
continue;
}
contributed.push(ContributedProfile {
extension_id: extension.id.clone(),
id: id.to_owned(),
title: title.to_owned(),
icon: profile["icon"].as_str().map(str::to_owned),
color: profile["color"].as_str().map(str::to_owned),
});
}
}
contributed
}
/// `getContributedDefaultProfile`: the contributed profile `terminal.integrated.defaultProfile.osx` names.
fn contributed_default_profile(&self) -> Option<ContributedProfile> {
let default_name = self.setting("terminal.integrated.defaultProfile.osx");
self.contributed_terminal_profiles().into_iter().find(|profile| default_name == profile.title.as_str())
}
/// terminalService.ts `createContributedTerminalProfile`: the extension activates on `onTerminalProfile:<id>`,
/// then its provider creates the terminal through `$createContributedProfileTerminal`; the last panel terminal
/// becomes active after.
pub(super) fn create_contributed_terminal(&mut self, profile: ContributedProfile, location: Value, cwd: Option<PathBuf>, window: &mut Window, cx: &mut Context<Self>) {
let activation = self.host.activate_by_event(&format!("onTerminalProfile:{}", profile.id));
// An undefined option is no key over the RPC.
let mut options = json!({ "location": location });
for (key, value) in [("icon", profile.icon.clone()), ("color", profile.color.clone()), ("cwd", cwd.map(|cwd| cwd.to_string_lossy().into_owned()))] {
if let Some(value) = value {
options[key] = json!(value);
}
}
cx.spawn_in(window, async move |workbench, cx| {
let _ = activation.await;
let Ok(Some(answer)) = workbench.update(cx, |workbench, _| {
let registered = workbench.terminal_view.host.profile_providers.iter().any(|(extension, id)| extension.eq_ignore_ascii_case(&profile.extension_id) && *id == profile.id);
if !registered {
workbench.notify_error(format!("No terminal profile provider registered for id \"{}\"", profile.id));
return None;
}
Some(workbench.host.call("ExtHostTerminalService", "$createContributedProfileTerminal", vec![json!(profile.id), options]))
}) else {
return;
};
let answer = answer.await;
let _ = workbench.update(cx, |workbench, cx| {
if let Ok(Err(error)) = answer {
workbench.notify_error(error["message"].as_str().unwrap_or_default());
return;
}
if let Some(&last) = workbench.terminal_view.group_instances().concat().last() {
workbench.terminal_view.set_active(last);
cx.notify();
}
});
})
.detach();
}
/// `getCwd`: `terminal.integrated.cwd` with its variables resolved, against the workspace folder, else the
/// folder, else the home directory; one that fails to resolve is dropped (`_resolveCwd`).
pub(super) fn terminal_initial_cwd(&self) -> PathBuf {
let home = std::env::var_os("HOME").map(PathBuf::from).unwrap_or_else(|| PathBuf::from("/"));
let root = self.workspace().and_then(|workspace| workspace.folders.first()).map(|folder| folder.path.clone());
let configured = self.setting("terminal.integrated.cwd").as_str().filter(|cwd| !cwd.is_empty()).map(str::to_owned);
let resolved = configured.and_then(|cwd| match self.resolve_variables(&Value::String(cwd), root.clone()) {
Ok(Value::String(cwd)) if !cwd.is_empty() => Some(PathBuf::from(cwd)),
Ok(_) => None,
Err(error) => {
eprintln!("Could not resolve terminal cwd {error}");
None
}
});
match (resolved, root) {
(Some(cwd), _) if cwd.is_absolute() => cwd,
(Some(cwd), Some(root)) => root.join(cwd),
(_, Some(root)) => root,
(_, None) => home,
}
}
pub(super) fn shell_launch_config(&self, profile: &Profile, cwd: PathBuf) -> ShellLaunchConfig {
ShellLaunchConfig {
name: profile.override_name.then(|| profile.name.clone()),
executable: profile.path.clone(),
args: profile.args.clone(),
cwd,
env: self.terminal_environment(&profile.env),
icon: profile.icon.clone(),
color: profile.color.clone(),
..ShellLaunchConfig::default()
}
}
/// `createTerminalEnvironment` with `terminal.integrated.env.osx`, then the launch's own variables.
pub(super) fn terminal_environment(&self, launch_env: &HashMap<String, Option<String>>) -> HashMap<String, String> {
let configured_env: HashMap<String, Option<String>> = match self.setting("terminal.integrated.env.osx") {
Value::Object(map) => map.into_iter().map(|(key, value)| (key, value.as_str().map(str::to_owned))).collect(),
_ => HashMap::new(),
};
let detect_locale = self.setting("terminal.integrated.detectLocale");
create_environment(std::env::vars(), &[configured_env, launch_env.clone()], &crate::VERSION, "en", detect_locale.as_str().unwrap_or("auto"))
}
/// The PTY's output reaches the instance in the order it was read; the exit follows the last read.
fn pump_terminal_events(&mut self, id: u32, mut receiver: UnboundedReceiver<PtyEvent>, window: &mut Window, cx: &mut Context<Self>) {
// The pty-read thread would wake this task off the test scheduler's thread, which aborts `cargo test`.
if cfg!(test) {
return;
}
cx.spawn_in(window, async move |workbench, cx| {
while let Some(event) = receiver.next().await {
let mut events = vec![event];
while let Ok(event) = receiver.try_recv() {
events.push(event);
}
let handled = workbench.update_in(cx, |workbench, window, cx| workbench.terminal_events(id, events, window, cx));
if !matches!(handled, Ok(true)) {
break;
}
}
})
.detach();
}
/// Returns false once the instance is gone.
fn terminal_events(&mut self, id: u32, events: Vec<PtyEvent>, window: &mut Window, cx: &mut Context<Self>) -> bool {
for event in events {
match event {
PtyEvent::Data(bytes) => {
if self.terminal_data(id, &bytes, window, cx) {
self.sync_terminal_suggest(id, window, cx);
}
}
PtyEvent::Exit(code) => {
if let Some(instance) = self.terminal_view.instance_mut(id) {
instance.exited(code);
}
self.terminal_process_exited(id, code, window, cx);
return false;
}
}
}
self.terminal_view.instance(id).is_some()
}
/// Output of the process, a PTY's or an extension pty's: the screen, the clipboard and what the host listens to.
/// Returns whether the prompt input changed.
pub(super) fn terminal_data(&mut self, id: u32, bytes: &[u8], window: &mut Window, cx: &mut Context<Self>) -> bool {
let colors = TerminalColors::new(&self.theme);
let auto_replies = self.setting("terminal.integrated.autoReplies");
let Some(instance) = self.terminal_view.instance_mut(id) else { return false };
let processed = instance.process(bytes, &colors);
let input_changed = processed.input_changed;
instance.answer_auto_replies(&auto_replies, &processed.text);
// xtermTerminal.ts loads ClipboardAddon: OSC 52 writes go to the clipboard and `?` reads it back.
if let Some(text) = instance.clipboard_requests.drain(..).last() {
cx.write_to_clipboard(ClipboardItem::new_string(text));
}
if !instance.clipboard_loads.is_empty() {
let text = cx.read_from_clipboard().and_then(|item| item.text()).unwrap_or_default();
instance.answer_clipboard_loads(&text);
}
instance.bell = false;
self.task_output(id, &processed.text, cx);
self.record_terminal_history(id, &processed.shell_events);
let finished = processed.shell_events.iter().any(|event| matches!(event, crate::terminal::shell_integration::ShellEvent::CommandFinished(_)));
self.terminal_output_to_host(id, processed);
if finished {
self.terminal_command_finished_quick_fixes(id, cx);
}
self.terminal_osc99(id, window, cx);
cx.notify();
input_changed
}
/// terminalInstance.ts `_onProcessExit`: a non-zero exit shows its message, then the instance goes.
pub(super) fn terminal_process_exited(&mut self, id: u32, code: Option<i32>, window: &mut Window, cx: &mut Context<Self>) {
let task_wait = self.task_process_exited(id, code, window, cx);
if let Some(wait @ (WaitOnExit::Message(_) | WaitOnExit::Wait)) = task_wait {
let colors = TerminalColors::new(&self.theme);
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
// node-pty reports exit code 0 for a process a signal ended.
if let Some(message) = instance.exit_message(code.unwrap_or(0)) {
instance.process(format!("\r\n{}", crate::terminal::format_message_for_terminal(&message)).as_bytes(), &colors);
}
// `taskShellIntegrationWaitOnExitSequence`: D with the exit code ends the command before the message.
if let WaitOnExit::Message(message) = wait {
let message = format!("\x1b]633;D;{}\x07{message}", code.unwrap_or(0));
instance.process(crate::terminal::format_message_for_terminal(&message).as_bytes(), &colors);
}
instance.waiting_on_exit = true;
return cx.notify();
}
let Some(instance) = self.terminal_view.instance(id) else { return };
// node-pty reports exit code 0 for a process a signal ended.
let message = instance.exit_message(code.unwrap_or(0));
let alert = instance.shows_exit_alert(self.setting("terminal.integrated.showExitAlert") == true);
if let Some(message) = message {
if alert {
self.notify_error(message);
} else {
eprintln!("[terminal] {message}");
}
}
self.dispose_terminal(id, EXIT_REASON_PROCESS, window, cx);
}
/// `killInstance`: the process gets SIGHUP and the instance goes without an exit message.
pub(super) fn kill_terminal(&mut self, id: u32, window: &mut Window, cx: &mut Context<Self>) {
if let Some(instance) = self.terminal_view.instance_mut(id) {
instance.kill();
}
self.dispose_terminal(id, EXIT_REASON_USER, window, cx);
}
/// terminalGroup.ts `_onInstanceDisposed`: the next pane of the group becomes active; an empty group goes, and
/// with it the view when it was the last one and `terminal.integrated.hideOnLastClosed` is on.
pub(super) fn dispose_terminal(&mut self, id: u32, exit_reason: u8, window: &mut Window, cx: &mut Context<Self>) {
let view = &mut self.terminal_view;
let had_focus = view.focus.is_focused(window);
let Some(index) = view.instances.iter().position(|instance| instance.id == id) else { return };
let instance = view.instances.remove(index);
view.pane_bounds.remove(&id);
let in_editor = view.editor_instances.contains(&id);
view.editor_instances.retain(|instance| *instance != id);
view.active_editor = view.active_editor.filter(|active| *active != id);
view.remove_from_groups(id);
if in_editor {
self.close_terminal_editor(id, cx);
}
self.terminal_closed(instance, exit_reason);
// terminalTaskSystem.ts `onDisposed`: a task whose terminal goes ends with it.
self.task_process_exited(id, None, window, cx);
self.forget_task_terminal(id);
if in_editor {
if had_focus && self.editors.active().is_some() {
window.focus(&self.editor_focus, cx);
}
} else if self.terminal_view.groups.is_empty() {
self.hide_terminal_view_on_last_closed(cx);
} else if had_focus {
window.focus(&self.terminal_view.focus, cx);
}
cx.notify();
}
/// terminalService.ts: the group service's active instance gone, `hideOnLastClosed` hides the panel.
pub(super) fn hide_terminal_view_on_last_closed(&mut self, cx: &mut Context<Self>) {
if self.setting("terminal.integrated.hideOnLastClosed") == true {
self.close_terminal_view(cx);
}
}
/// xterm.js `CursorBlinkStateManager`: the cursor toggles every 600 ms while the focused terminal blinks.
fn start_cursor_blink(&mut self, cx: &mut Context<Self>) {
if self.terminal_view.cursor_blink_running {
return;
}
self.terminal_view.cursor_blink_running = true;
cx.spawn(async move |workbench, cx| {
loop {
cx.background_executor().timer(CURSOR_BLINK_INTERVAL).await;
let running = workbench.update(cx, |workbench, cx| {
let view = &mut workbench.terminal_view;
view.cursor_blink_off = view.cursor_blinking && !view.cursor_blink_off;
if view.cursor_blinking {
cx.notify();
}
view.cursor_blink_running = !view.instances.is_empty();
view.cursor_blink_running
});
if !matches!(running, Ok(true)) {
break;
}
}
})
.detach();
}
fn start_title_poll(&mut self, cx: &mut Context<Self>) {
if self.terminal_view.title_polling {
return;
}
self.terminal_view.title_polling = true;
cx.spawn(async move |workbench, cx| {
loop {
cx.background_executor().timer(TITLE_POLL_INTERVAL).await;
let running = workbench.update(cx, |workbench, cx| {
let view = &mut workbench.terminal_view;
let mut changed = false;
for instance in &mut view.instances {
changed |= instance.refresh_process();
}
view.title_polling = !view.instances.is_empty();
let polling = view.title_polling;
if changed {
workbench.sync_terminals_to_host();
cx.notify();
}
polling
});
if !matches!(running, Ok(true)) {
break;
}
}
})
.detach();
}
/// `TerminalProfileQuickpick.showAndGetResult`: configured profiles, then the contributed ones, then the detected
/// ones, each list with the profile `terminal.integrated.defaultProfile.osx` names first (`getDefaultProfileName`),
/// then by name.
fn show_terminal_profiles(&mut self, set_default: bool, in_editor: bool, window: &mut Window, cx: &mut Context<Self>) {
let default_name = self.setting("terminal.integrated.defaultProfile.osx").as_str().unwrap_or_default().to_owned();
let (detected, configured): (Vec<Profile>, Vec<Profile>) = self.terminal_profiles().into_iter().partition(|profile| profile.auto_detected);
let sort = |mut picks: Vec<ProfilePick>| {
picks.sort_by(|a, b| (b.name() == default_name).cmp(&(a.name() == default_name)).then_with(|| a.name().to_lowercase().cmp(&b.name().to_lowercase())));
picks
};
let shell = |profiles: Vec<Profile>| sort(profiles.into_iter().map(ProfilePick::Shell).collect());
let contributed = sort(self.contributed_terminal_profiles().into_iter().map(ProfilePick::Contributed).collect());
let mut entries = Vec::new();
let mut picks = Vec::new();
for (separator, section) in [("profiles", shell(configured)), ("contributed", contributed), ("detected", shell(detected))] {
if section.is_empty() && separator != "contributed" {
continue;
}
entries.push(Entry::Separator(Some(separator.into())));
for pick in section {
let handle = picks.len() as i64;
entries.push(Entry::Item(match &pick {
ProfilePick::Shell(profile) => Item {
handle,
label: format!("$({}) {}", profile.icon.as_deref().unwrap_or("terminal"), profile.name),
description: Some(profile_description(profile)),
buttons: vec![Button { handle: 0, codicon: Some("gear".into()), tooltip: Some("Configure Terminal Profile".into()), ..Button::default() }],
..Item::default()
},
// `ThemeIcon.fromString` for `$(id)`, else `fromId`; an id with no icon falls back to `terminal`.
ProfilePick::Contributed(profile) => {
let icon = profile.icon.as_deref().map(|icon| icon.strip_prefix("$(").and_then(|icon| icon.strip_suffix(')')).unwrap_or(icon));
let icon = icon.filter(|icon| crate::codicons::svg_path(&self.app_root, icon).exists()).unwrap_or("terminal");
Item { handle, label: format!("$({icon}) {}", profile.title), ..Item::default() }
}
}));
picks.push(pick);
}
}
let mut input = QuickInput::new(0, Kind::QuickPick);
input.placeholder = Some(if set_default { "Select your default terminal profile" } else { "Select the terminal profile to create" }.into());
input.set_items(entries);
let id = self.quick_inputs.add(input, Owner::Terminal(TerminalInput::Profiles { profiles: picks, set_default, in_editor }));
self.show_quick_input(id, window, cx);
self.flush_quick_input_events(window, cx);
}
fn show_terminal_rename(&mut self, id: u32, window: &mut Window, cx: &mut Context<Self>) {
let Some(instance) = self.terminal_view.instance(id) else { return };
let mut input = QuickInput::new(0, Kind::InputBox);
input.prompt = Some("Enter terminal name".into());
input.set_value(instance.title());
let id = self.quick_inputs.add(input, Owner::Terminal(TerminalInput::Rename(id)));
self.show_quick_input(id, window, cx);
self.flush_quick_input_events(window, cx);
}
/// terminalInstance.ts `changeColor`: a quick pick of the standard colors, each a colored `circle-filled`.
fn show_terminal_color_picker(&mut self, id: u32, window: &mut Window, cx: &mut Context<Self>) {
let colors = crate::terminal::colors::standard_colors();
let mut entries = Vec::new();
for (handle, key) in colors.iter().enumerate() {
let label = format!("$(circle-filled) {}", key.replace("terminal.ansi", ""));
entries.push(Entry::Item(Item { handle: handle as i64, label, description: Some((*key).into()), icon_color: Some((*key).into()), ..Item::default() }));
}
entries.push(Entry::Separator(None));
entries.push(Entry::Item(Item { handle: colors.len() as i64, label: "Reset to default".into(), ..Item::default() }));
let mut input = QuickInput::new(0, Kind::QuickPick);
input.match_on_description = true;
input.placeholder = Some("Select a color for the terminal".into());
input.set_items(entries);
let quick_input = self.quick_inputs.add(input, Owner::Terminal(TerminalInput::Color { instance: id, colors }));
self.show_quick_input(quick_input, window, cx);
self.flush_quick_input_events(window, cx);
}
/// terminalInstance.ts `changeIcon`: iconPicker.ts `IconPicker.pickIcons` in a quick widget titled "Change icon".
fn show_terminal_icon_picker(&mut self, id: u32, window: &mut Window, cx: &mut Context<Self>) {
let mut input = QuickInput::new(0, Kind::Widget);
input.title = Some("Change icon".into());
let quick_input = self.quick_inputs.add(input, Owner::Terminal(TerminalInput::Icon));
self.show_quick_input(quick_input, window, cx);
self.show_icon_select_box(super::icon_select_box::IconTarget::Terminal(id), window, cx);
self.flush_quick_input_events(window, cx);
}
/// `changeIcon`'s result: the picked icon, or `None` from Reset to default for the launch config's.
pub(super) fn set_terminal_icon(&mut self, id: u32, icon: Option<String>, cx: &mut Context<Self>) {
if let Some(instance) = self.terminal_view.instance_mut(id) {
instance.user_icon = icon;
}
cx.notify();
}
pub fn terminal_input_event(&mut self, id: i64, event: Event, window: &mut Window, cx: &mut Context<Self>) {
let Some(Owner::Terminal(owner)) = self.quick_inputs.owner(id) else { return };
let owner = owner.clone();
match (event, owner) {
(Event::Accept(_), TerminalInput::Profiles { profiles, set_default, in_editor }) => {
let selected = self.quick_inputs.get(id).and_then(|input| input.selected_handles().first().copied());
let picked = selected.map(|handle| profiles[handle as usize].clone());
self.hide_quick_input_by_id(id, window, cx);
match (picked, set_default) {
(Some(pick), true) => self.update_setting("terminal.integrated.defaultProfile.osx", json!(pick.name())),
(Some(ProfilePick::Shell(profile)), false) if in_editor => self.create_terminal_editor(Some(profile), super::terminal_editor::EditorTarget::Active, window, cx),
(Some(ProfilePick::Shell(profile)), false) => self.create_terminal(Some(profile), false, window, cx),
// TerminalLocation.Editor or .Panel
(Some(ProfilePick::Contributed(profile)), false) => self.create_contributed_terminal(profile, json!(if in_editor { 2 } else { 1 }), None, window, cx),
(None, _) => {}
}
}
(Event::TriggerItemButton(handle, _, _), TerminalInput::Profiles { profiles, .. }) => {
let ProfilePick::Shell(profile) = profiles[handle as usize].clone() else { return };
self.hide_quick_input_by_id(id, window, cx);
self.show_terminal_profile_name(profile, window, cx);
}
(Event::Accept(_), TerminalInput::Rename(instance)) => {
let value = self.quick_inputs.get(id).map(|input| input.input_box.value.clone()).unwrap_or_default();
self.hide_quick_input_by_id(id, window, cx);
// `if (title) instance.rename(title)`: an empty name keeps the title.
if !value.is_empty() {
self.rename_terminal(instance, value);
cx.notify();
}
}
(Event::ChangeValue(value), TerminalInput::ProfileName { existing, .. }) => {
let message = existing.contains(&value).then(|| "A terminal profile already exists with that name".to_owned());
let Some(input) = self.quick_inputs.get_mut(id) else { return };
input.severity = if message.is_some() { Severity::Error } else { Severity::Ignore };
input.validation_message = message;
cx.notify();
}
(Event::Accept(_), TerminalInput::ProfileName { profile, existing }) => {
let Some(name) = self.quick_inputs.get(id).map(|input| input.input_box.value.clone()) else { return };
if name.is_empty() || existing.contains(&name) {
return;
}
// `_createNewProfileConfig`: the path, and args and env when the profile has them.
let mut config = json!({ "path": profile.path });
if !profile.args.is_empty() {
config["args"] = json!(profile.args);
}
if !profile.env.is_empty() {
config["env"] = json!(profile.env);
}
self.hide_quick_input_by_id(id, window, cx);
let key = "terminal.integrated.profiles.osx";
let mut settings = self.settings.lock().unwrap();
let mut profiles = settings.configuration.inspect(key, &Overrides::default()).user_local.value.filter(Value::is_object).unwrap_or_else(|| json!({}));
profiles[name.as_str()] = config;
match settings.update_value(key, Some(&profiles), Target::User, None, &[]) {
Ok(change) if !change.is_empty() => self.host.send_configuration_change(&settings, &change),
Ok(_) => {}
Err(error) => eprintln!("writing {key}: {error}"),
}
}
(event, TerminalInput::RunRecent { kind, picks, fuzzy, instance }) => self.run_recent_input_event(id, event, kind, picks, fuzzy, instance, window, cx),
(Event::Accept(_), TerminalInput::Color { instance, colors }) => {
let selected = self.quick_inputs.get(id).and_then(|input| input.selected_handles().first().copied());
self.hide_quick_input_by_id(id, window, cx);
let Some(handle) = selected else { return };
// Reset to default has no `id`, so the color is unset.
let color = colors.get(handle as usize).map(|key| (*key).to_owned());
if let Some(instance) = self.terminal_view.instance_mut(instance) {
instance.launch.color = color;
}
cx.notify();
}
(Event::Hide, TerminalInput::Icon) => {
self.quick_inputs.remove(id);
self.hide_icon_select_box(window, cx);
}
(Event::Hide, _) => self.quick_inputs.remove(id),
(Event::Accept(_), TerminalInput::DecorationVisibility) => self.hide_quick_input_by_id(id, window, cx),
(event, TerminalInput::DecorationVisibility) => self.decoration_visibility_event(event),
(event, input @ (TerminalInput::SendSequence | TerminalInput::WriteData | TerminalInput::Signal | TerminalInput::ManualSignal)) => self.terminal_contrib_input_event(id, event, input, window, cx),
_ => {}
}
}
/// `onDidTriggerItemButton` of the profile picker: name a copy of the profile in the user's profiles.
fn show_terminal_profile_name(&mut self, profile: Profile, window: &mut Window, cx: &mut Context<Self>) {
let configured = self.setting("terminal.integrated.profiles.osx");
let existing: Vec<String> = configured.as_object().map(|profiles| profiles.keys().cloned().collect()).unwrap_or_default();
let mut input = QuickInput::new(0, Kind::InputBox);
input.prompt = Some("Enter terminal profile name".into());
input.set_value(profile.name.clone());
let id = self.quick_inputs.add(input, Owner::Terminal(TerminalInput::ProfileName { profile, existing }));
self.show_quick_input(id, window, cx);
self.flush_quick_input_events(window, cx);
}
/// terminalContextKey.ts keys.
pub(super) fn terminal_context(&self, window: &Window, context: &mut HashMap<String, Value>) {
let view = &self.terminal_view;
let focused = view.focus.is_focused(window);
let find_focused = view.find.focus.is_focused(window);
context.insert("terminalFindVisible".into(), json!(view.find.visible));
context.insert("terminalFindFocused".into(), json!(find_focused));
context.insert("terminalFindInputFocused".into(), json!(find_focused));
let selected = view.active_instance_id().and_then(|id| view.instance(id)).is_some_and(|instance| instance.term.has_nonempty_selection());
context.insert("terminalFocus".into(), json!(focused));
context.insert("terminalFocusInAny".into(), json!(focused || find_focused));
context.insert("terminalHasBeenCreated".into(), json!(view.has_been_created));
context.insert("terminalIsOpen".into(), json!(!view.instances.is_empty()));
context.insert("terminalCount".into(), json!(view.instances.len()));
context.insert("terminalGroupCount".into(), json!(view.groups.len()));
context.insert("terminalProcessSupported".into(), json!(true));
context.insert("terminalEditorFocus".into(), json!(focused && view.active_editor.is_some()));
context.insert("terminalEditorActive".into(), json!(self.active_terminal_editor().is_some()));
context.insert("terminalTextSelected".into(), json!(selected));
context.insert("terminalTextSelectedInFocused".into(), json!(focused && selected));
context.insert("terminalSplitPaneActive".into(), json!(focused && view.groups.get(view.active_group).is_some_and(|group| group.instances.len() > 1)));
let alt_buffer = view.active_instance_id().and_then(|id| view.instance(id)).is_some_and(|instance| instance.term.mode().contains(TermMode::ALT_SCREEN));
context.insert("terminalAltBufferActive".into(), json!(alt_buffer));
context.insert("terminal.active".into(), json!(true));
self.terminal_suggest_context(context);
if self.terminal_view_visible() {
context.insert("view".into(), json!(TERMINAL_VIEW_ID));
}
}
/// terminalInstance.ts `attachCustomKeyEventHandler` and xterm.js `_keyDown`: a chord in progress and the
/// commands to skip the shell go to the workbench, every other key the terminal turns into input. Returns
/// whether the terminal took the key.
pub(super) fn terminal_key(&mut self, keystroke: &Keystroke, resolution: &Resolution, window: &Window, cx: &mut Context<Self>) -> bool {
// The quick fix widget takes the keys while it shows, as its list has the focus upstream.
if !self.terminal_view.focus.is_focused(window) || self.action_widget_visible() {
return false;
}
// `restartBlinkAnimation` on a key: the cursor shows.
self.terminal_view.cursor_blink_off = false;
match resolution {
Resolution::MoreChordsNeeded if self.setting("terminal.integrated.allowChords") == true && keystroke.key != "escape" => return false,
Resolution::KbFound { command, .. } if self.setting("terminal.integrated.sendKeybindingsToShell") != true && (keystroke.modifiers.platform || self.skips_shell(command)) => return false,
_ => {}
}
// terminalInstance.ts: with Tab Focus Mode on, Tab and Shift+Tab move the focus as the browser's Tab order does.
if keystroke.key == "tab" && self.tab_focus_mode() {
return false;
}
let mac_option_is_meta = self.setting("terminal.integrated.macOptionIsMeta") == true;
let Some(instance) = self.terminal_view.active_instance_mut() else { return false };
let application_cursor = instance.term.mode().contains(TermMode::APP_CURSOR);
match keys::evaluate(keystroke, application_cursor, mac_option_is_meta) {
// A task terminal whose process ended closes on any key (`waitOnExit`).
KeyResult::Send(_) if instance.waiting_on_exit => {
let id = instance.id;
cx.defer_in(window, move |workbench, window, cx| workbench.dispose_terminal(id, EXIT_REASON_USER, window, cx));
}
KeyResult::Send(data) => {
instance.input(&data);
let id = instance.id;
self.terminal_view.initial_hint_dismissed.insert(id);
self.terminal_view.suggest.user_data(&data);
self.terminal_view.command_guide = None;
}
KeyResult::PageUp => instance.scroll(Scroll::PageUp),
KeyResult::PageDown => instance.scroll(Scroll::PageDown),
KeyResult::SelectAll => instance.select_all(),
KeyResult::None => return false,
}
self.sync_terminals_to_host();
cx.notify();
true
}
/// `_skipTerminalCommands`: the defaults without the `-` entries of `terminal.integrated.commandsToSkipShell`,
/// plus its other entries.
fn skips_shell(&self, command: &str) -> bool {
let configured = self.setting("terminal.integrated.commandsToSkipShell");
let configured: Vec<&str> = configured.as_array().into_iter().flatten().filter_map(Value::as_str).collect();
if configured.contains(&command) {
return true;
}
DEFAULT_COMMANDS_TO_SKIP_SHELL.contains(&command) && !configured.iter().any(|entry| entry.strip_prefix('-') == Some(command))
}
/// The view's title actions in the panel title (terminalMenus.ts `MenuId.ViewTitle`): the active terminal while
/// the tabs list is hidden, New with the profile dropdown, Split and Kill while the tabs list is hidden.
pub(super) fn terminal_title_actions(&self, cx: &mut Context<Self>) -> Div {
// terminalMenus.ts: the active terminal entry and the inline Split and Kill actions follow
// `tabs.showActiveTerminal` and `tabs.showActions` (`shouldShowViewInlineActions`) over the group count.
let tabs_enabled = self.setting("terminal.integrated.tabs.enabled") == true;
let single_group = self.terminal_view.groups.len() == 1;
let shown_for = |key: &str| match self.setting(key).as_str() {
Some("always") => true,
Some("singleTerminal" | "singleTerminalOrNarrow" | "singleGroup") => single_group,
_ => false,
};
let show_active_terminal = tabs_enabled && shown_for("terminal.integrated.tabs.showActiveTerminal");
let show_inline_actions = self.setting("terminal.integrated.tabs.hideCondition") != "never" && (!tabs_enabled || shown_for("terminal.integrated.tabs.showActions"));
let active = self.terminal_view.group_active_instance_id().and_then(|id| self.terminal_view.instance(id));
let foreground = self.foreground_or("icon.foreground");
// `getSingleTabLabel`: `$(icon) title`, then the primary status's icon; the label takes its severity's color. The
// leading icon keeps `icon.foreground` (style.css `.monaco-workbench .codicon`), the status icon inherits.
let active_title = active.map(|instance| {
let status = instance.primary_status();
let color = status.and_then(|status| self.terminal_status_color(status.severity));
let text_color = color.unwrap_or(self.foreground_or("foreground"));
div()
.flex()
.items_center()
.gap(px(4.))
.text_color(text_color)
.child(self.terminal_instance_icon(instance, foreground))
.child(self.single_tab_title(instance))
.children(status.and_then(|status| status.icon).map(|icon| self.terminal_status_icon(instance.id, icon, color.unwrap_or(foreground))))
});
div()
.flex()
.items_center()
.gap(px(4.))
.when_some(active_title.filter(|_| show_active_terminal), |actions, title| {
actions.child(self.action_bar_item(
TERMINAL_VIEW_ID,
"single-tab",
div()
.id("terminal-single-tab")
.h(px(22.))
.px(px(4.))
.flex()
.items_center()
.gap(px(4.))
.rounded(px(4.))
.text_size(px(11.))
.cursor_pointer()
.text_color(self.foreground_or("foreground"))
.hover(|tab| tab.bg(self.color("toolbar.hoverBackground")))
.child(title)
.on_click(cx.listener(|workbench, _, window, cx| {
workbench.execute_command("workbench.action.terminal.focus", Vec::new(), window, cx);
})),
cx,
))
})
// dropdown.css `.monaco-dropdown-with-primary`: New Terminal and its 12px profile chevron side by side. The width is
// fixed (22 + 16 - 3): taffy sizes a row with a negative-margin child to 0.
.child(div().w(px(35.)).flex_none().flex().items_center().child(self.action_bar_item(TERMINAL_VIEW_ID, "new", self.search_action("add", "workbench.action.terminal.new", true, cx).debug_selector(|| "terminal-new".into()), cx)).child(self.action_bar_item(TERMINAL_VIEW_ID, "new-profile", self.terminal_profile_chevron(foreground, Vec::new(), cx).debug_selector(|| "terminal-new-profile".into()), cx)))
.when(show_inline_actions, |actions| {
let split = self.turned_action("split-horizontal", self.panel_title_icon_turns(), "workbench.action.terminal.split", true, cx).debug_selector(|| "terminal-split".into());
actions.child(self.action_bar_item(TERMINAL_VIEW_ID, "split", split, cx)).child(self.action_bar_item(TERMINAL_VIEW_ID, "kill", self.search_action("trash", "workbench.action.terminal.kill", true, cx), cx))
})
.child(self.action_bar_item(TERMINAL_VIEW_ID, "more", self.terminal_more_actions(foreground, cx).debug_selector(|| "terminal-more".into()), cx))
}
/// terminalIcon.ts: the instance's codicon (`terminal` without one) in its `color` (`getColorClass`).
pub(super) fn terminal_instance_icon(&self, instance: &TerminalInstance, foreground: gpui::Rgba) -> Div {
self.icon(instance.icon(), 16., 16., self.terminal_icon_color(instance, foreground))
}
/// terminalIcon.ts `getColorClass`: the tab color, else the label's.
pub(super) fn terminal_icon_color(&self, instance: &TerminalInstance, foreground: gpui::Rgba) -> gpui::Rgba {
instance.launch.color.as_deref().and_then(|color| self.theme.color(color)).unwrap_or(foreground)
}
/// The primary status's codicon; `~spin` turns as codicon-animations.css `codicon-modifier-spin` does (1.5s, 30 steps).
fn terminal_status_icon(&self, id: u32, icon: &str, color: gpui::Rgba) -> gpui::AnyElement {
let Some(name) = icon.strip_suffix("~spin") else { return self.icon(icon, 16., 16., color).into_any_element() };
use gpui::AnimationExt;
let path = crate::codicons::svg_path(&self.app_root, name);
let icon = gpui::svg().external_path(path.to_string_lossy().into_owned()).size(px(16.)).flex_none().text_color(color);
let spin = icon.with_animation(("terminal-status-spin", id as usize), gpui::Animation::new(Duration::from_millis(1500)).repeat(), |icon, delta| {
icon.with_transformation(gpui::Transformation::rotate(gpui::percentage((delta * 30.).floor() / 30.)))
});
div().size(px(16.)).flex_none().flex().items_center().justify_center().child(spin).into_any_element()
}
/// terminalStatusList.ts `getColorForSeverity`: errors and warnings in the list colors.
fn terminal_status_color(&self, severity: u8) -> Option<gpui::Rgba> {
match severity {
3 => self.theme.color("list.errorForeground"),
2 => self.theme.color("list.warningForeground"),
_ => None,
}
}
/// terminalView.ts `getSingleTabTitle`: `<title> <separator> <description>`, its whitespace collapsed as HTML does.
fn single_tab_title(&self, instance: &TerminalInstance) -> String {
let description = self.terminal_description(instance);
if description.is_empty() {
return instance.title();
}
let separator = self.setting("terminal.integrated.tabs.separator");
let title = format!("{} {} {description}", instance.title(), separator.as_str().unwrap_or(" - "));
title.split_whitespace().collect::<Vec<_>>().join(" ")
}
/// `TerminalInstance.description`, `terminal.integrated.tabs.description` resolved.
pub(super) fn terminal_description(&self, instance: &TerminalInstance) -> String {
let template = self.setting("terminal.integrated.tabs.description");
let separator = self.setting("terminal.integrated.tabs.separator");
let workspace = self.workspace();
let launch_folder = workspace.and_then(|workspace| workspace.folders.iter().filter(|folder| instance.launch.cwd.starts_with(&folder.path)).max_by_key(|folder| folder.path.as_os_str().len()));
let workspace_folder = launch_folder.map(|folder| folder.path.clone()).or_else(|| self.last_active_workspace_root().map(std::path::PathBuf::from));
let user_home = std::env::var("HOME").unwrap_or_default();
let input = crate::terminal::label::LabelInput {
process: &instance.process_title,
sequence: instance.sequence_title.as_deref(),
cwd: instance.cwd.as_deref(),
user_home: &user_home,
workspace_folder: workspace_folder.as_deref(),
multi_root: workspace.is_some_and(|workspace| workspace.folders.len() > 1),
task: instance.launch.is_feature_terminal,
shell_type: instance.shell_type,
};
crate::terminal::label::description(template.as_str().unwrap_or_default(), separator.as_str().unwrap_or(" - "), &input)
}
pub(super) fn terminal_profile_chevron(&self, foreground: gpui::Rgba, args: Vec<Value>, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let hover = self.color("toolbar.hoverBackground");
// dropdown.css: the 16x22 chevron overlaps the primary action by 3px (`margin-left: -3px`).
self.icon("chevron-down", 16., 12., foreground).h(px(22.)).ml(px(-3.)).id("workbench.action.terminal.newWithProfile").rounded(px(4.)).cursor_pointer().hover(|action| action.bg(hover)).on_click(cx.listener(move |workbench, _: &gpui::ClickEvent, window, cx| {
cx.stop_propagation();
workbench.execute_command("workbench.action.terminal.newWithProfile", args.clone(), window, cx);
}))
}
/// The title's "...": terminalMenus.ts's `isHiddenByDefault` ViewTitle items (Start Dictation left out, speech is
/// removed), then terminal.history.contribution.ts's `shellIntegration` group.
fn terminal_more_actions(&self, foreground: gpui::Rgba, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let hover = self.color("toolbar.hoverBackground");
self.icon("toolbar-more", 22., 16., foreground)
.id("terminal-more-actions")
.rounded(px(4.))
.cursor_pointer()
.hover(|action| action.bg(hover))
.child(super::output_view::bounds_tracker(self.terminal_view.more_actions_bounds.clone()))
.on_mouse_down(gpui::MouseButton::Left, cx.listener(|workbench, _: &gpui::MouseDownEvent, window, cx| {
cx.stop_propagation();
workbench.show_terminal_more_actions(window, cx);
}))
// Enter and Space on the focused item: gpui's keyboard click.
.on_click(cx.listener(|workbench, event: &gpui::ClickEvent, window, cx| {
if event.is_keyboard() {
workbench.show_terminal_more_actions(window, cx);
}
}))
.child(self.hover_target("terminal-more-actions-hover", super::hover::HoverContent::Text("Views and More Actions...".into()), super::hover::HoverOptions::action_item()))
}
fn show_terminal_more_actions(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let command = |id: &str, title: &str| MenuAction::Command { id: format!("workbench.action.terminal.{id}"), title: title.into(), enabled: true, checked: false, alt: None };
let actions = vec![
command("scrollToPreviousCommand", "Scroll to Previous Command"),
command("scrollToNextCommand", "Scroll to Next Command"),
command("clear", "Clear Terminal"),
command("runActiveFile", "Run Active File"),
command("runSelectedText", "Run Selected Text"),
MenuAction::Separator,
command("goToRecentDirectory", "Go to Recent Directory..."),
command("runRecentCommand", "Run Recent Command..."),
];
let anchor = self.terminal_view.more_actions_bounds.get();
self.show_context_menu_right_aligned(actions, anchor, window, cx);
}
/// terminalActions.ts RunSelectedText: the selection, or the trimmed line of an empty one, run in the active
/// terminal (bracketed when the shell asked for it), which shows without the focus.
fn run_selected_text_in_terminal(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some(text) = self.editors.active().map(|editor| {
let (selection, lines) = (editor.selection(), &editor.document().lines);
match selection.is_empty() {
true => lines[selection.anchor.line].trim().to_owned(),
false => crate::editor::text_between(lines, selection.start(), selection.end()),
}
}) else {
return;
};
let id = self.active_or_create_terminal(window, cx);
let instance = self.terminal_view.instance_mut(id).expect("just made active");
let text = match instance.term.mode().contains(ghostty_vt::TermMode::BRACKETED_PASTE) {
true => format!("\x1b[200~{text}\x1b[201~"),
false => text,
};
instance.send_text(&text, true);
self.open_panel_view(TERMINAL_VIEW_ID, cx);
}
/// terminalActions.ts RunActiveFile: the active file's path, quoted for the shell, run in the active terminal.
fn run_active_file_in_terminal(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some((on_disk, path)) = self.editors.active().map(|editor| {
let document = editor.document();
(matches!(document.scheme.as_str(), "file" | "vscode-userdata"), document.path.to_string_lossy().into_owned())
}) else {
return;
};
if !on_disk {
return self.notify_warning("Only files on disk can be run in the terminal");
}
let id = self.active_or_create_terminal(window, cx);
let instance = self.terminal_view.instance_mut(id).expect("just made active");
let path = instance.prepare_path_for_shell(&path);
instance.send_text(&path, true);
self.open_panel_view(TERMINAL_VIEW_ID, cx);
}
/// terminalService.ts `getActiveOrCreateInstance`: a new terminal of the default profile when none is active.
fn active_or_create_terminal(&mut self, window: &mut Window, cx: &mut Context<Self>) -> u32 {
if let Some(id) = self.terminal_view.active_instance_id() {
return id;
}
let launch = self.shell_launch_config(&self.default_terminal_profile(), self.terminal_initial_cwd());
let id = self.add_terminal(launch, None, window, cx);
self.sync_terminals_to_host();
id
}
fn terminal_tabs_shown(&self) -> bool {
let enabled = self.setting("terminal.integrated.tabs.enabled") == true;
let hide = self.setting("terminal.integrated.tabs.hideCondition");
self.terminal_view.should_show_tabs(enabled, hide.as_str().unwrap_or("singleTerminal"))
}
/// The view below its title: the active group's panes and, at `terminal.integrated.tabs.location`, the tabs list.
pub(super) fn terminal_body(&self, window: &Window, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let view = &self.terminal_view;
let background = self.theme.color("terminal.background").unwrap_or(self.color("panel.background"));
let border = self.color("terminal.border");
let mut panes = div().relative().flex_1().min_w_0().h_full().flex();
if let Some(group) = view.groups.get(view.active_group) {
for (index, &id) in group.instances.iter().enumerate() {
panes = panes.child(self.terminal_pane(id, index > 0, border, cx));
}
}
if view.active_editor.is_none() {
panes = panes.children(self.terminal_find_widget(window, cx)).children(self.terminal_suggest_widget(window, cx));
}
let tabs_left = self.setting("terminal.integrated.tabs.location") == "left";
div()
.id("terminal-view")
.track_focus(&view.focus)
.flex_1()
.min_h_0()
.flex()
.bg(background)
.when(tabs_left, |body| body.flex_row_reverse())
.child(panes)
.when(self.terminal_tabs_shown(), |body| body.child(self.terminal_tabs_list(tabs_left, cx)))
}
pub(super) fn terminal_pane(&self, id: u32, border_left: bool, border: gpui::Rgba, cx: &mut Context<Self>) -> impl IntoElement {
let entity = cx.entity();
div()
.id(("terminal-pane", id as usize))
.flex_1()
.min_w_0()
.h_full()
.relative()
.cursor_text()
.when(border_left, |pane| pane.border_l_1().border_color(border))
.child(canvas(|_, _, _| {}, move |bounds, _, window, cx| entity.update(cx, |workbench, cx| workbench.paint_terminal_pane(id, bounds, window, cx))).size_full())
.children(self.terminal_sticky_scroll_hit_area(id, cx))
.children(self.terminal_resize_overlay(id))
.children(self.terminal_command_guide(id))
.children(self.terminal_navigation_highlight(id))
.children(self.terminal_decorations(id, cx))
.children(self.terminal_initial_hint(id, cx))
.on_hover(cx.listener(move |workbench, hovered: &bool, _, cx| {
workbench.terminal_view.hovered = hovered.then_some(id);
if !hovered {
workbench.terminal_view.command_guide = None;
}
cx.notify();
}))
.on_mouse_down(MouseButton::Left, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| workbench.terminal_mouse_down(id, event, window, cx)))
.on_mouse_down(MouseButton::Right, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| workbench.terminal_mouse_down(id, event, window, cx)))
.on_mouse_down(MouseButton::Middle, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| workbench.terminal_mouse_down(id, event, window, cx)))
.on_mouse_move(cx.listener(move |workbench, event: &MouseMoveEvent, window, cx| workbench.terminal_mouse_move(id, event, window, cx)))
.on_mouse_up(MouseButton::Left, cx.listener(move |workbench, event: &MouseUpEvent, _, cx| workbench.terminal_mouse_up(id, event, cx)))
.on_scroll_wheel(cx.listener(move |workbench, event: &ScrollWheelEvent, _, cx| workbench.terminal_wheel(id, event, cx)))
}
/// Lays out and paints one pane: the instance takes the columns and rows its pane fits (`_evaluateColsAndRows`).
fn paint_terminal_pane(&mut self, id: u32, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
self.terminal_metrics(window);
let metrics = self.terminal_view.pane_metrics(id).expect("measured above");
let scrollbar_width = self.terminal_scrollbar_width();
let sticky_look = self.terminal_sticky_look(id);
let colors = if self.terminal_view.editor_instances.contains(&id) { TerminalColors::in_editor(&self.theme) } else { TerminalColors::new(&self.theme) };
let cursor_inactive_style = self.setting("terminal.integrated.cursorStyleInactive");
let find_matches = if self.terminal_view.active_instance_id() == Some(id) { self.terminal_view.find.matches.clone() } else { Vec::new() };
let focused = self.terminal_view.focus.is_focused(window) && self.terminal_view.active_instance_id() == Some(id);
// xterm `isCursorInitialized`: no cursor until the terminal was focused, typed into or switched screens.
let cursor_initialized = self.terminal_view.instance_mut(id).is_some_and(|instance| {
instance.cursor_initialized |= focused || instance.term.mode().contains(TermMode::ALT_SCREEN);
instance.cursor_initialized
});
if focused {
// `cursorBlinking` is the default style's; a program's DECSCUSR overrides it.
self.terminal_view.cursor_blinking = self.terminal_view.instance(id).is_some_and(|instance| instance.term.cursor_style().blinking);
}
let link_underline = self.terminal_link_underline(id).map(<[_]>::to_vec).unwrap_or_default();
let overview_ruler = self.terminal_overview_ruler(id);
let look = Look {
colors: &colors,
metrics: &metrics,
focused,
cursor_initialized,
cursor_blink_off: self.terminal_view.cursor_blinking && self.terminal_view.cursor_blink_off,
cursor_inactive_style: cursor_inactive_style.as_str().unwrap_or("outline"),
cursor_width: self.setting("terminal.integrated.cursorWidth").as_f64().unwrap_or(1.) as f32,
minimum_contrast_ratio: self.setting("terminal.integrated.minimumContrastRatio").as_f64().unwrap_or(4.5),
draw_bold_text_in_bright_colors: self.setting("terminal.integrated.drawBoldTextInBrightColors") == true,
scrollbar_width,
hovered: self.terminal_view.hovered == Some(id),
scrollbar_slider: self.color("scrollbarSlider.background"),
overview_ruler,
link_underline: &link_underline,
find_matches: &find_matches,
find_current: self.terminal_view.find.current,
find_match: self.color("terminal.findMatchBackground"),
find_highlight: self.color("terminal.findMatchHighlightBackground"),
};
let fixed_cols = self.terminal_view.instance(id).and_then(|instance| instance.fixed_cols);
// `_evaluateColsAndRows`: a fixed width leaves 5px for its horizontal scroll bar.
let scroll_bar_height = if fixed_cols.is_some() { 5. } else { 0. };
let detached = self.terminal_view.detached.iter().any(|instance| instance.id == id);
let padded = self.setting("workbench.experimental.modernUI") == true && !self.terminal_view.editor_instances.contains(&id) && !detached;
let padding_block = if padded { grid::PADDING_BLOCK } else { 0. };
let pane_height = f32::from(bounds.size.height);
let (fitted_cols, rows) = metrics.grid_size(f32::from(bounds.size.width) - GUTTER - scrollbar_width, pane_height - 2. * padding_block - scroll_bar_height);
// terminal.css bottom aligns `.xterm`; a `.fixed-dims` one is static and a modernUI panel one sits at `top: 0`.
// A detached terminal's `.xterm` sits at the top of the element it attaches to.
let grid_top = if padded || fixed_cols.is_some() || detached { padding_block } else { pane_height - rows as f32 * metrics.cell_height };
let cols = fixed_cols.unwrap_or(fitted_cols);
let config = self.terminal_config();
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
// `_addScrollbar`: the xterm element as wide as its columns, scrolled inside the pane.
let xterm_bounds = match fixed_cols {
Some(fixed) => {
let width = fixed as f32 * metrics.cell_width + GUTTER + scrollbar_width;
let scroll_width = fixed as f32 * metrics.cell_width + 40.;
instance.scroll_x = instance.scroll_x.clamp(0., (scroll_width - f32::from(bounds.size.width)).max(0.));
Bounds::new(gpui::point(bounds.origin.x - px(instance.scroll_x), bounds.origin.y), gpui::size(px(width), bounds.size.height))
}
None => bounds,
};
instance.cell_size = (metrics.cell_width, metrics.cell_height);
instance.grid_top = grid_top;
let resized = instance.resize(cols, rows);
instance.configure(config);
self.terminal_view.pane_bounds.insert(id, xterm_bounds);
let sticky = self.terminal_sticky_scroll(id);
let instance = self.terminal_view.instance(id).expect("resized above");
window.with_content_mask(Some(gpui::ContentMask { bounds }), |window| {
grid::paint(xterm_bounds, instance, &look, window);
if let Some(sticky) = &sticky {
grid::paint_sticky_scroll(xterm_bounds, instance, sticky, &sticky_look, &look, window);
}
if look.hovered && fixed_cols.is_some() {
grid::paint_horizontal_scrollbar(bounds, xterm_bounds, &look, window);
}
});
if resized && !detached {
self.terminal_resized(id);
self.show_resize_overlay(id, cx);
}
}
pub(super) fn terminal_scrollbar_width(&self) -> f32 {
if self.setting("workbench.experimental.modernUI") == true { SCROLLBAR_WIDTH_MODERN } else { SCROLLBAR_WIDTH }
}
/// The xterm.js options the settings set: `scrollback`, `wordSeparator` and `cursorStyle`, which a program's
/// DECSCUSR overrides.
pub(super) fn terminal_config(&self) -> Config {
let shape = match self.setting("terminal.integrated.cursorStyle").as_str() {
Some("line") => CursorShape::Beam,
Some("underline") => CursorShape::Underline,
_ => CursorShape::Block,
};
Config {
scrolling_history: self.setting("terminal.integrated.scrollback").as_u64().unwrap_or(1000) as usize,
semantic_escape_chars: self.setting("terminal.integrated.wordSeparators").as_str().unwrap_or_default().to_owned(),
default_cursor_style: CursorStyle { shape, blinking: self.setting("terminal.integrated.cursorBlinking") == true },
}
}
/// `TerminalConfigurationService.getFont`: `terminal.integrated.fontFamily`, else the editor's font.
fn terminal_metrics(&mut self, window: &Window) -> CellMetrics {
let family = self.setting("terminal.integrated.fontFamily");
let family = family.as_str().and_then(|family| family.split(',').next()).map(|family| family.trim().trim_matches(['\'', '"']).to_owned()).filter(|family| !family.is_empty()).unwrap_or_else(|| "Menlo".into());
let font_size = self.setting("terminal.integrated.fontSize").as_f64().unwrap_or(12.) as f32;
let line_height = self.setting("terminal.integrated.lineHeight").as_f64().unwrap_or(1.) as f32;
let letter_spacing = self.setting("terminal.integrated.letterSpacing").as_f64().unwrap_or(0.) as f32;
let weight = |key: &str, fallback: FontWeight| match self.setting(key) {
Value::String(name) if name == "normal" => FontWeight::NORMAL,
Value::String(name) if name == "bold" => FontWeight::BOLD,
Value::String(number) => number.parse().map(FontWeight).unwrap_or(fallback),
Value::Number(number) => number.as_f64().map_or(fallback, |weight| FontWeight(weight as f32)),
_ => fallback,
};
let (normal, bold) = (weight("terminal.integrated.fontWeight", FontWeight::NORMAL), weight("terminal.integrated.fontWeightBold", FontWeight::BOLD));
let family = SharedString::from(family);
let current = self.terminal_view.metrics.as_ref();
let stale = current.is_none_or(|metrics| metrics.family != family || metrics.font_size != font_size || metrics.line_height != line_height || metrics.letter_spacing != letter_spacing || metrics.normal_weight != normal || metrics.bold_weight != bold || metrics.scale != window.scale_factor());
if stale {
self.terminal_view.metrics = Some(CellMetrics::measure(window, family, font_size, line_height, letter_spacing, normal, bold));
}
self.terminal_view.metrics.clone().expect("measured above")
}
/// terminalContextMenu.ts `openContextMenu` after `rightClickBehavior`: selectWord (the macOS default) selects
/// the word under the pointer when nothing is selected, copyPaste copies a selection or pastes, paste pastes.
#[allow(clippy::too_many_arguments)]
fn terminal_right_click(&mut self, id: u32, row: i32, column: usize, side: Side, position: Point<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
let behavior = self.setting("terminal.integrated.rightClickBehavior");
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
let selection = instance.selection_text();
match behavior.as_str().unwrap_or("selectWord") {
"copyPaste" | "paste" => {
match selection.filter(|_| behavior == "copyPaste") {
Some(text) => {
cx.write_to_clipboard(ClipboardItem::new_string(text));
instance.term.clear_selection();
}
None => {
if let Some(text) = cx.read_from_clipboard().and_then(|item| item.text()) {
instance.paste(&text);
}
}
}
self.sync_terminals_to_host();
cx.notify();
return;
}
"selectWord" if selection.is_none() => instance.start_selection(SelectionType::Semantic, row, column, side),
"nothing" => return,
_ => {}
}
self.sync_terminals_to_host();
let context = self.key_context(window);
let anchor = Bounds { origin: position, size: Size::default() };
// terminalEditor.ts shows its own menu for an editor's terminal.
let menu = if self.terminal_view.editor_instances.contains(&id) { MenuId::TerminalEditorInstanceContext } else { MenuId::TerminalInstanceContext };
self.show_context_menu(menu, &context, anchor, Vec::new(), window, cx);
}
/// terminalTabbedView.ts `_onContextMenu`: the right-clicked row is selected, then the TerminalTabContext menu opens.
fn terminal_tab_right_click(&mut self, id: u32, position: Point<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
cx.stop_propagation();
self.terminal_view.set_active(id);
self.sync_terminals_to_host();
let context = self.key_context(window);
let anchor = Bounds { origin: position, size: Size::default() };
self.show_context_menu(MenuId::TerminalTabContext, &context, anchor, Vec::new(), window, cx);
}
fn terminal_mouse_down(&mut self, id: u32, event: &MouseDownEvent, window: &mut Window, cx: &mut Context<Self>) {
self.terminal_view.set_active(id);
// terminalSuggestAddon.ts: a click in the terminal hides the widget.
self.hide_terminal_suggest(false);
window.focus(&self.terminal_view.focus, cx);
cx.stop_propagation();
let force_selection = event.modifiers.alt && self.setting("terminal.integrated.macOptionClickForcesSelection") == true;
let (Some(bounds), Some(metrics)) = (self.terminal_view.pane_bounds.get(&id).copied(), self.terminal_view.pane_metrics(id)) else { return };
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
let (row, column, side) = grid::cell_at(bounds, event.position, instance, &metrics);
let link_modifier = self.terminal_link_modifier(event.modifiers);
if event.button == MouseButton::Left && link_modifier && self.open_terminal_link(id, row, column, window, cx) {
return;
}
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
let mode = instance.term.mode();
let button = match event.button {
MouseButton::Left => mouse::MouseButton::Left,
MouseButton::Middle => mouse::MouseButton::Middle,
_ => mouse::MouseButton::Right,
};
if !force_selection && mouse::wants(mode, button, MouseAction::Down) {
if let Some(report) = mouse::report(mode, button, MouseAction::Down, column, row.max(0) as usize, event.modifiers.shift, event.modifiers.alt, event.modifiers.control) {
instance.input(&report);
}
cx.notify();
return;
}
if event.button == MouseButton::Right {
return self.terminal_right_click(id, row, column, side, event.position, window, cx);
}
let kind = match (event.button, event.click_count) {
(MouseButton::Left, 2) => SelectionType::Semantic,
(MouseButton::Left, 3..) => SelectionType::Lines,
(MouseButton::Left, _) => SelectionType::Simple,
_ => return,
};
if event.button == MouseButton::Left && event.click_count == 1 && event.modifiers.shift && instance.term.has_selection() {
instance.extend_selection(row, column, side);
} else {
instance.start_selection(kind, row, column, side);
}
if event.button == MouseButton::Left {
self.terminal_view.selecting = Some(id);
}
self.sync_terminals_to_host();
cx.notify();
}
fn terminal_mouse_move(&mut self, id: u32, event: &MouseMoveEvent, window: &mut Window, cx: &mut Context<Self>) {
let (Some(bounds), Some(metrics)) = (self.terminal_view.pane_bounds.get(&id).copied(), self.terminal_view.pane_metrics(id)) else { return };
let selecting = self.terminal_view.selecting == Some(id) && event.pressed_button == Some(MouseButton::Left);
let show_command_guide = self.setting("terminal.integrated.shellIntegration.showCommandGuide") != false;
let Some(instance) = self.terminal_view.instance(id) else { return };
let (row, column, side) = grid::cell_at(bounds, event.position, instance, &metrics);
let guide = show_command_guide.then(|| instance.command_guide_rows(instance.viewport_y() + row as i64)).flatten().map(|rows| (id, rows));
if guide != self.terminal_view.command_guide {
self.terminal_view.command_guide = guide;
cx.notify();
}
if event.pressed_button.is_none() {
self.terminal_link_hover(id, row, column, event.modifiers, window, cx);
}
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
if selecting {
instance.extend_selection(row, column, side);
return cx.notify();
}
let mode = instance.term.mode();
let button = match event.pressed_button {
Some(MouseButton::Left) => mouse::MouseButton::Left,
Some(MouseButton::Middle) => mouse::MouseButton::Middle,
Some(MouseButton::Right) => mouse::MouseButton::Right,
_ => mouse::MouseButton::None,
};
if mouse::wants(mode, button, MouseAction::Move) && (0..instance.term.screen_lines() as i32).contains(&row) {
if let Some(report) = mouse::report(mode, button, MouseAction::Move, column, row as usize, event.modifiers.shift, event.modifiers.alt, event.modifiers.control) {
instance.input(&report);
}
}
}
fn terminal_mouse_up(&mut self, id: u32, event: &MouseUpEvent, cx: &mut Context<Self>) {
let was_selecting = self.terminal_view.selecting.take() == Some(id);
let copy_on_selection = self.setting("terminal.integrated.copyOnSelection") == true;
let (Some(bounds), Some(metrics)) = (self.terminal_view.pane_bounds.get(&id).copied(), self.terminal_view.pane_metrics(id)) else { return };
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
if was_selecting {
if copy_on_selection && let Some(text) = instance.selection_text() {
cx.write_to_clipboard(ClipboardItem::new_string(text));
}
return self.sync_terminals_to_host();
}
let mode = instance.term.mode();
let (row, column, _) = grid::cell_at(bounds, event.position, instance, &metrics);
if mouse::wants(mode, mouse::MouseButton::Left, MouseAction::Up) && let Some(report) = mouse::report(mode, mouse::MouseButton::Left, MouseAction::Up, column, row.max(0) as usize, event.modifiers.shift, event.modifiers.alt, event.modifiers.control) {
instance.input(&report);
}
}
/// xterm.js `Viewport` wheel handling: a program that asked for the mouse gets wheel reports; in the alternate
/// buffer the wheel sends arrow keys; otherwise the scrollback scrolls by `mouseWheelScrollSensitivity` lines,
/// `fastScrollSensitivity` with Alt.
fn terminal_wheel(&mut self, id: u32, event: &ScrollWheelEvent, cx: &mut Context<Self>) {
cx.stop_propagation();
let modifiers = event.modifiers;
if (modifiers.platform || modifiers.control) && !modifiers.shift && !modifiers.alt && self.setting("terminal.integrated.mouseWheelZoom") == true {
self.terminal_wheel_zoom(event.delta);
return;
}
let Some(metrics) = self.terminal_view.metrics.clone() else { return };
if let Some(instance) = self.terminal_view.instance_mut(id).filter(|instance| instance.fixed_cols.is_some()) {
// `scrollYToX: false`: only a horizontal delta scrolls the fixed width xterm; painting clamps it.
instance.scroll_x -= f32::from(event.delta.pixel_delta(px(metrics.cell_height)).x);
cx.notify();
}
let units = match event.delta {
ScrollDelta::Pixels(delta) => f32::from(delta.y) / 40.,
ScrollDelta::Lines(delta) => delta.y,
};
let key = if event.modifiers.alt { "terminal.integrated.fastScrollSensitivity" } else { "terminal.integrated.mouseWheelScrollSensitivity" };
let sensitivity = self.setting(key).as_f64().unwrap_or(1.) as f32;
let lines = self.terminal_view.scroll_remainder + units * SCROLL_WHEEL_SENSITIVITY * sensitivity / metrics.cell_height;
let whole = lines.trunc();
self.terminal_view.scroll_remainder = lines - whole;
if whole == 0. {
return;
}
let bounds = self.terminal_view.pane_bounds.get(&id).copied();
let Some(instance) = self.terminal_view.instance_mut(id) else { return };
let mode = instance.term.mode();
let up = whole > 0.;
let count = whole.abs() as usize;
if mode.intersects(TermMode::MOUSE_MODE) {
let (row, column, _) = bounds.map_or((0, 0, Side::Left), |bounds| grid::cell_at(bounds, event.position, instance, &metrics));
let button = if up { mouse::MouseButton::WheelUp } else { mouse::MouseButton::WheelDown };
if let Some(report) = mouse::report(mode, button, MouseAction::Down, column, row.max(0) as usize, event.modifiers.shift, event.modifiers.alt, event.modifiers.control) {
instance.input(&report.repeat(count));
}
} else if mode.contains(TermMode::ALT_SCREEN) && mode.contains(TermMode::ALTERNATE_SCROLL) {
let prefix = if mode.contains(TermMode::APP_CURSOR) { "\x1bO" } else { "\x1b[" };
instance.input(&format!("{prefix}{}", if up { 'A' } else { 'B' }).repeat(count));
} else {
instance.scroll(Scroll::Delta(if up { count as i32 } else { -(count as i32) }));
}
cx.notify();
}
/// terminalTabsList.ts: one 22px row per terminal, `┌ ├ └` before the panes of a split group, the inline
/// Split and Kill actions on hover once the list is wide enough.
fn terminal_tabs_list(&self, at_left: bool, cx: &mut Context<Self>) -> impl IntoElement {
let view = &self.terminal_view;
let foreground = self.foreground_or("foreground");
let active_id = view.group_active_instance_id();
let mut list = div().id("terminal-tabs").debug_selector(|| "terminal-tabs".into()).w(px(TABS_DEFAULT_WIDTH)).h_full().flex_none().flex().flex_col().overflow_y_scroll().border_color(self.color("panel.border"));
list = if at_left { list.border_r_1() } else { list.border_l_1() };
for group in &view.groups {
for (index, &id) in group.instances.iter().enumerate() {
let Some(instance) = view.instance(id) else { continue };
let prefix = match (group.instances.len(), index) {
(1, _) => "",
(_, 0) => "┌",
(count, index) if index + 1 == count => "└",
_ => "├",
};
let active = Some(id) == active_id;
let description = self.terminal_description(instance);
let row = div()
.id(("terminal-tab", id as usize))
.group("terminal-tab")
.relative()
.debug_selector(move || format!("terminal-tab-{id}"))
.h(px(TAB_HEIGHT))
.flex_none()
.flex()
.items_center()
.mx(px(self.pane_row_inset()))
.rounded(px(4.))
// modernUI tabs.css: a .terminal-tabs-entry with text is padded 8px, not terminal.css's 10px.
.px(px(8.))
.text_size(px(13.))
.cursor_pointer()
.text_color(foreground)
.when(active, |row| row.bg(self.color("list.inactiveSelectionBackground")).text_color(self.foreground_or("list.inactiveSelectionForeground")))
.when(!active, |row| row.hover(|row| row.bg(self.color("list.hoverBackground"))))
// `.is-active::before`, cut by the row's rounded corners.
.when(active, |row| row.child(div().absolute().inset_0().rounded(px(4.)).border_l_1().border_color(self.color("terminal.tab.activeBorder"))))
// terminalTabsList.ts labels the tab `${prefix}$(icon) title`, one line of text: spaces, not a gap, around the icon.
.when(!prefix.is_empty(), |row| row.child(div().debug_selector(|| "terminal-tab-prefix".into()).flex_none().whitespace_nowrap().child(format!("{prefix} "))))
.child(self.terminal_instance_icon(instance, foreground).debug_selector(|| "terminal-tab-icon".into()))
.child(
// iconlabel.css `.label-description`: margin-left .5em, opacity .7, font-size .9em.
div().flex_1().min_w_0().flex().items_center().overflow_hidden().whitespace_nowrap().child(div().debug_selector(move || format!("terminal-tab-title-{id}")).min_w_0().overflow_hidden().text_ellipsis().child(format!(" {}", instance.title()))).when(!description.is_empty(), |label| {
label.child(super::icon_label::label_description().flex_none().opacity(0.7).child(description))
}),
)
// terminalTabsList.ts `getDecoration`: the primary status's icon at the row's end, in its severity's color.
.children(instance.primary_status().and_then(|status| status.icon.map(|icon| div().ml(px(5.)).child(self.terminal_status_icon(id, icon, self.terminal_status_color(status.severity).unwrap_or(foreground))))))
.when(TABS_DEFAULT_WIDTH >= TABS_ACTIONBAR_MINIMUM_WIDTH, |row| {
row.child(
// terminal.css: `.actions { display: none }`, shown on the hovered row.
div()
.hidden()
.ml(px(6.))
.group_hover("terminal-tab", |actions| actions.flex())
.child(self.terminal_tab_action("split-horizontal", id, false, cx))
.child(self.terminal_tab_action("trash", id, true, cx)),
)
})
.on_click(cx.listener(move |workbench, event: &gpui::ClickEvent, window, cx| {
workbench.terminal_view.set_active(id);
workbench.sync_terminals_to_host();
if event.click_count() >= 2 {
workbench.show_terminal_rename(id, window, cx);
return;
}
window.focus(&workbench.terminal_view.focus, cx);
cx.notify();
}))
.on_mouse_down(MouseButton::Right, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| workbench.terminal_tab_right_click(id, event.position, window, cx)));
list = list.child(row);
}
}
// terminalTabsList.ts: a double click below the rows creates a terminal.
list.child(div().id("terminal-tabs-empty").flex_1().min_h(px(TAB_HEIGHT)).on_click(cx.listener(|workbench, event: &gpui::ClickEvent, window, cx| {
if event.click_count() >= 2 {
workbench.create_terminal(None, false, window, cx);
}
})))
}
fn terminal_tab_action(&self, icon: &'static str, id: u32, kill: bool, cx: &mut Context<Self>) -> impl IntoElement {
let hover = self.color("toolbar.hoverBackground");
self.icon(icon, 20., 16., self.foreground_or("icon.foreground")).id((icon, id as usize)).rounded(px(4.)).cursor_pointer().hover(|action| action.bg(hover)).on_click(cx.listener(move |workbench, _: &gpui::ClickEvent, window, cx| {
cx.stop_propagation();
if kill {
workbench.kill_terminal(id, window, cx);
} else {
workbench.terminal_view.set_active(id);
workbench.create_terminal(None, true, window, cx);
}
}))
}
}
/// The profile's path, with its arguments when it has any (`_createProfileQuickPickItem`).
fn profile_description(profile: &Profile) -> String {
let path = match profile.is_from_path {
true => profile.path.file_name().map_or_else(|| profile.path.to_string_lossy(), |name| name.to_string_lossy()),
false => profile.path.to_string_lossy(),
};
if profile.args.is_empty() {
return path.into_owned();
}
let args: Vec<String> = profile.args.iter().map(|arg| if arg.contains(' ') { format!("\"{}\"", arg.replace('"', "\\\"")) } else { arg.clone() }).collect();
format!("{path} {}", args.join(" "))
}