mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
refac: use Lazy cell to optimize env::current_dir call
This commit is contained in:
parent
b2c87c5307
commit
86234084ab
@ -14,13 +14,19 @@ pub mod utils;
|
|||||||
/// CLI argparsing definitions, using `clap`.
|
/// CLI argparsing definitions, using `clap`.
|
||||||
pub mod opts;
|
pub mod opts;
|
||||||
|
|
||||||
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
use error::{Error, Result};
|
use error::{Error, Result};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use opts::{Opts, Subcommand};
|
use opts::{Opts, Subcommand};
|
||||||
use utils::{QuestionAction, QuestionPolicy};
|
use utils::{QuestionAction, QuestionPolicy};
|
||||||
|
|
||||||
// Used in BufReader and BufWriter to perform less syscalls
|
// Used in BufReader and BufWriter to perform less syscalls
|
||||||
const BUFFER_CAPACITY: usize = 1024 * 32;
|
const BUFFER_CAPACITY: usize = 1024 * 32;
|
||||||
|
|
||||||
|
/// Current directory or empty directory
|
||||||
|
static CURRENT_DIRECTORY: Lazy<PathBuf> = Lazy::new(|| env::current_dir().unwrap_or_default());
|
||||||
|
|
||||||
/// The status code returned from `ouch` on error
|
/// The status code returned from `ouch` on error
|
||||||
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
|
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
use std::{borrow::Cow, cmp, env, path::Path};
|
use std::{borrow::Cow, cmp, path::Path};
|
||||||
|
|
||||||
|
use crate::CURRENT_DIRECTORY;
|
||||||
|
|
||||||
/// Converts an OsStr to utf8 with custom formatting.
|
/// Converts an OsStr to utf8 with custom formatting.
|
||||||
///
|
///
|
||||||
@ -17,12 +19,7 @@ pub fn to_utf(os_str: &Path) -> Cow<str> {
|
|||||||
/// Removes the current dir from the beginning of a path as it's redundant information,
|
/// Removes the current dir from the beginning of a path as it's redundant information,
|
||||||
/// useful for presentation sake.
|
/// useful for presentation sake.
|
||||||
pub fn strip_cur_dir(source_path: &Path) -> &Path {
|
pub fn strip_cur_dir(source_path: &Path) -> &Path {
|
||||||
let current_dir = env::current_dir();
|
let current_dir = &*CURRENT_DIRECTORY;
|
||||||
|
|
||||||
let current_dir = match ¤t_dir {
|
|
||||||
Ok(inner) => inner.as_path(),
|
|
||||||
Err(_) => Path::new(""),
|
|
||||||
};
|
|
||||||
|
|
||||||
source_path.strip_prefix(current_dir).unwrap_or(source_path)
|
source_path.strip_prefix(current_dir).unwrap_or(source_path)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user