mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-08 12:35:41 +00:00
Merge pull request #156 from ouch-org/cur-dir-cow
Avoid allocating in `nice_directory_display` when possible, make `Extension` non-exhaustive
This commit is contained in:
commit
e1378b3ac6
@ -6,6 +6,7 @@ use self::CompressionFormat::*;
|
|||||||
|
|
||||||
/// A wrapper around `CompressionFormat` that allows combinations like `tgz`
|
/// A wrapper around `CompressionFormat` that allows combinations like `tgz`
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub struct Extension {
|
pub struct Extension {
|
||||||
/// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
|
/// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
|
||||||
pub compression_formats: &'static [CompressionFormat],
|
pub compression_formats: &'static [CompressionFormat],
|
||||||
@ -35,7 +36,7 @@ impl Extension {
|
|||||||
|
|
||||||
impl fmt::Display for Extension {
|
impl fmt::Display for Extension {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.write_str(&self.display_text)
|
self.display_text.fmt(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
src/utils.rs
11
src/utils.rs
@ -1,6 +1,7 @@
|
|||||||
//! Random stuff used on ouch.
|
//! Random stuff used on ouch.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
cmp, env,
|
cmp, env,
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
io,
|
io,
|
||||||
@ -109,12 +110,12 @@ pub fn concatenate_list_of_os_str(os_strs: &[impl AsRef<OsStr>]) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Display the directory name, but change to "current directory" when necessary.
|
/// Display the directory name, but change to "current directory" when necessary.
|
||||||
pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> String {
|
pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> Cow<'static, str> {
|
||||||
let text = to_utf(os_str);
|
if os_str.as_ref() == "." {
|
||||||
if text == "." {
|
Cow::Borrowed("current directory")
|
||||||
"current directory".to_string()
|
|
||||||
} else {
|
} else {
|
||||||
format!("'{}'", text)
|
let text = to_utf(os_str);
|
||||||
|
Cow::Owned(format!("'{}'", text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user