alacritty/alacritty_terminal/src/config/mod.rs

372 lines
9.4 KiB
Rust
Raw Normal View History

use std::cmp::max;
2019-03-30 16:48:36 +00:00
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::{CursorShape, 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 DEFAULT_CURSOR_THICKNESS: f32 = 0.15;
const MAX_SCROLLBACK_LINES: u32 = 100_000;
const MIN_BLINK_INTERVAL: u64 = 10;
pub type MockConfig = Config<HashMap<String, serde_yaml::Value>>;
2020-05-05 22:50:23 +00:00
/// Top-level config type.
#[derive(Debug, PartialEq, Default, Deserialize)]
pub struct Config<T> {
2020-05-05 22:50:23 +00:00
/// TERM env variable.
#[serde(default, deserialize_with = "failure_default")]
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.
#[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,
2020-05-05 22:50:23 +00:00
/// Path to a shell program to run on startup.
#[serde(default, deserialize_with = "failure_default")]
pub shell: Option<Program>,
/// Bell configuration.
#[serde(default, deserialize_with = "failure_default")]
bell: BellConfig,
2017-02-22 19:52:37 +00:00
2020-05-05 22:50:23 +00:00
/// How much scrolling history to keep.
#[serde(default, deserialize_with = "failure_default")]
pub scrolling: Scrolling,
2020-05-05 22:50:23 +00:00
/// Cursor configuration.
#[serde(default, deserialize_with = "failure_default")]
pub cursor: Cursor,
2020-05-05 22:50:23 +00:00
/// Shell startup directory.
#[serde(default, deserialize_with = "option_explicit_none")]
pub working_directory: Option<PathBuf>,
2020-05-05 22:50:23 +00:00
/// Additional configuration options not directly required by the terminal.
#[serde(flatten)]
pub ui_config: T,
2020-05-05 22:50:23 +00:00
/// Remain open after child process exits.
#[serde(skip)]
pub hold: bool,
2020-11-06 17:33:02 +00:00
// TODO: DEPRECATED
#[cfg(windows)]
#[serde(default, deserialize_with = "failure_default")]
pub winpty_backend: 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: ConfigCursorStyle,
#[serde(deserialize_with = "option_explicit_none")]
pub vi_mode_style: Option<ConfigCursorStyle>,
#[serde(deserialize_with = "failure_default")]
blink_interval: BlinkInterval,
#[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
}
#[inline]
pub fn style(self) -> CursorStyle {
self.style.into()
}
#[inline]
pub fn vi_mode_style(self) -> Option<CursorStyle> {
self.vi_mode_style.map(From::from)
}
#[inline]
pub fn blink_interval(self) -> u64 {
max(self.blink_interval.0, MIN_BLINK_INTERVAL)
}
}
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(),
blink_interval: Default::default(),
}
}
}
#[derive(Deserialize, Copy, Clone, Debug, PartialEq)]
struct BlinkInterval(u64);
impl Default for BlinkInterval {
fn default() -> Self {
BlinkInterval(750)
}
}
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, Copy, Clone, PartialEq, Eq)]
pub enum ConfigCursorStyle {
Shape(CursorShape),
WithBlinking {
#[serde(default, deserialize_with = "failure_default")]
shape: CursorShape,
#[serde(default, deserialize_with = "failure_default")]
blinking: CursorBlinking,
},
}
impl Default for ConfigCursorStyle {
fn default() -> Self {
Self::WithBlinking { shape: CursorShape::default(), blinking: CursorBlinking::default() }
}
}
impl ConfigCursorStyle {
/// Check if blinking is force enabled/disabled.
pub fn blinking_override(&self) -> Option<bool> {
match self {
Self::Shape(_) => None,
Self::WithBlinking { blinking, .. } => blinking.blinking_override(),
}
}
}
impl From<ConfigCursorStyle> for CursorStyle {
fn from(config_style: ConfigCursorStyle) -> Self {
match config_style {
ConfigCursorStyle::Shape(shape) => Self { shape, blinking: false },
ConfigCursorStyle::WithBlinking { shape, blinking } => {
Self { shape, blinking: blinking.into() }
},
}
}
}
#[derive(Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
pub enum CursorBlinking {
Never,
Off,
On,
Always,
}
impl Default for CursorBlinking {
fn default() -> Self {
CursorBlinking::Off
}
}
impl CursorBlinking {
fn blinking_override(&self) -> Option<bool> {
match self {
Self::Never => Some(false),
Self::Off | Self::On => None,
Self::Always => Some(true),
}
}
}
impl Into<bool> for CursorBlinking {
fn into(self) -> bool {
self == Self::On || self == Self::Always
}
}
#[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)),
})
}