2018-02-07 18:51:04 +03:00
|
|
|
//! Configuration for the gui portion of the terminal
|
|
|
|
|
|
|
|
use failure::Error;
|
|
|
|
use std;
|
|
|
|
use std::fs;
|
|
|
|
use std::io::prelude::*;
|
2018-02-25 02:24:51 +03:00
|
|
|
use term::hyperlink;
|
2018-02-07 18:51:04 +03:00
|
|
|
use toml;
|
|
|
|
|
2018-02-07 20:23:24 +03:00
|
|
|
use term;
|
2018-02-10 19:36:34 +03:00
|
|
|
use term::color::RgbColor;
|
2018-02-07 18:51:04 +03:00
|
|
|
|
2018-02-07 20:23:24 +03:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2018-02-07 18:51:04 +03:00
|
|
|
pub struct Config {
|
|
|
|
/// The font size, measured in points
|
|
|
|
#[serde(default = "default_font_size")]
|
|
|
|
pub font_size: f64,
|
|
|
|
|
|
|
|
/// The DPI to assume
|
|
|
|
#[serde(default = "default_dpi")]
|
|
|
|
pub dpi: f64,
|
|
|
|
|
2018-02-07 20:23:24 +03:00
|
|
|
/// The baseline font to use
|
2018-02-07 18:51:04 +03:00
|
|
|
#[serde(default)]
|
|
|
|
pub font: TextStyle,
|
2018-02-07 20:23:24 +03:00
|
|
|
|
|
|
|
/// An optional set of style rules to select the font based
|
|
|
|
/// on the cell attributes
|
2018-02-08 02:42:12 +03:00
|
|
|
#[serde(default)]
|
2018-02-07 20:23:24 +03:00
|
|
|
pub font_rules: Vec<StyleRule>,
|
2018-02-10 19:16:20 +03:00
|
|
|
|
|
|
|
/// The color palette
|
|
|
|
pub colors: Option<Palette>,
|
2018-02-10 20:43:54 +03:00
|
|
|
|
|
|
|
/// How many lines of scrollback you want to retain
|
|
|
|
pub scrollback_lines: Option<usize>,
|
2018-02-25 02:24:51 +03:00
|
|
|
|
|
|
|
#[serde(default = "default_hyperlink_rules")]
|
|
|
|
pub hyperlink_rules: Vec<hyperlink::Rule>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_hyperlink_rules() -> Vec<hyperlink::Rule> {
|
|
|
|
vec![
|
|
|
|
// URL with a protocol
|
|
|
|
hyperlink::Rule::new(r"\b\w+://(?:[\w.-]+)\.[a-z]{2,15}\S*\b", "$0").unwrap(),
|
|
|
|
// implicit mailto link
|
|
|
|
hyperlink::Rule::new(r"\b\w+@[\w-]+(\.[\w-]+)+\b", "mailto:$0").unwrap(),
|
|
|
|
]
|
2018-02-07 18:51:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_font_size() -> f64 {
|
|
|
|
10.0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_dpi() -> f64 {
|
|
|
|
96.0
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
font_size: default_font_size(),
|
|
|
|
dpi: default_dpi(),
|
|
|
|
font: TextStyle::default(),
|
2018-02-07 20:23:24 +03:00
|
|
|
font_rules: Vec::new(),
|
2018-02-10 19:16:20 +03:00
|
|
|
colors: None,
|
2018-02-10 20:43:54 +03:00
|
|
|
scrollback_lines: None,
|
2018-02-25 02:24:51 +03:00
|
|
|
hyperlink_rules: default_hyperlink_rules(),
|
2018-02-07 18:51:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents textual styling.
|
2018-02-07 20:23:24 +03:00
|
|
|
#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash)]
|
2018-02-07 18:51:04 +03:00
|
|
|
pub struct TextStyle {
|
|
|
|
/// A font config pattern to parse to locate the font.
|
|
|
|
/// Note that the dpi and current font_size for the terminal
|
|
|
|
/// will be set on the parsed result.
|
|
|
|
pub fontconfig_pattern: String,
|
2018-02-10 19:36:34 +03:00
|
|
|
|
|
|
|
/// If set, when rendering text that is set to the default
|
|
|
|
/// foreground color, use this color instead. This is most
|
|
|
|
/// useful in a `[[font_rules]]` section to implement changing
|
|
|
|
/// the text color for eg: bold text.
|
|
|
|
pub foreground: Option<RgbColor>,
|
2018-02-07 18:51:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TextStyle {
|
|
|
|
fn default() -> Self {
|
2018-02-10 19:36:34 +03:00
|
|
|
Self {
|
|
|
|
fontconfig_pattern: "monospace".into(),
|
|
|
|
foreground: None,
|
|
|
|
}
|
2018-02-07 18:51:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 19:07:24 +03:00
|
|
|
impl TextStyle {
|
2018-03-01 19:18:19 +03:00
|
|
|
/// Make a version of this style with bold enabled.
|
|
|
|
/// Semi-lame: we just append fontconfig style settings
|
|
|
|
/// to the string blindly. We could get more involved
|
|
|
|
/// but it would mean adding in the fontsystem stuff here
|
|
|
|
/// and this is probably good enough.
|
2018-03-01 19:07:24 +03:00
|
|
|
fn make_bold(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
fontconfig_pattern: format!("{}:weight=bold", self.fontconfig_pattern),
|
|
|
|
foreground: self.foreground,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 19:18:19 +03:00
|
|
|
/// Make a version of this style with italic enabled.
|
|
|
|
/// Semi-lame: we just append fontconfig style settings
|
|
|
|
/// to the string blindly. We could get more involved
|
|
|
|
/// but it would mean adding in the fontsystem stuff here
|
|
|
|
/// and this is probably good enough.
|
2018-03-01 19:07:24 +03:00
|
|
|
fn make_italic(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
fontconfig_pattern: format!("{}:style=Italic", self.fontconfig_pattern),
|
|
|
|
foreground: self.foreground,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 20:23:24 +03:00
|
|
|
/// Defines a rule that can be used to select a TextStyle given
|
|
|
|
/// an input CellAttributes value. The logic that applies the
|
|
|
|
/// matching can be found in src/font/mod.rs. The concept is that
|
|
|
|
/// the user can specify something like this:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// [[font_rules]]
|
|
|
|
/// italic = true
|
|
|
|
/// font = { fontconfig_pattern = "Operator Mono SSm Lig:style=Italic" }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The above is translated as: "if the CellAttributes have the italic bit
|
|
|
|
/// set, then use the italic style of font rather than the default", and
|
|
|
|
/// stop processing further font rules.
|
2018-03-01 19:07:24 +03:00
|
|
|
#[derive(Debug, Default, Deserialize, Clone)]
|
2018-02-07 20:23:24 +03:00
|
|
|
pub struct StyleRule {
|
|
|
|
/// If present, this rule matches when CellAttributes::intensity holds
|
|
|
|
/// a value that matches this rule. Valid values are "Bold", "Normal",
|
|
|
|
/// "Half".
|
|
|
|
pub intensity: Option<term::Intensity>,
|
|
|
|
/// If present, this rule matches when CellAttributes::underline holds
|
|
|
|
/// a value that matches this rule. Valid values are "None", "Single",
|
|
|
|
/// "Double".
|
|
|
|
pub underline: Option<term::Underline>,
|
|
|
|
/// If present, this rule matches when CellAttributes::italic holds
|
|
|
|
/// a value that matches this rule.
|
|
|
|
pub italic: Option<bool>,
|
|
|
|
/// If present, this rule matches when CellAttributes::blink holds
|
|
|
|
/// a value that matches this rule.
|
|
|
|
pub blink: Option<bool>,
|
|
|
|
/// If present, this rule matches when CellAttributes::reverse holds
|
|
|
|
/// a value that matches this rule.
|
|
|
|
pub reverse: Option<bool>,
|
|
|
|
/// If present, this rule matches when CellAttributes::strikethrough holds
|
|
|
|
/// a value that matches this rule.
|
|
|
|
pub strikethrough: Option<bool>,
|
|
|
|
/// If present, this rule matches when CellAttributes::invisible holds
|
|
|
|
/// a value that matches this rule.
|
|
|
|
pub invisible: Option<bool>,
|
|
|
|
|
|
|
|
/// When this rule matches, `font` specifies the styling to be used.
|
|
|
|
pub font: TextStyle,
|
|
|
|
}
|
|
|
|
|
2018-02-07 18:51:04 +03:00
|
|
|
impl Config {
|
|
|
|
pub fn load() -> Result<Self, Error> {
|
2018-02-21 09:08:19 +03:00
|
|
|
let home = std::env::home_dir().ok_or_else(|| format_err!("can't find home dir"))?;
|
2018-02-07 18:51:04 +03:00
|
|
|
|
|
|
|
let paths = [
|
|
|
|
home.join(".config").join("wezterm").join("wezterm.toml"),
|
|
|
|
home.join(".wezterm.toml"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for p in paths.iter() {
|
|
|
|
let mut file = match fs::File::open(p) {
|
|
|
|
Ok(file) => file,
|
2018-02-21 09:08:19 +03:00
|
|
|
Err(err) => match err.kind() {
|
|
|
|
std::io::ErrorKind::NotFound => continue,
|
|
|
|
_ => bail!("Error opening {}: {:?}", p.display(), err),
|
|
|
|
},
|
2018-02-07 18:51:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
file.read_to_string(&mut s)?;
|
|
|
|
|
2018-03-01 19:07:24 +03:00
|
|
|
let cfg: Self = toml::from_str(&s)
|
|
|
|
.map_err(|e| format_err!("Error parsing TOML from {}: {:?}", p.display(), e))?;
|
|
|
|
return Ok(cfg.compute_extra_defaults());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self::default().compute_extra_defaults())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// In some cases we need to compute expanded values based
|
|
|
|
/// on those provided by the user. This is where we do that.
|
|
|
|
fn compute_extra_defaults(&self) -> Self {
|
|
|
|
let mut cfg = self.clone();
|
|
|
|
|
|
|
|
if cfg.font_rules.len() == 0 {
|
|
|
|
// Expand out some reasonable default font rules
|
|
|
|
let bold = self.font.make_bold();
|
|
|
|
let italic = self.font.make_italic();
|
|
|
|
let bold_italic = bold.make_italic();
|
|
|
|
|
|
|
|
cfg.font_rules.push(StyleRule {
|
|
|
|
italic: Some(true),
|
|
|
|
font: italic,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
cfg.font_rules.push(StyleRule {
|
|
|
|
intensity: Some(term::Intensity::Bold),
|
|
|
|
font: bold,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
cfg.font_rules.push(StyleRule {
|
|
|
|
italic: Some(true),
|
|
|
|
intensity: Some(term::Intensity::Bold),
|
|
|
|
font: bold_italic,
|
|
|
|
..Default::default()
|
|
|
|
});
|
2018-02-07 18:51:04 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 19:07:24 +03:00
|
|
|
cfg
|
2018-02-07 18:51:04 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-10 19:16:20 +03:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct Palette {
|
|
|
|
/// The text color to use when the attributes are reset to default
|
2018-02-10 19:36:34 +03:00
|
|
|
pub foreground: Option<RgbColor>,
|
2018-02-10 19:16:20 +03:00
|
|
|
/// The background color to use when the attributes are reset to default
|
2018-02-10 19:36:34 +03:00
|
|
|
pub background: Option<RgbColor>,
|
2018-02-10 19:16:20 +03:00
|
|
|
/// The color of the cursor
|
2018-02-23 18:52:14 +03:00
|
|
|
pub cursor_fg: Option<RgbColor>,
|
|
|
|
pub cursor_bg: Option<RgbColor>,
|
|
|
|
/// The color of selected text
|
|
|
|
pub selection_fg: Option<RgbColor>,
|
|
|
|
pub selection_bg: Option<RgbColor>,
|
2018-02-10 19:16:20 +03:00
|
|
|
/// A list of 8 colors corresponding to the basic ANSI palette
|
2018-02-10 19:36:34 +03:00
|
|
|
pub ansi: Option<[RgbColor; 8]>,
|
2018-02-10 19:16:20 +03:00
|
|
|
/// A list of 8 colors corresponding to bright versions of the
|
|
|
|
/// ANSI palette
|
2018-02-10 19:36:34 +03:00
|
|
|
pub brights: Option<[RgbColor; 8]>,
|
2018-02-10 19:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Palette> for term::color::ColorPalette {
|
|
|
|
fn from(cfg: Palette) -> term::color::ColorPalette {
|
|
|
|
let mut p = term::color::ColorPalette::default();
|
2018-02-23 18:52:14 +03:00
|
|
|
macro_rules! apply_color {
|
|
|
|
($name:ident) => {
|
|
|
|
if let Some($name) = cfg.$name {
|
|
|
|
p.$name = $name;
|
|
|
|
}
|
|
|
|
};
|
2018-02-10 19:16:20 +03:00
|
|
|
}
|
2018-02-23 18:52:14 +03:00
|
|
|
apply_color!(foreground);
|
|
|
|
apply_color!(background);
|
|
|
|
apply_color!(cursor_fg);
|
|
|
|
apply_color!(cursor_bg);
|
|
|
|
apply_color!(selection_fg);
|
|
|
|
apply_color!(selection_bg);
|
|
|
|
|
2018-02-10 19:16:20 +03:00
|
|
|
if let Some(ansi) = cfg.ansi {
|
|
|
|
for (idx, col) in ansi.iter().enumerate() {
|
2018-02-25 04:28:35 +03:00
|
|
|
p.colors.0[idx] = *col;
|
2018-02-10 19:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(brights) = cfg.brights {
|
|
|
|
for (idx, col) in brights.iter().enumerate() {
|
2018-02-25 04:28:35 +03:00
|
|
|
p.colors.0[idx + 8] = *col;
|
2018-02-10 19:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p
|
|
|
|
}
|
|
|
|
}
|