forked from sharkdp/fd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: sharkdp#1295 Fixes: sharkdp#1563
- Loading branch information
Showing
11 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::filesystem::absolute_path; | ||
use std::fmt::{self, Formatter, Write}; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::OnceLock; | ||
|
||
pub(crate) struct PathUrl(PathBuf); | ||
|
||
#[cfg(unix)] | ||
static HOSTNAME: OnceLock<String> = OnceLock::new(); | ||
|
||
impl PathUrl { | ||
pub(crate) fn new(path: &Path) -> Option<PathUrl> { | ||
Some(PathUrl(absolute_path(path).ok()?)) | ||
} | ||
} | ||
|
||
impl fmt::Display for PathUrl { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "file://{}", host())?; | ||
let bytes = self.0.as_os_str().as_encoded_bytes(); | ||
for &byte in bytes.iter() { | ||
encode(f, byte)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn encode(f: &mut Formatter, byte: u8) -> fmt::Result { | ||
match byte { | ||
b'0'..=b'9' | ||
| b'A'..=b'Z' | ||
| b'a'..=b'z' | ||
| b'/' | ||
| b':' | ||
| b'-' | ||
| b'.' | ||
| b'_' | ||
| b'~' | ||
| 128.. => f.write_char(byte.into()), | ||
#[cfg(windows)] | ||
b'\\' => f.write_char('/'), | ||
_ => { | ||
write!(f, "%{:X}", byte) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
fn host() -> &'static str { | ||
HOSTNAME | ||
.get_or_init(|| { | ||
nix::unistd::gethostname() | ||
.ok() | ||
.and_then(|h| h.into_string().ok()) | ||
.unwrap_or_default() | ||
}) | ||
.as_ref() | ||
} | ||
|
||
#[cfg(not(unix))] | ||
const fn host() -> &'static str { | ||
"" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters