terminal_settings.rs

  1use alacritty_terminal::vte::ansi::{
  2    CursorShape as AlacCursorShape, CursorStyle as AlacCursorStyle,
  3};
  4use collections::HashMap;
  5use gpui::{FontFallbacks, FontFeatures, FontWeight, Pixels, px};
  6use schemars::JsonSchema;
  7use serde::{Deserialize, Serialize};
  8
  9pub use settings::AlternateScroll;
 10
 11use settings::{
 12    IntoGpui, PathHyperlinkRegex, RegisterSetting, ShowScrollbar, TerminalBlink,
 13    TerminalDockPosition, TerminalLineHeight, VenvSettings, WorkingDirectory,
 14    merge_from::MergeFrom,
 15};
 16use task::Shell;
 17use theme::FontFamilyName;
 18
 19#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
 20pub struct Toolbar {
 21    pub breadcrumbs: bool,
 22}
 23
 24#[derive(Clone, Debug, Deserialize, RegisterSetting)]
 25pub struct TerminalSettings {
 26    pub shell: Shell,
 27    pub working_directory: WorkingDirectory,
 28    pub font_size: Option<Pixels>, // todo(settings_refactor) can be non-optional...
 29    pub font_family: Option<FontFamilyName>,
 30    pub font_fallbacks: Option<FontFallbacks>,
 31    pub font_features: Option<FontFeatures>,
 32    pub font_weight: Option<FontWeight>,
 33    pub line_height: TerminalLineHeight,
 34    pub env: HashMap<String, String>,
 35    pub cursor_shape: CursorShape,
 36    pub blinking: TerminalBlink,
 37    pub alternate_scroll: AlternateScroll,
 38    pub option_as_meta: bool,
 39    pub copy_on_select: bool,
 40    pub keep_selection_on_copy: bool,
 41    pub button: bool,
 42    pub dock: TerminalDockPosition,
 43    pub default_width: Pixels,
 44    pub default_height: Pixels,
 45    pub detect_venv: VenvSettings,
 46    pub max_scroll_history_lines: Option<usize>,
 47    pub scroll_multiplier: f32,
 48    pub toolbar: Toolbar,
 49    pub scrollbar: ScrollbarSettings,
 50    pub minimum_contrast: f32,
 51    pub path_hyperlink_regexes: Vec<String>,
 52    pub path_hyperlink_timeout_ms: u64,
 53    pub show_count_badge: bool,
 54}
 55
 56#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
 57pub struct ScrollbarSettings {
 58    /// When to show the scrollbar in the terminal.
 59    ///
 60    /// Default: inherits editor scrollbar settings
 61    pub show: Option<ShowScrollbar>,
 62}
 63
 64fn settings_shell_to_task_shell(shell: settings::Shell) -> Shell {
 65    match shell {
 66        settings::Shell::System => Shell::System,
 67        settings::Shell::Program(program) => Shell::Program(program),
 68        settings::Shell::WithArguments {
 69            program,
 70            args,
 71            title_override,
 72        } => Shell::WithArguments {
 73            program,
 74            args,
 75            title_override,
 76        },
 77    }
 78}
 79
 80impl settings::Settings for TerminalSettings {
 81    fn from_settings(content: &settings::SettingsContent) -> Self {
 82        let user_content = content.terminal.clone().unwrap();
 83        // Note: we allow a subset of "terminal" settings in the project files.
 84        let mut project_content = user_content.project.clone();
 85        project_content.merge_from_option(content.project.terminal.as_ref());
 86        TerminalSettings {
 87            shell: settings_shell_to_task_shell(project_content.shell.unwrap()),
 88            working_directory: project_content.working_directory.unwrap(),
 89            font_size: user_content.font_size.map(|s| s.into_gpui()),
 90            font_family: user_content.font_family,
 91            font_fallbacks: user_content.font_fallbacks.map(|fallbacks| {
 92                FontFallbacks::from_fonts(
 93                    fallbacks
 94                        .into_iter()
 95                        .map(|family| family.0.to_string())
 96                        .collect(),
 97                )
 98            }),
 99            font_features: user_content.font_features.map(|f| f.into_gpui()),
100            font_weight: user_content.font_weight.map(|w| w.into_gpui()),
101            line_height: user_content.line_height.unwrap(),
102            env: project_content.env.unwrap(),
103            cursor_shape: user_content.cursor_shape.unwrap().into(),
104            blinking: user_content.blinking.unwrap(),
105            alternate_scroll: user_content.alternate_scroll.unwrap(),
106            option_as_meta: user_content.option_as_meta.unwrap(),
107            copy_on_select: user_content.copy_on_select.unwrap(),
108            keep_selection_on_copy: user_content.keep_selection_on_copy.unwrap(),
109            button: user_content.button.unwrap(),
110            dock: user_content.dock.unwrap(),
111            default_width: px(user_content.default_width.unwrap()),
112            default_height: px(user_content.default_height.unwrap()),
113            detect_venv: project_content.detect_venv.unwrap(),
114            scroll_multiplier: user_content.scroll_multiplier.unwrap(),
115            max_scroll_history_lines: user_content.max_scroll_history_lines,
116            toolbar: Toolbar {
117                breadcrumbs: user_content.toolbar.unwrap().breadcrumbs.unwrap(),
118            },
119            scrollbar: ScrollbarSettings {
120                show: user_content.scrollbar.unwrap().show,
121            },
122            minimum_contrast: user_content.minimum_contrast.unwrap(),
123            path_hyperlink_regexes: project_content
124                .path_hyperlink_regexes
125                .unwrap()
126                .into_iter()
127                .map(|regex| match regex {
128                    PathHyperlinkRegex::SingleLine(regex) => regex,
129                    PathHyperlinkRegex::MultiLine(regex) => regex.join("\n"),
130                })
131                .collect(),
132            path_hyperlink_timeout_ms: project_content.path_hyperlink_timeout_ms.unwrap(),
133            show_count_badge: user_content.show_count_badge.unwrap(),
134        }
135    }
136}
137
138#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
139#[serde(rename_all = "snake_case")]
140pub enum CursorShape {
141    /// Cursor is a block like `█`.
142    #[default]
143    Block,
144    /// Cursor is an underscore like `_`.
145    Underline,
146    /// Cursor is a vertical bar like `⎸`.
147    Bar,
148    /// Cursor is a hollow box like `▯`.
149    Hollow,
150}
151
152impl From<settings::CursorShapeContent> for CursorShape {
153    fn from(value: settings::CursorShapeContent) -> Self {
154        match value {
155            settings::CursorShapeContent::Block => CursorShape::Block,
156            settings::CursorShapeContent::Underline => CursorShape::Underline,
157            settings::CursorShapeContent::Bar => CursorShape::Bar,
158            settings::CursorShapeContent::Hollow => CursorShape::Hollow,
159        }
160    }
161}
162
163impl From<CursorShape> for AlacCursorShape {
164    fn from(value: CursorShape) -> Self {
165        match value {
166            CursorShape::Block => AlacCursorShape::Block,
167            CursorShape::Underline => AlacCursorShape::Underline,
168            CursorShape::Bar => AlacCursorShape::Beam,
169            CursorShape::Hollow => AlacCursorShape::HollowBlock,
170        }
171    }
172}
173
174impl From<CursorShape> for AlacCursorStyle {
175    fn from(value: CursorShape) -> Self {
176        AlacCursorStyle {
177            shape: value.into(),
178            blinking: false,
179        }
180    }
181}