terminal.rs

  1use std::path::PathBuf;
  2
  3use collections::HashMap;
  4use gpui::{AbsoluteLength, FontFeatures, SharedString, px};
  5use schemars::JsonSchema;
  6use serde::{Deserialize, Serialize};
  7use serde_with::skip_serializing_none;
  8use settings_macros::MergeFrom;
  9
 10use crate::FontFamilyName;
 11
 12#[skip_serializing_none]
 13#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize, JsonSchema, MergeFrom)]
 14pub struct TerminalSettingsContent {
 15    /// What shell to use when opening a terminal.
 16    ///
 17    /// Default: system
 18    pub shell: Option<Shell>,
 19    /// What working directory to use when launching the terminal
 20    ///
 21    /// Default: current_project_directory
 22    pub working_directory: Option<WorkingDirectory>,
 23    /// Sets the terminal's font size.
 24    ///
 25    /// If this option is not included,
 26    /// the terminal will default to matching the buffer's font size.
 27    pub font_size: Option<f32>,
 28    /// Sets the terminal's font family.
 29    ///
 30    /// If this option is not included,
 31    /// the terminal will default to matching the buffer's font family.
 32    pub font_family: Option<FontFamilyName>,
 33
 34    /// Sets the terminal's font fallbacks.
 35    ///
 36    /// If this option is not included,
 37    /// the terminal will default to matching the buffer's font fallbacks.
 38    #[schemars(extend("uniqueItems" = true))]
 39    pub font_fallbacks: Option<Vec<FontFamilyName>>,
 40
 41    /// Sets the terminal's line height.
 42    ///
 43    /// Default: comfortable
 44    pub line_height: Option<TerminalLineHeight>,
 45    pub font_features: Option<FontFeatures>,
 46    /// Sets the terminal's font weight in CSS weight units 0-900.
 47    pub font_weight: Option<f32>,
 48    /// Any key-value pairs added to this list will be added to the terminal's
 49    /// environment. Use `:` to separate multiple values.
 50    ///
 51    /// Default: {}
 52    pub env: Option<HashMap<String, String>>,
 53    /// Default cursor shape for the terminal.
 54    /// Can be "bar", "block", "underline", or "hollow".
 55    ///
 56    /// Default: None
 57    pub cursor_shape: Option<CursorShapeContent>,
 58    /// Sets the cursor blinking behavior in the terminal.
 59    ///
 60    /// Default: terminal_controlled
 61    pub blinking: Option<TerminalBlink>,
 62    /// Sets whether Alternate Scroll mode (code: ?1007) is active by default.
 63    /// Alternate Scroll mode converts mouse scroll events into up / down key
 64    /// presses when in the alternate screen (e.g. when running applications
 65    /// like vim or  less). The terminal can still set and unset this mode.
 66    ///
 67    /// Default: on
 68    pub alternate_scroll: Option<AlternateScroll>,
 69    /// Sets whether the option key behaves as the meta key.
 70    ///
 71    /// Default: false
 72    pub option_as_meta: Option<bool>,
 73    /// Whether or not selecting text in the terminal will automatically
 74    /// copy to the system clipboard.
 75    ///
 76    /// Default: false
 77    pub copy_on_select: Option<bool>,
 78    /// Whether to keep the text selection after copying it to the clipboard.
 79    ///
 80    /// Default: false
 81    pub keep_selection_on_copy: Option<bool>,
 82    /// Whether to show the terminal button in the status bar.
 83    ///
 84    /// Default: true
 85    pub button: Option<bool>,
 86    pub dock: Option<TerminalDockPosition>,
 87    /// Default width when the terminal is docked to the left or right.
 88    ///
 89    /// Default: 640
 90    pub default_width: Option<f32>,
 91    /// Default height when the terminal is docked to the bottom.
 92    ///
 93    /// Default: 320
 94    pub default_height: Option<f32>,
 95    /// Activates the python virtual environment, if one is found, in the
 96    /// terminal's working directory (as resolved by the working_directory
 97    /// setting). Set this to "off" to disable this behavior.
 98    ///
 99    /// Default: on
100    pub detect_venv: Option<VenvSettings>,
101    /// The maximum number of lines to keep in the scrollback history.
102    /// Maximum allowed value is 100_000, all values above that will be treated as 100_000.
103    /// 0 disables the scrolling.
104    /// Existing terminals will not pick up this change until they are recreated.
105    /// See <a href="https://github.com/alacritty/alacritty/blob/cb3a79dbf6472740daca8440d5166c1d4af5029e/extra/man/alacritty.5.scd?plain=1#L207-L213">Alacritty documentation</a> for more information.
106    ///
107    /// Default: 10_000
108    pub max_scroll_history_lines: Option<usize>,
109    /// Toolbar related settings
110    pub toolbar: Option<TerminalToolbarContent>,
111    /// Scrollbar-related settings
112    pub scrollbar: Option<ScrollbarSettingsContent>,
113    /// The minimum APCA perceptual contrast between foreground and background colors.
114    ///
115    /// APCA (Accessible Perceptual Contrast Algorithm) is more accurate than WCAG 2.x,
116    /// especially for dark mode. Values range from 0 to 106.
117    ///
118    /// Based on APCA Readability Criterion (ARC) Bronze Simple Mode:
119    /// https://readtech.org/ARC/tests/bronze-simple-mode/
120    /// - 0: No contrast adjustment
121    /// - 45: Minimum for large fluent text (36px+)
122    /// - 60: Minimum for other content text
123    /// - 75: Minimum for body text
124    /// - 90: Preferred for body text
125    ///
126    /// Default: 45
127    pub minimum_contrast: Option<f32>,
128}
129
130/// Shell configuration to open the terminal with.
131#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema, MergeFrom)]
132#[serde(rename_all = "snake_case")]
133pub enum Shell {
134    /// Use the system's default terminal configuration in /etc/passwd
135    #[default]
136    System,
137    /// Use a specific program with no arguments.
138    Program(String),
139    /// Use a specific program with arguments.
140    WithArguments {
141        /// The program to run.
142        program: String,
143        /// The arguments to pass to the program.
144        args: Vec<String>,
145        /// An optional string to override the title of the terminal tab
146        title_override: Option<SharedString>,
147    },
148}
149
150#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, MergeFrom)]
151#[serde(rename_all = "snake_case")]
152pub enum WorkingDirectory {
153    /// Use the current file's project directory.  Will Fallback to the
154    /// first project directory strategy if unsuccessful.
155    CurrentProjectDirectory,
156    /// Use the first project in this workspace's directory.
157    FirstProjectDirectory,
158    /// Always use this platform's home directory (if it can be found).
159    AlwaysHome,
160    /// Always use a specific directory. This value will be shell expanded.
161    /// If this path is not a valid directory the terminal will default to
162    /// this platform's home directory  (if it can be found).
163    Always { directory: String },
164}
165
166#[skip_serializing_none]
167#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, MergeFrom, PartialEq, Eq)]
168pub struct ScrollbarSettingsContent {
169    /// When to show the scrollbar in the terminal.
170    ///
171    /// Default: inherits editor scrollbar settings
172    pub show: Option<ShowScrollbar>,
173}
174
175#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom, Default)]
176#[serde(rename_all = "snake_case")]
177pub enum TerminalLineHeight {
178    /// Use a line height that's comfortable for reading, 1.618
179    #[default]
180    Comfortable,
181    /// Use a standard line height, 1.3. This option is useful for TUIs,
182    /// particularly if they use box characters
183    Standard,
184    /// Use a custom line height.
185    Custom(f32),
186}
187
188impl TerminalLineHeight {
189    pub fn value(&self) -> AbsoluteLength {
190        let value = match self {
191            TerminalLineHeight::Comfortable => 1.618,
192            TerminalLineHeight::Standard => 1.3,
193            TerminalLineHeight::Custom(line_height) => f32::max(*line_height, 1.),
194        };
195        px(value).into()
196    }
197}
198
199/// When to show the scrollbar.
200///
201/// Default: auto
202#[derive(
203    Copy,
204    Clone,
205    Debug,
206    Serialize,
207    Deserialize,
208    JsonSchema,
209    MergeFrom,
210    PartialEq,
211    Eq,
212    strum::VariantArray,
213    strum::VariantNames,
214)]
215#[serde(rename_all = "snake_case")]
216pub enum ShowScrollbar {
217    /// Show the scrollbar if there's important information or
218    /// follow the system's configured behavior.
219    Auto,
220    /// Match the system's configured behavior.
221    System,
222    /// Always show the scrollbar.
223    Always,
224    /// Never show the scrollbar.
225    Never,
226}
227
228#[derive(
229    Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema, MergeFrom,
230)]
231#[serde(rename_all = "snake_case")]
232// todo() -> combine with CursorShape
233pub enum CursorShapeContent {
234    /// Cursor is a block like `█`.
235    #[default]
236    Block,
237    /// Cursor is an underscore like `_`.
238    Underline,
239    /// Cursor is a vertical bar like `⎸`.
240    Bar,
241    /// Cursor is a hollow box like `▯`.
242    Hollow,
243}
244
245#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, MergeFrom)]
246#[serde(rename_all = "snake_case")]
247pub enum TerminalBlink {
248    /// Never blink the cursor, ignoring the terminal mode.
249    Off,
250    /// Default the cursor blink to off, but allow the terminal to
251    /// set blinking.
252    TerminalControlled,
253    /// Always blink the cursor, ignoring the terminal mode.
254    On,
255}
256
257#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, MergeFrom)]
258#[serde(rename_all = "snake_case")]
259pub enum AlternateScroll {
260    On,
261    Off,
262}
263
264// Toolbar related settings
265#[skip_serializing_none]
266#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema, MergeFrom, PartialEq, Eq)]
267pub struct TerminalToolbarContent {
268    /// Whether to display the terminal title in breadcrumbs inside the terminal pane.
269    /// Only shown if the terminal title is not empty.
270    ///
271    /// The shell running in the terminal needs to be configured to emit the title.
272    /// Example: `echo -e "\e]2;New Title\007";`
273    ///
274    /// Default: true
275    pub breadcrumbs: Option<bool>,
276}
277
278#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
279#[serde(rename_all = "snake_case")]
280pub enum VenvSettings {
281    #[default]
282    Off,
283    On {
284        /// Default directories to search for virtual environments, relative
285        /// to the current working directory. We recommend overriding this
286        /// in your project's settings, rather than globally.
287        activate_script: Option<ActivateScript>,
288        venv_name: Option<String>,
289        directories: Option<Vec<PathBuf>>,
290    },
291}
292#[skip_serializing_none]
293pub struct VenvSettingsContent<'a> {
294    pub activate_script: ActivateScript,
295    pub venv_name: &'a str,
296    pub directories: &'a [PathBuf],
297}
298
299impl VenvSettings {
300    pub fn as_option(&self) -> Option<VenvSettingsContent<'_>> {
301        match self {
302            VenvSettings::Off => None,
303            VenvSettings::On {
304                activate_script,
305                venv_name,
306                directories,
307            } => Some(VenvSettingsContent {
308                activate_script: activate_script.unwrap_or(ActivateScript::Default),
309                venv_name: venv_name.as_deref().unwrap_or(""),
310                directories: directories.as_deref().unwrap_or(&[]),
311            }),
312        }
313    }
314}
315
316#[derive(
317    Copy,
318    Clone,
319    Debug,
320    Serialize,
321    Deserialize,
322    JsonSchema,
323    MergeFrom,
324    PartialEq,
325    Eq,
326    strum::VariantArray,
327    strum::VariantNames,
328)]
329#[serde(rename_all = "snake_case")]
330pub enum TerminalDockPosition {
331    Left,
332    Bottom,
333    Right,
334}
335
336#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
337#[serde(rename_all = "snake_case")]
338pub enum ActivateScript {
339    #[default]
340    Default,
341    Csh,
342    Fish,
343    Nushell,
344    PowerShell,
345    Pyenv,
346}