1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::collections::HashMap;
use std::fmt::Display;
use std::path::PathBuf;

use log::error;
use serde::{Deserialize, Deserializer};
use serde_yaml::Value;

mod bell;
mod colors;
mod scrolling;

use crate::ansi::CursorStyle;

pub use crate::config::bell::{BellAnimation, BellConfig};
pub use crate::config::colors::Colors;
pub use crate::config::scrolling::Scrolling;

pub const LOG_TARGET_CONFIG: &str = "alacritty_config";
const MAX_SCROLLBACK_LINES: u32 = 100_000;
const DEFAULT_CURSOR_THICKNESS: f32 = 0.15;

pub type MockConfig = Config<HashMap<String, serde_yaml::Value>>;

/// Top-level config type.
#[derive(Debug, PartialEq, Default, Deserialize)]
pub struct Config<T> {
    /// TERM env variable.
    #[serde(default, deserialize_with = "failure_default")]
    pub env: HashMap<String, String>,

    /// Should draw bold text with brighter colors instead of bold font.
    #[serde(default, deserialize_with = "failure_default")]
    draw_bold_text_with_bright_colors: bool,

    #[serde(default, deserialize_with = "failure_default")]
    pub colors: Colors,

    #[serde(default, deserialize_with = "failure_default")]
    pub selection: Selection,

    /// Path to a shell program to run on startup.
    #[serde(default, deserialize_with = "failure_default")]
    pub shell: Option<Program>,

    /// Path where config was loaded from.
    #[serde(default, deserialize_with = "failure_default")]
    pub config_path: Option<PathBuf>,

    /// Bell configuration.
    #[serde(default, deserialize_with = "failure_default")]
    bell: BellConfig,

    /// How much scrolling history to keep.
    #[serde(default, deserialize_with = "failure_default")]
    pub scrolling: Scrolling,

    /// Cursor configuration.
    #[serde(default, deserialize_with = "failure_default")]
    pub cursor: Cursor,

    /// Use WinPTY backend even if ConPTY is available.
    #[cfg(windows)]
    #[serde(default, deserialize_with = "failure_default")]
    pub winpty_backend: bool,

    /// Shell startup directory.
    #[serde(default, deserialize_with = "option_explicit_none")]
    pub working_directory: Option<PathBuf>,

    /// Additional configuration options not directly required by the terminal.
    #[serde(flatten)]
    pub ui_config: T,

    /// Remain open after child process exits.
    #[serde(skip)]
    pub hold: bool,

    // TODO: DEPRECATED
    #[serde(default, deserialize_with = "failure_default")]
    pub visual_bell: Option<BellConfig>,

    // TODO: REMOVED
    #[serde(default, deserialize_with = "failure_default")]
    pub tabspaces: Option<usize>,
}

impl<T> Config<T> {
    #[inline]
    pub fn draw_bold_text_with_bright_colors(&self) -> bool {
        self.draw_bold_text_with_bright_colors
    }

    #[inline]
    pub fn bell(&self) -> &BellConfig {
        self.visual_bell.as_ref().unwrap_or(&self.bell)
    }
}

#[serde(default)]
#[derive(Deserialize, Default, Clone, Debug, PartialEq, Eq)]
pub struct Selection {
    #[serde(deserialize_with = "failure_default")]
    semantic_escape_chars: EscapeChars,
    #[serde(deserialize_with = "failure_default")]
    pub save_to_clipboard: bool,
}

impl Selection {
    pub fn semantic_escape_chars(&self) -> &str {
        &self.semantic_escape_chars.0
    }
}

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
struct EscapeChars(String);

impl Default for EscapeChars {
    fn default() -> Self {
        EscapeChars(String::from(",│`|:\"' ()[]{}<>\t"))
    }
}

#[serde(default)]
#[derive(Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct Cursor {
    #[serde(deserialize_with = "failure_default")]
    pub style: CursorStyle,
    #[serde(deserialize_with = "option_explicit_none")]
    pub vi_mode_style: Option<CursorStyle>,
    #[serde(deserialize_with = "deserialize_cursor_thickness")]
    thickness: Percentage,
    #[serde(deserialize_with = "failure_default")]
    unfocused_hollow: DefaultTrueBool,
}

impl Cursor {
    #[inline]
    pub fn unfocused_hollow(self) -> bool {
        self.unfocused_hollow.0
    }

    #[inline]
    pub fn thickness(self) -> f64 {
        self.thickness.0 as f64
    }
}

impl Default for Cursor {
    fn default() -> Self {
        Self {
            style: Default::default(),
            vi_mode_style: Default::default(),
            thickness: Percentage::new(DEFAULT_CURSOR_THICKNESS),
            unfocused_hollow: Default::default(),
        }
    }
}

fn deserialize_cursor_thickness<'a, D>(deserializer: D) -> Result<Percentage, D::Error>
where
    D: Deserializer<'a>,
{
    let value = Value::deserialize(deserializer)?;
    match Percentage::deserialize(value) {
        Ok(value) => Ok(value),
        Err(err) => {
            error!(
                target: LOG_TARGET_CONFIG,
                "Problem with config: {}, using default thickness value {}",
                err,
                DEFAULT_CURSOR_THICKNESS
            );

            Ok(Percentage::new(DEFAULT_CURSOR_THICKNESS))
        },
    }
}

#[serde(untagged)]
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum Program {
    Just(String),
    WithArgs {
        program: String,
        #[serde(default, deserialize_with = "failure_default")]
        args: Vec<String>,
    },
}

impl Program {
    pub fn program(&self) -> &str {
        match self {
            Program::Just(program) => program,
            Program::WithArgs { program, .. } => program,
        }
    }

    pub fn args(&self) -> &[String] {
        match self {
            Program::Just(_) => &[],
            Program::WithArgs { args, .. } => args,
        }
    }
}

/// Wrapper around f32 that represents a percentage value between 0.0 and 1.0.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Percentage(f32);

impl Percentage {
    pub fn new(value: f32) -> Self {
        Percentage(if value < 0.0 {
            0.0
        } else if value > 1.0 {
            1.0
        } else {
            value
        })
    }

    pub fn as_f32(self) -> f32 {
        self.0
    }
}

impl Default for Percentage {
    fn default() -> Self {
        Percentage(1.0)
    }
}

impl<'a> Deserialize<'a> for Percentage {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'a>,
    {
        Ok(Percentage::new(f32::deserialize(deserializer)?))
    }
}

#[derive(Deserialize, Copy, Clone, Debug, PartialEq, Eq)]
struct DefaultTrueBool(bool);

impl Default for DefaultTrueBool {
    fn default() -> Self {
        DefaultTrueBool(true)
    }
}

fn fallback_default<T, E>(err: E) -> T
where
    T: Default,
    E: Display,
{
    error!(target: LOG_TARGET_CONFIG, "Problem with config: {}; using default value", err);
    T::default()
}

pub fn failure_default<'a, D, T>(deserializer: D) -> Result<T, D::Error>
where
    D: Deserializer<'a>,
    T: Deserialize<'a> + Default,
{
    Ok(T::deserialize(Value::deserialize(deserializer)?).unwrap_or_else(fallback_default))
}

pub fn option_explicit_none<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
where
    D: Deserializer<'de>,
    T: Deserialize<'de> + Default,
{
    Ok(match Value::deserialize(deserializer)? {
        Value::String(ref value) if value.to_lowercase() == "none" => None,
        value => Some(T::deserialize(value).unwrap_or_else(fallback_default)),
    })
}