terminal_link/
lib.rs

1use std::fmt;
2
3/// A clickable link component.
4#[derive(Clone, Debug, Eq, Hash, PartialEq)]
5pub struct Link<'a> {
6    pub id: &'a str,
7    pub text: &'a str,
8    pub url: &'a str,
9}
10
11impl<'a> Link<'a> {
12    /// Create a new link with a name and target url.
13    pub fn new(text: &'a str, url: &'a str) -> Self {
14        Self { text, url, id: "" }
15    }
16
17    /// Create a new link with a name, a target url and an id.
18    pub fn with_id(text: &'a str, url: &'a str, id: &'a str) -> Self {
19        Self { text, url, id }
20    }
21}
22
23impl fmt::Display for Link<'_> {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        if self.id != "" {
26            write!(
27                f,
28                "\u{1b}]8;id={};{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\",
29                self.id, self.url, self.text
30            )
31        } else {
32            write!(
33                f,
34                "\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\",
35                self.url, self.text
36            )
37        }
38    }
39}