Skip to content

Commit

Permalink
Plumb OSC 777 to toast notifications
Browse files Browse the repository at this point in the history
```
echo -e "\033]777;notify;This is the notification title;This is the notification text\a"
```

Now pops up a notification in a similar manner to OSC 9, except
that this form allows setting both the title and the body separately.

refs: #489
  • Loading branch information
wez committed Feb 19, 2021
1 parent 84a45f2 commit f0c163e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions term/src/terminalstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3192,6 +3192,27 @@ impl<'a> Performer<'a> {
log::info!("Application sends SystemNotification: {}", message);
}
}
OperatingSystemCommand::RxvtExtension(params) => {
if let Some("notify") = params.get(0).map(String::as_str) {
let title = params.get(1);
let body = params.get(2);
let (title, body) = match (title.cloned(), body.cloned()) {
(Some(title), None) => (None, title),
(Some(title), Some(body)) => (Some(title), body),
_ => {
log::error!("malformed rxvt notify escape: {:?}", params);
return;
}
};
if let Some(handler) = self.notification_handler.as_mut() {
handler.show_notification(ToastNotification {
title,
body,
focus: true,
});
}
}
}
OperatingSystemCommand::CurrentWorkingDirectory(url) => {
self.current_dir = Url::parse(&url).ok();
}
Expand Down
24 changes: 24 additions & 0 deletions termwiz/src/escape/osc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum OperatingSystemCommand {
ResetDynamicColor(DynamicColorNumber),
CurrentWorkingDirectory(String),
ResetColors(Vec<u8>),
RxvtExtension(Vec<String>),

Unspecified(Vec<Vec<u8>>),
}
Expand Down Expand Up @@ -300,6 +301,13 @@ impl OperatingSystemCommand {
ITermProprietary => {
self::ITermProprietary::parse(osc).map(OperatingSystemCommand::ITermProprietary)
}
RxvtProprietary => {
let mut vec = vec![];
for slice in osc.iter().skip(1) {
vec.push(String::from_utf8_lossy(slice).to_string());
}
Ok(OperatingSystemCommand::RxvtExtension(vec))
}
FinalTermSemanticPrompt => self::FinalTermSemanticPrompt::parse(osc)
.map(OperatingSystemCommand::FinalTermSemanticPrompt),
ChangeColorNumber => Self::parse_change_color_number(osc),
Expand Down Expand Up @@ -473,6 +481,7 @@ impl Display for OperatingSystemCommand {
SetIconNameSun(title) => single_string!(SetIconNameSun, title),
SetHyperlink(Some(link)) => link.fmt(f)?,
SetHyperlink(None) => write!(f, "8;;")?,
RxvtExtension(params) => write!(f, "777;{}", params.join(";"))?,
Unspecified(v) => {
for (idx, item) in v.iter().enumerate() {
if idx > 0 {
Expand Down Expand Up @@ -1466,6 +1475,21 @@ mod test {
);
}

#[test]
fn rxvt() {
assert_eq!(
parse(
&["777", "notify", "alert user", "the tea is ready"],
"\x1b]777;notify;alert user;the tea is ready\x1b\\"
),
OperatingSystemCommand::RxvtExtension(vec![
"notify".into(),
"alert user".into(),
"the tea is ready".into()
]),
)
}

#[test]
fn iterm() {
assert_eq!(
Expand Down

0 comments on commit f0c163e

Please sign in to comment.