diff options
author | Kirill Chibisov <contact@kchibisov.com> | 2022-07-10 20:11:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-10 20:11:28 +0300 |
commit | 694a52bcffeffdc9e163818c3b2ac5c39e26f1ef (patch) | |
tree | a774babc1869b4c700d7df1478dbbfe5b2c3bcda /alacritty/src/config/ui_config.rs | |
parent | 8451b75689b44f11ec1707af7e26d915772c3972 (diff) | |
download | r-alacritty-694a52bcffeffdc9e163818c3b2ac5c39e26f1ef.tar.gz r-alacritty-694a52bcffeffdc9e163818c3b2ac5c39e26f1ef.tar.bz2 r-alacritty-694a52bcffeffdc9e163818c3b2ac5c39e26f1ef.zip |
Add support for hyperlink escape sequence
This commit adds support for hyperlink escape sequence
`OSC 8 ; params ; URI ST`. The configuration option responsible for
those is `hints.enabled.hyperlinks`.
Fixes #922.
Diffstat (limited to 'alacritty/src/config/ui_config.rs')
-rw-r--r-- | alacritty/src/config/ui_config.rs | 83 |
1 files changed, 80 insertions, 3 deletions
diff --git a/alacritty/src/config/ui_config.rs b/alacritty/src/config/ui_config.rs index 74b61923..28139d27 100644 --- a/alacritty/src/config/ui_config.rs +++ b/alacritty/src/config/ui_config.rs @@ -1,10 +1,11 @@ use std::cell::RefCell; +use std::fmt::{self, Formatter}; use std::path::PathBuf; use std::rc::Rc; use glutin::event::{ModifiersState, VirtualKeyCode}; use log::error; -use serde::de::Error as SerdeError; +use serde::de::{Error as SerdeError, MapAccess, Visitor}; use serde::{self, Deserialize, Deserializer}; use unicode_width::UnicodeWidthChar; @@ -236,6 +237,7 @@ impl Default for Hints { // Add URL hint by default when no other hint is present. let pattern = LazyRegexVariant::Pattern(String::from(URL_REGEX)); let regex = LazyRegex(Rc::new(RefCell::new(pattern))); + let content = HintContent::new(Some(regex), true); #[cfg(not(any(target_os = "macos", windows)))] let action = HintAction::Command(Program::Just(String::from("xdg-open"))); @@ -249,7 +251,7 @@ impl Default for Hints { Self { enabled: vec![Hint { - regex, + content, action, post_processing: true, mouse: Some(HintMouse { enabled: true, mods: Default::default() }), @@ -332,7 +334,8 @@ pub enum HintAction { #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] pub struct Hint { /// Regex for finding matches. - pub regex: LazyRegex, + #[serde(flatten)] + pub content: HintContent, /// Action executed when this hint is triggered. #[serde(flatten)] @@ -349,6 +352,80 @@ pub struct Hint { binding: Option<HintBinding>, } +#[derive(Default, Clone, Debug, PartialEq, Eq)] +pub struct HintContent { + /// Regex for finding matches. + pub regex: Option<LazyRegex>, + + /// Escape sequence hyperlinks. + pub hyperlinks: bool, +} + +impl HintContent { + pub fn new(regex: Option<LazyRegex>, hyperlinks: bool) -> Self { + Self { regex, hyperlinks } + } +} + +impl<'de> Deserialize<'de> for HintContent { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + struct HintContentVisitor; + impl<'a> Visitor<'a> for HintContentVisitor { + type Value = HintContent; + + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.write_str("a mapping") + } + + fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> + where + M: MapAccess<'a>, + { + let mut content = Self::Value::default(); + + while let Some((key, value)) = map.next_entry::<String, serde_yaml::Value>()? { + match key.as_str() { + "regex" => match Option::<LazyRegex>::deserialize(value) { + Ok(regex) => content.regex = regex, + Err(err) => { + error!( + target: LOG_TARGET_CONFIG, + "Config error: hint's regex: {}", err + ); + }, + }, + "hyperlinks" => match bool::deserialize(value) { + Ok(hyperlink) => content.hyperlinks = hyperlink, + Err(err) => { + error!( + target: LOG_TARGET_CONFIG, + "Config error: hint's hyperlinks: {}", err + ); + }, + }, + _ => (), + } + } + + // Require at least one of hyperlinks or regex trigger hint matches. + if content.regex.is_none() && !content.hyperlinks { + return Err(M::Error::custom( + "Config error: At least on of the hint's regex or hint's hyperlinks must \ + be set", + )); + } + + Ok(content) + } + } + + deserializer.deserialize_any(HintContentVisitor) + } +} + /// Binding for triggering a keyboard hint. #[derive(Deserialize, Copy, Clone, Debug, PartialEq, Eq)] pub struct HintBinding { |