Skip to content

Commit

Permalink
update name to totally avoid strings
Browse files Browse the repository at this point in the history
  • Loading branch information
0jdxt committed Mar 25, 2021
1 parent b660e8a commit c62a1dd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Expand Up @@ -25,7 +25,6 @@ dirs = "3.0.*"
libc = "0.2.*"
human-sort = "0.2.2"
term_grid = "0.1.*"
terminal_size = "0.1.*"
chrono = "0.4.*"
chrono-humanize = "0.1.*"
unicode-width = "0.1.*"
Expand All @@ -34,10 +33,10 @@ wild = "2.0.*"
globset = "0.4.*"
xdg = "2.1.*"
yaml-rust = "0.4.*"
termize = "0.1"
fxhash = "0.2.*"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
fxhash = "0.2.*"
termize = "0.1.1"

[target.'cfg(unix)'.dependencies]
users = "0.11.*"
Expand Down
10 changes: 4 additions & 6 deletions src/core.rs
Expand Up @@ -9,21 +9,19 @@ use std::path::PathBuf;
pub struct Core {
flags: Flags,
icons: Icons,
//display: Display,
colors: Colors,
sorters: Vec<(SortOrder, sort::SortFn)>,
}

impl Core {
pub fn new(flags: Flags) -> Self {
// termsize allows us to know if the stdout is a tty or not.
// termize allows us to know if the stdout is a tty or not.
let tty_available = termize::dimensions().is_some();

#[cfg(not(target_os = "windows"))]
let console_color_ok = true;

#[cfg(target_os = "windows")]
#[cfg(windows)]
let console_color_ok = ansi_term::enable_ansi_support().is_ok();
#[cfg(unix)]
let console_color_ok = true;

let mut inner_flags = flags.clone();

Expand Down
4 changes: 2 additions & 2 deletions src/meta/mod.rs
Expand Up @@ -83,11 +83,11 @@ impl Meta {

if Display::All == flags.display && flags.layout != Layout::Tree {
let mut current_meta = self.clone();
current_meta.name.set_name(".");
current_meta.name.set_current_dir();

let mut parent_meta =
Self::from_path(&self.path.join(Component::ParentDir), flags.dereference.0)?;
parent_meta.name.set_name("..");
parent_meta.name.set_parent_dir();

content.push(current_meta);
content.push(parent_meta);
Expand Down
19 changes: 14 additions & 5 deletions src/meta/name.rs
Expand Up @@ -13,10 +13,15 @@ pub enum DisplayOption<'a> {
None,
}

static CUR_DIR: &str = ".";
static PAR_DIR: &str = "..";

#[derive(Clone, Debug, Eq)]
pub struct Name {
// name is an option since we want to use the Cow<str> from path until we actually need a newly allocated String. See set_name and get_name for more
name: Option<String>,
// `name` is an option since we want to use the Cow from `path`.
// The only time we set the `name` to be different is for . and ..
// see set_current_dir, set_parent_dir and get_name.
name: Option<Cow<'static, str>>,
path: PathBuf,
file_type: FileType,
}
Expand All @@ -34,13 +39,17 @@ impl Name {
self.path.extension().map(|e| e.to_string_lossy())
}

pub fn set_name(&mut self, new_name: &str) {
self.name = Some(new_name.to_owned());
pub fn set_current_dir(&mut self) {
self.name = Some(Cow::from(CUR_DIR));
}

pub fn set_parent_dir(&mut self) {
self.name = Some(Cow::from(PAR_DIR));
}

pub fn get_name(&self) -> Cow<'_, str> {
match &self.name {
Some(name) => Cow::from(name),
Some(name) => name.clone(),
None => match self.path.file_name() {
Some(name) => name.to_string_lossy(),
None => self.path.to_string_lossy(),
Expand Down

0 comments on commit c62a1dd

Please sign in to comment.