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}
 54
 55#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
 56pub struct ScrollbarSettings {
 57    /// When to show the scrollbar in the terminal.
 58    ///
 59    /// Default: inherits editor scrollbar settings
 60    pub show: Option<ShowScrollbar>,
 61}
 62
 63fn settings_shell_to_task_shell(shell: settings::Shell) -> Shell {
 64    match shell {
 65        settings::Shell::System => Shell::System,
 66        settings::Shell::Program(program) => Shell::Program(program),
 67        settings::Shell::WithArguments {
 68            program,
 69            args,
 70            title_override,
 71        } => Shell::WithArguments {
 72            program,
 73            args,
 74            title_override,
 75        },
 76    }
 77}
 78
 79impl settings::Settings for TerminalSettings {
 80    fn from_settings(content: &settings::SettingsContent) -> Self {
 81        let user_content = content.terminal.clone().unwrap();
 82        // Note: we allow a subset of "terminal" settings in the project files.
 83        let mut project_content = user_content.project.clone();
 84        project_content.merge_from_option(content.project.terminal.as_ref());
 85        TerminalSettings {
 86            shell: settings_shell_to_task_shell(project_content.shell.unwrap()),
 87            working_directory: project_content.working_directory.unwrap(),
 88            font_size: user_content.font_size.map(|s| s.into_gpui()),
 89            font_family: user_content.font_family,
 90            font_fallbacks: user_content.font_fallbacks.map(|fallbacks| {
 91                FontFallbacks::from_fonts(
 92                    fallbacks
 93                        .into_iter()
 94                        .map(|family| family.0.to_string())
 95                        .collect(),
 96                )
 97            }),
 98            font_features: user_content.font_features.map(|f| f.into_gpui()),
 99            font_weight: user_content.font_weight.map(|w| w.into_gpui()),
100            line_height: user_content.line_height.unwrap(),
101            env: project_content.env.unwrap(),
102            cursor_shape: user_content.cursor_shape.unwrap().into(),
103            blinking: user_content.blinking.unwrap(),
104            alternate_scroll: user_content.alternate_scroll.unwrap(),
105            option_as_meta: user_content.option_as_meta.unwrap(),
106            copy_on_select: user_content.copy_on_select.unwrap(),
107            keep_selection_on_copy: user_content.keep_selection_on_copy.unwrap(),
108            button: user_content.button.unwrap(),
109            dock: user_content.dock.unwrap(),
110            default_width: px(user_content.default_width.unwrap()),
111            default_height: px(user_content.default_height.unwrap()),
112            detect_venv: project_content.detect_venv.unwrap(),
113            scroll_multiplier: user_content.scroll_multiplier.unwrap(),
114            max_scroll_history_lines: user_content.max_scroll_history_lines,
115            toolbar: Toolbar {
116                breadcrumbs: user_content.toolbar.unwrap().breadcrumbs.unwrap(),
117            },
118            scrollbar: ScrollbarSettings {
119                show: user_content.scrollbar.unwrap().show,
120            },
121            minimum_contrast: user_content.minimum_contrast.unwrap(),
122            path_hyperlink_regexes: project_content
123                .path_hyperlink_regexes
124                .unwrap()
125                .into_iter()
126                .map(|regex| match regex {
127                    PathHyperlinkRegex::SingleLine(regex) => regex,
128                    PathHyperlinkRegex::MultiLine(regex) => regex.join("\n"),
129                })
130                .collect(),
131            path_hyperlink_timeout_ms: project_content.path_hyperlink_timeout_ms.unwrap(),
132        }
133    }
134}
135
136#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
137#[serde(rename_all = "snake_case")]
138pub enum CursorShape {
139    /// Cursor is a block like `█`.
140    #[default]
141    Block,
142    /// Cursor is an underscore like `_`.
143    Underline,
144    /// Cursor is a vertical bar like `⎸`.
145    Bar,
146    /// Cursor is a hollow box like `▯`.
147    Hollow,
148}
149
150impl From<settings::CursorShapeContent> for CursorShape {
151    fn from(value: settings::CursorShapeContent) -> Self {
152        match value {
153            settings::CursorShapeContent::Block => CursorShape::Block,
154            settings::CursorShapeContent::Underline => CursorShape::Underline,
155            settings::CursorShapeContent::Bar => CursorShape::Bar,
156            settings::CursorShapeContent::Hollow => CursorShape::Hollow,
157        }
158    }
159}
160
161impl From<CursorShape> for AlacCursorShape {
162    fn from(value: CursorShape) -> Self {
163        match value {
164            CursorShape::Block => AlacCursorShape::Block,
165            CursorShape::Underline => AlacCursorShape::Underline,
166            CursorShape::Bar => AlacCursorShape::Beam,
167            CursorShape::Hollow => AlacCursorShape::HollowBlock,
168        }
169    }
170}
171
172impl From<CursorShape> for AlacCursorStyle {
173    fn from(value: CursorShape) -> Self {
174        AlacCursorStyle {
175            shape: value.into(),
176            blinking: false,
177        }
178    }
179}