Skip to content

Commit

Permalink
Convert URLs in the about text into hyperlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Sep 15, 2024
1 parent afbc210 commit e2d7cd0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/config/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::enums::{DetailField, SortField, Typ, UnitSys};
use crate::fmt::render;
use crate::utils::urls::get_osc;
use clap::Parser;
use log::warn;
use regex::bytes::{Regex, RegexBuilder};
Expand Down Expand Up @@ -32,12 +33,16 @@ fn regex_parser(s: &str) -> Result<Regex, RegexError> {
author,
version,
about = render("<red bold>`pls`</> is a prettier and powerful `ls` for the pros."),
long_about = render([
"<red bold>`pls`</> is a prettier and powerful `ls` for the pros.\n",
"<bold>Read docs:</> https://pls.cli.rs/",
"<bold>Get source:</> https://github.com/pls-rs/pls",
"<bold>Sponsor:</> https://github.com/sponsors/dhruvkb"
].join("\n")),
long_about = render(format!(
"<red bold>`pls`</> is a prettier and powerful `ls` for the pros.
<bold>Read docs:</> {}
<bold>Get source:</> {}
<bold>Sponsor:</> {}",
get_osc("https://pls.cli.rs/", None),
get_osc("https://github.com/pls-rs/pls/", None),
get_osc("https://github.com/sponsors/dhruvkb/", None),
)),
args_override_self = true,
)]
pub struct Args {
Expand Down
2 changes: 2 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//! can contain any number of utility functions.
//!
//! * [`paths`]
//! * [`urls`]
//! * [`vectors`]
pub mod paths;
pub mod urls;
pub mod vectors;
46 changes: 46 additions & 0 deletions src/utils/urls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! This module contains code for working with URLs.
//!
//! The public interface of the module consists of one function:
//!
//! * [`get_osc`]
use std::fmt::Display;

/// Get the OSC-8 escape sequence for the given URL.
///
/// Many terminal emulators support OSC-8, which allows terminals to
/// render hyperlinks that have a display text that points to a URL
/// which is not displayed.
///
/// # Arguments
///
/// * `url` - the URL to generate the escape sequence for
/// * `text` - the text to display for the hyperlink
pub fn get_osc<S>(url: S, text: Option<S>) -> String
where
S: AsRef<str> + Display,
{
let text = text.as_ref().unwrap_or(&url);
format!("\x1b]8;;{url}\x1b\\{text}\x1b]8;;\x1b\\")
}

#[cfg(test)]
mod tests {
use super::get_osc;

macro_rules! make_test {
( $($name:ident: $url:expr, $text:expr => $expected:expr,)* ) => {
$(
#[test]
fn $name() {
assert_eq!(get_osc($url, $text), $expected);
}
)*
};
}

make_test!(
test_url_and_test: "https://example.com", Some("Example") => "\x1b]8;;https://example.com\x1b\\Example\x1b]8;;\x1b\\",
test_url_only: "https://example.com", None => "\x1b]8;;https://example.com\x1b\\https://example.com\x1b]8;;\x1b\\",
);
}

0 comments on commit e2d7cd0

Please sign in to comment.