| 1 |
//! Experimental support for hyperlinking. |
| 2 |
//! |
| 3 |
//! # Usage |
| 4 |
//! |
| 5 |
//! Enable the `hyperlink` crate feature. Enabling `hyperlink` implicitly |
| 6 |
//! enables `std`. |
| 7 |
//! |
| 8 |
//! Import the `HyperlinkExt` extension trait and use the [`link()`] builder |
| 9 |
//! method. |
| 10 |
//! |
| 11 |
//! [`link()`]: HyperlinkExt::link() |
| 12 |
//! |
| 13 |
//! ```rust |
| 14 |
//! use yansi::Paint; |
| 15 |
//! use yansi::hyperlink::HyperlinkExt; |
| 16 |
//! |
| 17 |
//! println!("Go to {}.", "our docs".link("https://docs.rs/yansi").green()); |
| 18 |
//! ``` |
| 19 |
//! |
| 20 |
//! `>` Go to <a href="https://docs.rs/yansi"><span style="color: green;">our docs</span></a>. |
| 21 |
//! |
| 22 |
//! The `link()` method returns a [`PaintedLink`] structure which implements all |
| 23 |
//! of the unverisal chainable methods available across the library. |
| 24 |
//! Furthermore, [`Painted`] is extended with a [`link()`](Painted::link()) |
| 25 |
//! method. The net effect is that you can use `link()` as if it were any other |
| 26 |
//! styling method: |
| 27 |
//! |
| 28 |
//! ```rust |
| 29 |
//! use yansi::Paint; |
| 30 |
//! use yansi::hyperlink::HyperlinkExt; |
| 31 |
//! |
| 32 |
//! println!("Go to {}.", "our docs".green().link("https://docs.rs/yansi").on_black().invert()); |
| 33 |
//! ``` |
| 34 |
//! |
| 35 |
//! `>` Go to <a href="https://docs.rs/yansi"> |
| 36 |
//! <span style="background: green; color: black;">our docs</span> |
| 37 |
//! </a>. |
| 38 |
//! |
| 39 |
//! # Caveats |
| 40 |
//! |
| 41 |
//! 1. You can only create a link when there is a target value to print, that |
| 42 |
//! is, when the receiver is something "printable". In other words, you |
| 43 |
//! _cannot_ apply `link()` to a bare `Style`. This means the following will |
| 44 |
//! not work: |
| 45 |
//! |
| 46 |
//! ```rust,compile_fail |
| 47 |
//! use yansi::{Paint, Style, Color::*}; |
| 48 |
//! use yansi::hyperlink::HyperlinkExt; |
| 49 |
//! |
| 50 |
//! static LINKED: Style = Green.link("https://docs.rs/yansi"); |
| 51 |
//! ``` |
| 52 |
//! <br/> |
| 53 |
//! |
| 54 |
//! 2. While some modern terminals support hyperlinking, many do not. Those that |
| 55 |
//! do not _should_ gracefully ignore the target URL and print the original |
| 56 |
//! value. That is, instead of `>` <a href="https://docs.rs/yansi">our |
| 57 |
//! docs</a>, such terminals would print `>` our docs. |
| 58 |
use core::fmt; |
| 59 |
|
| 60 |
use crate::*; |
| 61 |
|
| 62 |
/// A [`Painted`] with an associated target URL to hyperlink. |
| 63 |
pub struct PaintedLink<T> { |
| 64 |
painted: Painted<T>, |
| 65 |
link: String, |
| 66 |
} |
| 67 |
|
| 68 |
/// Extension trait to apply hyperlinks to any value, implemented for all types. |
| 69 |
/// |
| 70 |
/// See the [module level docs](hyperlink) for usage details. |
| 71 |
pub trait HyperlinkExt { |
| 72 |
/// Create a painted hyperlink with a target URL of `url`. |
| 73 |
/// |
| 74 |
/// See [`hyperlink`] for details. |
| 75 |
/// |
| 76 |
/// # Example |
| 77 |
/// |
| 78 |
/// ```rust |
| 79 |
/// use yansi::hyperlink::HyperlinkExt; |
| 80 |
/// |
| 81 |
/// println!("See {}.", "our docs".link("https://docs.rs/yansi")); |
| 82 |
/// ``` |
| 83 |
fn link(&self, url: impl ToString) -> PaintedLink<&Self>; |
| 84 |
} |
| 85 |
|
| 86 |
impl<T> PaintedLink<T> { |
| 87 |
fn fmt_args( |
| 88 |
&self, |
| 89 |
fmt: &dyn Fn(&Painted<T>, &mut fmt::Formatter) -> fmt::Result, |
| 90 |
f: &mut fmt::Formatter, |
| 91 |
_args: fmt::Arguments<'_>, |
| 92 |
) -> fmt::Result { |
| 93 |
if !self.painted.enabled() { |
| 94 |
return fmt(&self.painted, f); |
| 95 |
} |
| 96 |
|
| 97 |
write!(f, "\x1B]8;;{}\x1B\\", self.link)?; |
| 98 |
fmt(&self.painted, f)?; |
| 99 |
write!(f, "\x1B]8;;\x1B\\") |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
impl_fmt_traits!(<T> PaintedLink<T> => self.painted (Painted<T>)); |
| 104 |
|
| 105 |
impl<T> HyperlinkExt for T { |
| 106 |
fn link(&self, url: impl ToString) -> PaintedLink<&Self> { |
| 107 |
PaintedLink { painted: Painted::new(self), link: url.to_string() } |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/// Experimental support for hyperlinking. |
| 112 |
impl<T> Painted<T> { |
| 113 |
/// Create a painted hyperlink with a target URL of `url`. |
| 114 |
/// |
| 115 |
/// See [`hyperlink`] for details. |
| 116 |
/// |
| 117 |
/// # Example |
| 118 |
/// |
| 119 |
/// ```rust |
| 120 |
/// use yansi::Paint; |
| 121 |
/// use yansi::hyperlink::HyperlinkExt; |
| 122 |
/// |
| 123 |
/// println!("See {}.", "our docs".green().link("https://docs.rs/yansi")); |
| 124 |
/// ``` |
| 125 |
pub fn link(&self, url: impl ToString) -> PaintedLink<&Self> { |
| 126 |
PaintedLink { painted: Painted::new(self), link: url.to_string() } |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl<T> PaintedLink<T> { |
| 131 |
#[inline(always)] |
| 132 |
const fn apply(mut self, a: crate::style::Application) -> Self { |
| 133 |
self.painted.style = self.painted.style.apply(a); |
| 134 |
self |
| 135 |
} |
| 136 |
|
| 137 |
properties!([pub const] constructor(Self) -> Self); |
| 138 |
} |