Skip to content

Commit

Permalink
Add support for running commands on BEL
Browse files Browse the repository at this point in the history
Adds a new configuration option under `visual_bell` called
`bell_command` that reuses the command formatting of the keybindings.

This allows for things like playing a notification sound.
See alacritty#1528 (comment)

Closes alacritty#1528
  • Loading branch information
robertgzr committed Apr 10, 2019
1 parent 6518231 commit 1b2d517
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/config/mod.rs
Expand Up @@ -215,6 +215,9 @@ pub struct VisualBellConfig {
/// Visual bell flash color
#[serde(deserialize_with = "rgb_from_hex")]
color: Rgb,

#[serde(deserialize_with = "failure_default")]
bell_command: Option<CommandWrapper>,
}

impl Default for VisualBellConfig {
Expand All @@ -223,6 +226,7 @@ impl Default for VisualBellConfig {
animation: Default::default(),
duration: Default::default(),
color: default_visual_bell_color(),
bell_command: None,
}
}
}
Expand All @@ -249,6 +253,12 @@ impl VisualBellConfig {
pub fn color(&self) -> Rgb {
self.color
}

/// Visual bell command
#[inline]
pub fn bell_command(&self) -> Option<CommandWrapper> {
self.bell_command.clone()
}
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
Expand Down
18 changes: 17 additions & 1 deletion src/term/mod.rs
Expand Up @@ -25,7 +25,7 @@ use unicode_width::UnicodeWidthChar;
use crate::ansi::{
self, Attr, CharsetIndex, Color, CursorStyle, Handler, NamedColor, StandardCharset,
};
use crate::config::{Config, VisualBellAnimation};
use crate::config::{Config, VisualBellAnimation, CommandWrapper};
use crate::grid::{
BidirectionalIterator, DisplayIter, Grid, GridCell, IndexRegion, Indexed, Scroll,
ViewportPosition,
Expand All @@ -37,6 +37,7 @@ use crate::selection::{self, Locations, Selection};
use crate::term::cell::{Cell, Flags, LineLength};
use crate::term::color::Rgb;
use crate::url::{Url, UrlParser};
use crate::util::start_daemon;
use copypasta::{Clipboard, Load, Store};
use font::{self, Size};

Expand Down Expand Up @@ -632,6 +633,9 @@ pub struct VisualBell {

/// The last time the visual bell rang, if at all
start_time: Option<Instant>,

/// Command to run when bell is rang
bell_command: Option<CommandWrapper>,
}

fn cubic_bezier(p0: f64, p1: f64, p2: f64, p3: f64, x: f64) -> f64 {
Expand All @@ -647,6 +651,7 @@ impl VisualBell {
VisualBell {
animation: visual_bell_config.animation(),
duration: visual_bell_config.duration(),
bell_command: visual_bell_config.bell_command(),
start_time: None,
}
}
Expand Down Expand Up @@ -737,10 +742,20 @@ impl VisualBell {
}
}

pub fn run_bell_command(&self) {
if let Some(ref cmd) = self.bell_command {
match start_daemon(cmd.program(), cmd.args()) {
Ok(_) => debug!("Launched {} with args {:?} on bell", cmd.program(), cmd.args()),
Err(_) => warn!("Unable to launch {} with args {:?} on bell", cmd.program(), cmd.args()),
}
}
}

pub fn update_config(&mut self, config: &Config) {
let visual_bell_config = config.visual_bell();
self.animation = visual_bell_config.animation();
self.duration = visual_bell_config.duration();
self.bell_command = visual_bell_config.bell_command();
}
}

Expand Down Expand Up @@ -1708,6 +1723,7 @@ impl ansi::Handler for Term {
fn bell(&mut self) {
trace!("Bell");
self.visual_bell.ring();
self.visual_bell.run_bell_command();
self.next_is_urgent = Some(true);
}

Expand Down

0 comments on commit 1b2d517

Please sign in to comment.