1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

ls: extract "create_hyperlink" function

This commit is contained in:
Daniel Hofstetter 2023-12-18 14:24:19 +01:00
parent 0fa074fcba
commit 8c6463c525

View file

@ -3047,31 +3047,7 @@ fn display_file_name(
let mut width = name.width(); let mut width = name.width();
if config.hyperlink { if config.hyperlink {
let hostname = hostname::get().unwrap_or(OsString::from("")); name = create_hyperlink(&name, path);
let hostname = hostname.to_string_lossy();
let absolute_path = fs::canonicalize(&path.p_buf).unwrap_or_default();
let absolute_path = absolute_path.to_string_lossy();
#[cfg(not(target_os = "windows"))]
let unencoded_chars = "_-.:~/";
#[cfg(target_os = "windows")]
let unencoded_chars = "_-.:~/\\";
// percentage encoding of path
let absolute_path: String = absolute_path
.chars()
.map(|c| {
if c.is_alphanumeric() || unencoded_chars.contains(c) {
c.to_string()
} else {
format!("%{:02x}", c as u8)
}
})
.collect();
// \x1b = ESC, \x07 = BEL
name = format!("\x1b]8;;file://{hostname}{absolute_path}\x07{name}\x1b]8;;\x07");
} }
if let Some(ls_colors) = &config.color { if let Some(ls_colors) = &config.color {
@ -3208,6 +3184,34 @@ fn display_file_name(
} }
} }
fn create_hyperlink(name: &str, path: &PathData) -> String {
let hostname = hostname::get().unwrap_or(OsString::from(""));
let hostname = hostname.to_string_lossy();
let absolute_path = fs::canonicalize(&path.p_buf).unwrap_or_default();
let absolute_path = absolute_path.to_string_lossy();
#[cfg(not(target_os = "windows"))]
let unencoded_chars = "_-.:~/";
#[cfg(target_os = "windows")]
let unencoded_chars = "_-.:~/\\";
// percentage encoding of path
let absolute_path: String = absolute_path
.chars()
.map(|c| {
if c.is_alphanumeric() || unencoded_chars.contains(c) {
c.to_string()
} else {
format!("%{:02x}", c as u8)
}
})
.collect();
// \x1b = ESC, \x07 = BEL
format!("\x1b]8;;file://{hostname}{absolute_path}\x07{name}\x1b]8;;\x07")
}
/// We need this struct to be able to store the previous style. /// We need this struct to be able to store the previous style.
/// This because we need to check the previous value in case we don't need /// This because we need to check the previous value in case we don't need
/// the reset /// the reset