2019-03-30 16:48:36 +00:00
|
|
|
use std::collections::HashMap;
|
2019-07-06 19:34:56 +00:00
|
|
|
use std::fmt::Display;
|
2020-01-12 00:24:56 +00:00
|
|
|
use std::path::PathBuf;
|
2017-07-20 17:50:50 +00:00
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
use log::error;
|
2019-05-10 11:36:16 +00:00
|
|
|
use serde::{Deserialize, Deserializer};
|
2019-07-06 19:34:56 +00:00
|
|
|
use serde_yaml::Value;
|
2017-02-24 16:23:07 +00:00
|
|
|
|
2020-07-10 19:32:44 +00:00
|
|
|
mod bell;
|
2019-05-10 11:36:16 +00:00
|
|
|
mod colors;
|
|
|
|
mod scrolling;
|
2018-09-18 15:54:40 +00:00
|
|
|
|
2020-07-09 21:45:22 +00:00
|
|
|
use crate::ansi::CursorStyle;
|
2019-05-10 11:36:16 +00:00
|
|
|
|
2020-07-10 19:32:44 +00:00
|
|
|
pub use crate::config::bell::{BellAnimation, BellConfig};
|
2019-05-10 11:36:16 +00:00
|
|
|
pub use crate::config::colors::Colors;
|
|
|
|
pub use crate::config::scrolling::Scrolling;
|
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
pub const LOG_TARGET_CONFIG: &str = "alacritty_config";
|
2019-05-10 11:36:16 +00:00
|
|
|
const MAX_SCROLLBACK_LINES: u32 = 100_000;
|
2020-04-15 03:50:34 +00:00
|
|
|
const DEFAULT_CURSOR_THICKNESS: f32 = 0.15;
|
2017-12-20 19:12:32 +00:00
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
pub type MockConfig = Config<HashMap<String, serde_yaml::Value>>;
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Top-level config type.
|
2019-10-06 10:47:20 +00:00
|
|
|
#[derive(Debug, PartialEq, Default, Deserialize)]
|
2019-10-05 00:29:26 +00:00
|
|
|
pub struct Config<T> {
|
2020-05-05 22:50:23 +00:00
|
|
|
/// TERM env variable.
|
2018-01-06 00:50:12 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub env: HashMap<String, String>,
|
2017-02-14 02:22:59 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Should draw bold text with brighter colors instead of bold font.
|
2019-05-10 11:36:16 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
2020-01-20 21:13:39 +00:00
|
|
|
draw_bold_text_with_bright_colors: bool,
|
2016-10-28 16:19:48 +00:00
|
|
|
|
2018-01-06 00:50:12 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub colors: Colors,
|
2016-11-15 17:38:50 +00:00
|
|
|
|
2018-01-06 00:50:12 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub selection: Selection,
|
2017-01-15 01:53:48 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Path to a shell program to run on startup.
|
2020-06-04 22:10:31 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
|
|
|
pub shell: Option<Program>,
|
2017-01-07 02:06:45 +00:00
|
|
|
|
2020-07-10 19:32:44 +00:00
|
|
|
/// Bell configuration.
|
2018-01-06 00:50:12 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
2020-07-10 19:32:44 +00:00
|
|
|
bell: BellConfig,
|
2017-02-22 19:52:37 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// How much scrolling history to keep.
|
2019-05-10 11:36:16 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
|
|
|
pub scrolling: Scrolling,
|
2017-02-05 10:01:26 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Cursor configuration.
|
2019-05-10 11:36:16 +00:00
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
|
|
|
pub cursor: Cursor,
|
2017-12-20 19:12:32 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Use WinPTY backend even if ConPTY is available.
|
2019-05-10 11:36:16 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
2019-12-21 21:23:18 +00:00
|
|
|
pub winpty_backend: bool,
|
2017-02-03 23:34:52 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Shell startup directory.
|
2019-07-06 19:34:56 +00:00
|
|
|
#[serde(default, deserialize_with = "option_explicit_none")]
|
2020-01-12 00:24:56 +00:00
|
|
|
pub working_directory: Option<PathBuf>,
|
2017-01-10 13:58:54 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Additional configuration options not directly required by the terminal.
|
2019-10-05 00:29:26 +00:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub ui_config: T,
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Remain open after child process exits.
|
2019-10-09 21:37:48 +00:00
|
|
|
#[serde(skip)]
|
|
|
|
pub hold: bool,
|
|
|
|
|
2020-07-10 19:32:44 +00:00
|
|
|
// TODO: DEPRECATED
|
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
|
|
|
pub visual_bell: Option<BellConfig>,
|
|
|
|
|
2020-03-01 04:07:36 +00:00
|
|
|
// TODO: REMOVED
|
|
|
|
#[serde(default, deserialize_with = "failure_default")]
|
|
|
|
pub tabspaces: Option<usize>,
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
2017-01-07 02:06:45 +00:00
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
impl<T> Config<T> {
|
2017-12-03 21:38:42 +00:00
|
|
|
#[inline]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub fn draw_bold_text_with_bright_colors(&self) -> bool {
|
2020-01-20 21:13:39 +00:00
|
|
|
self.draw_bold_text_with_bright_colors
|
2017-12-03 21:38:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 19:32:44 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn bell(&self) -> &BellConfig {
|
|
|
|
self.visual_bell.as_ref().unwrap_or(&self.bell)
|
|
|
|
}
|
2016-08-12 18:38:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 09:17:26 +00:00
|
|
|
#[serde(default)]
|
2019-05-10 11:36:16 +00:00
|
|
|
#[derive(Deserialize, Default, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct Selection {
|
2019-01-17 09:17:26 +00:00
|
|
|
#[serde(deserialize_with = "failure_default")]
|
2019-05-10 11:36:16 +00:00
|
|
|
semantic_escape_chars: EscapeChars,
|
2019-01-17 09:17:26 +00:00
|
|
|
#[serde(deserialize_with = "failure_default")]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub save_to_clipboard: bool,
|
2019-01-17 09:17:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 11:36:16 +00:00
|
|
|
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 {
|
2019-07-26 11:18:47 +00:00
|
|
|
EscapeChars(String::from(",│`|:\"' ()[]{}<>\t"))
|
2016-06-30 16:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 09:17:26 +00:00
|
|
|
#[serde(default)]
|
2020-04-15 03:50:34 +00:00
|
|
|
#[derive(Deserialize, Copy, Clone, Debug, PartialEq)]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub struct Cursor {
|
2019-01-17 09:17:26 +00:00
|
|
|
#[serde(deserialize_with = "failure_default")]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub style: CursorStyle,
|
2020-03-18 02:35:08 +00:00
|
|
|
#[serde(deserialize_with = "option_explicit_none")]
|
|
|
|
pub vi_mode_style: Option<CursorStyle>,
|
2020-04-15 03:50:34 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_cursor_thickness")]
|
|
|
|
thickness: Percentage,
|
2019-01-17 09:17:26 +00:00
|
|
|
#[serde(deserialize_with = "failure_default")]
|
2019-05-10 11:36:16 +00:00
|
|
|
unfocused_hollow: DefaultTrueBool,
|
2019-01-17 09:17:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 11:36:16 +00:00
|
|
|
impl Cursor {
|
2020-04-15 03:50:34 +00:00
|
|
|
#[inline]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub fn unfocused_hollow(self) -> bool {
|
|
|
|
self.unfocused_hollow.0
|
|
|
|
}
|
2020-04-15 03:50:34 +00:00
|
|
|
|
|
|
|
#[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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:31:57 +00:00
|
|
|
fn deserialize_cursor_thickness<'a, D>(deserializer: D) -> Result<Percentage, D::Error>
|
2020-04-15 03:50:34 +00:00
|
|
|
where
|
|
|
|
D: Deserializer<'a>,
|
|
|
|
{
|
2020-06-05 22:31:57 +00:00
|
|
|
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))
|
|
|
|
},
|
|
|
|
}
|
2016-12-11 07:32:12 +00:00
|
|
|
}
|
2016-10-27 17:05:04 +00:00
|
|
|
|
2020-06-04 22:10:31 +00:00
|
|
|
#[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>,
|
|
|
|
},
|
2016-10-27 17:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 22:10:31 +00:00
|
|
|
impl Program {
|
|
|
|
pub fn program(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Program::Just(program) => program,
|
|
|
|
Program::WithArgs { program, .. } => program,
|
|
|
|
}
|
2016-10-27 17:05:04 +00:00
|
|
|
}
|
2016-11-15 17:38:50 +00:00
|
|
|
|
2020-06-04 22:10:31 +00:00
|
|
|
pub fn args(&self) -> &[String] {
|
|
|
|
match self {
|
|
|
|
Program::Just(_) => &[],
|
|
|
|
Program::WithArgs { args, .. } => args,
|
|
|
|
}
|
2019-07-06 19:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 03:50:34 +00:00
|
|
|
/// Wrapper around f32 that represents a percentage value between 0.0 and 1.0.
|
2019-05-10 11:36:16 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2020-04-15 03:50:34 +00:00
|
|
|
pub struct Percentage(f32);
|
2019-05-10 11:36:16 +00:00
|
|
|
|
2020-04-15 03:50:34 +00:00
|
|
|
impl Percentage {
|
2019-05-10 11:36:16 +00:00
|
|
|
pub fn new(value: f32) -> Self {
|
2020-04-15 03:50:34 +00:00
|
|
|
Percentage(if value < 0.0 {
|
2019-05-10 11:36:16 +00:00
|
|
|
0.0
|
|
|
|
} else if value > 1.0 {
|
|
|
|
1.0
|
|
|
|
} else {
|
|
|
|
value
|
|
|
|
})
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
2020-07-11 17:03:09 +00:00
|
|
|
|
|
|
|
pub fn as_f32(self) -> f32 {
|
|
|
|
self.0
|
|
|
|
}
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
2019-01-17 09:17:26 +00:00
|
|
|
|
2020-04-15 03:50:34 +00:00
|
|
|
impl Default for Percentage {
|
2019-05-10 11:36:16 +00:00
|
|
|
fn default() -> Self {
|
2020-04-15 03:50:34 +00:00
|
|
|
Percentage(1.0)
|
2019-01-17 09:17:26 +00:00
|
|
|
}
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
2019-01-17 09:17:26 +00:00
|
|
|
|
2020-04-15 03:50:34 +00:00
|
|
|
impl<'a> Deserialize<'a> for Percentage {
|
2019-05-10 11:36:16 +00:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'a>,
|
|
|
|
{
|
2020-04-15 03:50:34 +00:00
|
|
|
Ok(Percentage::new(f32::deserialize(deserializer)?))
|
2019-01-17 09:17:26 +00:00
|
|
|
}
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
2019-01-17 09:17:26 +00:00
|
|
|
|
2019-05-10 11:36:16 +00:00
|
|
|
#[derive(Deserialize, Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
struct DefaultTrueBool(bool);
|
2019-01-17 09:17:26 +00:00
|
|
|
|
2019-05-10 11:36:16 +00:00
|
|
|
impl Default for DefaultTrueBool {
|
|
|
|
fn default() -> Self {
|
|
|
|
DefaultTrueBool(true)
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 09:17:26 +00:00
|
|
|
|
2019-07-06 19:34:56 +00:00
|
|
|
fn fallback_default<T, E>(err: E) -> T
|
2019-07-07 14:51:11 +00:00
|
|
|
where
|
|
|
|
T: Default,
|
|
|
|
E: Display,
|
2019-07-06 19:34:56 +00:00
|
|
|
{
|
2019-10-05 00:29:26 +00:00
|
|
|
error!(target: LOG_TARGET_CONFIG, "Problem with config: {}; using default value", err);
|
2019-07-06 19:34:56 +00:00
|
|
|
T::default()
|
|
|
|
}
|
|
|
|
|
2019-05-10 11:36:16 +00:00
|
|
|
pub fn failure_default<'a, D, T>(deserializer: D) -> Result<T, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'a>,
|
|
|
|
T: Deserialize<'a> + Default,
|
|
|
|
{
|
2019-07-06 19:34:56 +00:00
|
|
|
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>
|
2019-07-07 14:51:11 +00:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
T: Deserialize<'de> + Default,
|
2019-07-06 19:34:56 +00:00
|
|
|
{
|
|
|
|
Ok(match Value::deserialize(deserializer)? {
|
|
|
|
Value::String(ref value) if value.to_lowercase() == "none" => None,
|
2019-07-07 14:51:11 +00:00
|
|
|
value => Some(T::deserialize(value).unwrap_or_else(fallback_default)),
|
2019-07-06 19:34:56 +00:00
|
|
|
})
|
|
|
|
}
|