Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
//! OSC 8 hyperlink support for terminal output.
use color_print::cformat;
use osc8::Hyperlink;
// Re-export for direct use
pub use supports_hyperlinks::{Stream, on as supports_hyperlinks};
/// Render `text` as an underlined OSC 8 hyperlink to `url`.
///
/// Every link worktrunk emits goes through here, so the underline is a
/// property of being a link rather than a rule each call site remembers. Link
/// text is sized to fit a column (`#3604`, `:11486`) and reads as ordinary
/// content, and color is already spoken for by state (the CI verdict, dim for
/// a port nothing answers on), so the underline is what marks the text as
/// clickable.
///
/// Closing with `[24m` rather than a full reset keeps a surrounding color or
/// dim intact, so a caller can wrap the result in either without the link
/// punching a hole in it.
pub fn hyperlink(url: &str, text: &str) -> String {
cformat!(
"<underline>{}{text}{}</>",
Hyperlink::new(url),
Hyperlink::END
)
}
/// Strip OSC 8 hyperlinks while preserving other ANSI sequences (colors).
///
/// OSC 8 terminal hyperlinks are great for terminal output (clickable links!), but can cause
/// issues in other contexts:
/// - **Markdown output**: terminal control bytes are not portable source and can render as
/// garbage text
/// - **Test output**: PTY tests using vt100 parser may show hyperlinks as garbage characters
/// (like `^D`) in rendered terminal output
///
/// Clap's `unstable-markdown` feature (added in 4.5.28) converts markdown links like
/// `[text](url)` in doc comments to OSC 8 terminal hyperlinks. Git on some platforms
/// (notably macOS) also generates OSC 8 hyperlinks in diff output.
///
/// There's no runtime toggle to disable OSC 8 generation. Environment variables like
/// `FORCE_HYPERLINK`, `NO_COLOR`, and `TERM=dumb` have no effect because hyperlink
/// generation is separate from `supports-hyperlinks` and `anstyle-query`.
///
/// Uses the `osc8` crate's parser to find hyperlinks and extract just the visible text.
pub fn strip_osc8_hyperlinks(s: &str) -> String {
// Fast path — callers routinely pass strings with no hyperlinks (skeleton
// rows, plain log output). Skip the parse-and-rebuild work entirely.
if !s.contains("\x1b]8;") {
return s.to_string();
}
let mut result = s.to_string();
// Keep parsing and removing hyperlinks until none remain
while let Ok(Some((_, range))) = Hyperlink::parse(&result) {
// The range covers the opening escape sequence only.
// We need to find and remove the closing sequence too.
let after_open = range.end;
// Find the closing sequence (empty hyperlink that ends the link)
if let Ok(Some((_, close_range))) = Hyperlink::parse(&result[after_open..]) {
// Extract the text between opening and closing sequences
let text_between = result[after_open..after_open + close_range.start].to_string();
// Replace the entire hyperlink (open + text + close) with just the text
let full_end = after_open + close_range.end;
result.replace_range(range.start..full_end, &text_between);
} else {
// Malformed hyperlink (no closing sequence) - just remove the opening
result.replace_range(range.clone(), "");
break;
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
/// The underline brackets the link and closes with `[24m`, so a caller's
/// color or dim survives the link untouched.
#[test]
fn test_hyperlink_underlines_the_link_text() {
let linked = hyperlink("https://example.com", "#123");
assert_eq!(
linked,
format!(
"\u{1b}[4m{}#123{}\u{1b}[24m",
Hyperlink::new("https://example.com"),
Hyperlink::END
)
);
assert_eq!(
strip_osc8_hyperlinks(&linked),
"\u{1b}[4m#123\u{1b}[24m",
"stripping the link leaves the underline and nothing else"
);
}
#[test]
fn test_strip_osc8_hyperlinks_removes_hyperlink() {
// OSC 8 format: ESC ] 8 ; params ; URL ST TEXT ESC ] 8 ; ; ST
// ST can be ESC \ or BEL (\x07)
let url = "https://example.com";
let text = "link text";
let input = format!(
"before {}{}{}after",
Hyperlink::new(url),
text,
Hyperlink::END
);
let result = strip_osc8_hyperlinks(&input);
assert_eq!(result, "before link textafter");
}
#[test]
fn test_strip_osc8_hyperlinks_preserves_sgr_codes() {
// SGR codes (colors) like ESC [ 0 m should be preserved
let url = "https://example.com";
let text = "link";
let input = format!(
"\u{1b}[1m{}{}{}bold\u{1b}[0m",
Hyperlink::new(url),
text,
Hyperlink::END
);
let result = strip_osc8_hyperlinks(&input);
assert_eq!(result, "\u{1b}[1mlinkbold\u{1b}[0m");
}
#[test]
fn test_strip_osc8_hyperlinks_handles_no_hyperlinks() {
let input = "plain text with no hyperlinks";
let result = strip_osc8_hyperlinks(input);
assert_eq!(result, input);
}
#[test]
fn test_strip_osc8_hyperlinks_handles_multiple() {
let input = format!(
"{}first{} and {}second{}",
Hyperlink::new("url1"),
Hyperlink::END,
Hyperlink::new("url2"),
Hyperlink::END
);
let result = strip_osc8_hyperlinks(&input);
assert_eq!(result, "first and second");
}
}