|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use crate::{impl_display, osc, Command}; |
| 4 | + |
| 5 | +/// A command that starts an [OSC 8 hyperlink]. |
| 6 | +/// |
| 7 | +/// Text printed after this command will be a clickable hyperlink in |
| 8 | +/// supported terminals until [`EndHyperlink`] is printed. |
| 9 | +/// |
| 10 | +/// [OSC 8 hyperlink]: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda |
| 11 | +/// |
| 12 | +/// # Notes |
| 13 | +/// |
| 14 | +/// Commands must be executed/queued for execution otherwise they do nothing. |
| 15 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 16 | +pub struct StartHyperlink<T> { |
| 17 | + pub url: T, |
| 18 | + pub params: Vec<(String, String)>, |
| 19 | +} |
| 20 | + |
| 21 | +impl<T> StartHyperlink<T> |
| 22 | +where |
| 23 | + T: AsRef<str>, |
| 24 | +{ |
| 25 | + pub fn new(url: T) -> Self { |
| 26 | + Self { |
| 27 | + url, |
| 28 | + params: Vec::new(), |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + pub fn param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self { |
| 33 | + self.params.push((key.into(), value.into())); |
| 34 | + self |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<T> Command for StartHyperlink<T> |
| 39 | +where |
| 40 | + T: AsRef<str>, |
| 41 | +{ |
| 42 | + fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { |
| 43 | + // OSC 8 ; params ; uri ST |
| 44 | + // params are key=value pairs separated by ':' |
| 45 | + f.write_str("\x1B]8;")?; |
| 46 | + for (i, (k, v)) in self.params.iter().enumerate() { |
| 47 | + if i > 0 { |
| 48 | + f.write_char(':')?; |
| 49 | + } |
| 50 | + f.write_str(k)?; |
| 51 | + f.write_char('=')?; |
| 52 | + f.write_str(v)?; |
| 53 | + } |
| 54 | + f.write_char(';')?; |
| 55 | + f.write_str(self.url.as_ref())?; |
| 56 | + f.write_str("\x1B\\") |
| 57 | + } |
| 58 | + |
| 59 | + #[cfg(windows)] |
| 60 | + fn execute_winapi(&self) -> std::io::Result<()> { |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// A command that ends an [OSC 8 hyperlink]. |
| 66 | +/// |
| 67 | +/// [OSC 8 hyperlink]: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda |
| 68 | +/// |
| 69 | +/// # Notes |
| 70 | +/// |
| 71 | +/// Commands must be executed/queued for execution otherwise they do nothing. |
| 72 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 73 | +pub struct EndHyperlink; |
| 74 | + |
| 75 | +impl Command for EndHyperlink { |
| 76 | + fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { |
| 77 | + f.write_str(osc!("8;;")) |
| 78 | + } |
| 79 | + |
| 80 | + #[cfg(windows)] |
| 81 | + fn execute_winapi(&self) -> std::io::Result<()> { |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl_display!(for StartHyperlink<T> where T: AsRef<str>); |
| 87 | +impl_display!(for EndHyperlink); |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use std::borrow::Cow; |
| 92 | + |
| 93 | + use super::*; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn start_no_params() { |
| 97 | + let mut buf = String::new(); |
| 98 | + StartHyperlink::new("https://example.com") |
| 99 | + .write_ansi(&mut buf) |
| 100 | + .unwrap(); |
| 101 | + assert_eq!(buf, "\x1B]8;;https://example.com\x1B\\"); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn start_with_param() { |
| 106 | + let mut buf = String::new(); |
| 107 | + StartHyperlink::new("https://example.com") |
| 108 | + .param("id", "link1") |
| 109 | + .write_ansi(&mut buf) |
| 110 | + .unwrap(); |
| 111 | + assert_eq!(buf, "\x1B]8;id=link1;https://example.com\x1B\\"); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn start_with_multiple_params() { |
| 116 | + let mut buf = String::new(); |
| 117 | + StartHyperlink::new("https://example.com") |
| 118 | + .param("id", "link1") |
| 119 | + .param(String::from("foo"), String::from("bar")) |
| 120 | + .param(Cow::Borrowed("baz"), Cow::Borrowed("fuz")) |
| 121 | + .write_ansi(&mut buf) |
| 122 | + .unwrap(); |
| 123 | + assert_eq!( |
| 124 | + buf, |
| 125 | + "\x1B]8;id=link1:foo=bar:baz=fuz;https://example.com\x1B\\" |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn start_owned_string() { |
| 131 | + let mut buf = String::new(); |
| 132 | + StartHyperlink::new(String::from("https://example.com")) |
| 133 | + .param("id", "link1") |
| 134 | + .write_ansi(&mut buf) |
| 135 | + .unwrap(); |
| 136 | + assert_eq!(buf, "\x1B]8;id=link1;https://example.com\x1B\\"); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn start_cow() { |
| 141 | + let mut buf = String::new(); |
| 142 | + StartHyperlink::new(Cow::Borrowed("https://example.com")) |
| 143 | + .param("id", "link1") |
| 144 | + .write_ansi(&mut buf) |
| 145 | + .unwrap(); |
| 146 | + assert_eq!(buf, "\x1B]8;id=link1;https://example.com\x1B\\"); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn end_hyperlink() { |
| 151 | + let mut buf = String::new(); |
| 152 | + EndHyperlink.write_ansi(&mut buf).unwrap(); |
| 153 | + assert_eq!(buf, "\x1B]8;;\x1B\\"); |
| 154 | + } |
| 155 | +} |
0 commit comments