use std::io::{self, Write};
pub const HYPERLINK_RESET: &[u8] = b"\x1b]8;;\x1b\\";
pub fn write_hyperlink<W: Write>(w: &mut W, url: &str, params: &str) -> io::Result<()> {
write!(w, "\x1b]8;{params};{url}\x1b\\")
}
pub fn parse_hyperlink(body: &[u8]) -> Option<(&str, &str)> {
let rest = body.strip_prefix(b"8;")?;
let sep = rest.iter().position(|&b| b == b';')?;
let params = std::str::from_utf8(&rest[..sep]).ok()?;
let url = std::str::from_utf8(&rest[sep + 1..]).ok()?;
Some((params, url))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hyperlink() {
let mut buf = Vec::new();
write_hyperlink(&mut buf, "https://example.com", "").unwrap();
assert_eq!(buf, b"\x1b]8;;https://example.com\x1b\\");
}
#[test]
fn parse_basic() {
let (params, url) = parse_hyperlink(b"8;;https://example.com").unwrap();
assert_eq!(params, "");
assert_eq!(url, "https://example.com");
}
#[test]
fn parse_with_params() {
let (params, url) = parse_hyperlink(b"8;id=abc;https://x").unwrap();
assert_eq!(params, "id=abc");
assert_eq!(url, "https://x");
}
#[test]
fn parse_close() {
let (params, url) = parse_hyperlink(b"8;;").unwrap();
assert_eq!(params, "");
assert_eq!(url, "");
}
#[test]
fn parse_url_with_semicolons() {
let (params, url) = parse_hyperlink(b"8;id=1;https://x?a=1;b=2").unwrap();
assert_eq!(params, "id=1");
assert_eq!(url, "https://x?a=1;b=2");
}
#[test]
fn parse_rejects_wrong_prefix() {
assert!(parse_hyperlink(b"0;title").is_none());
assert!(parse_hyperlink(b"").is_none());
}
#[test]
fn parse_rejects_missing_separator() {
assert!(parse_hyperlink(b"8;noseparator").is_none());
}
#[test]
fn parse_rejects_invalid_utf8() {
assert!(parse_hyperlink(b"8;\xff;url").is_none());
assert!(parse_hyperlink(b"8;;\xff").is_none());
}
}