mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
ls: extract "create_hyperlink" function
This commit is contained in:
parent
0fa074fcba
commit
8c6463c525
1 changed files with 29 additions and 25 deletions
|
@ -3047,31 +3047,7 @@ fn display_file_name(
|
|||
let mut width = name.width();
|
||||
|
||||
if config.hyperlink {
|
||||
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
|
||||
name = format!("\x1b]8;;file://{hostname}{absolute_path}\x07{name}\x1b]8;;\x07");
|
||||
name = create_hyperlink(&name, path);
|
||||
}
|
||||
|
||||
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.
|
||||
/// This because we need to check the previous value in case we don't need
|
||||
/// the reset
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue