Skip to content

Commit 9abab3f

Browse files
hemreariPeltoche
authored andcommitted
add Display enum
1 parent e33cde9 commit 9abab3f

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

src/core.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ impl Core {
7878
match Meta::from_path_recursive(
7979
&fs::canonicalize(&absolute_path.to_path_buf()).unwrap(),
8080
depth,
81-
self.flags.display_all,
82-
self.flags.display_almost_all,
81+
self.flags.display,
8382
) {
8483
Ok(meta) => meta_list.push(meta),
8584
Err(err) => println!("cannot access '{}': {}", path.display(), err),

src/flags.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use clap::{ArgMatches, Error, ErrorKind};
22

33
#[derive(Clone, Debug, Copy)]
44
pub struct Flags {
5-
pub display_all: bool,
6-
pub display_almost_all: bool,
5+
pub display: Display,
76
pub layout: Layout,
87
pub display_indicators: bool,
98
pub recursive: bool,
@@ -28,6 +27,14 @@ impl Flags {
2827
let date_inputs: Vec<&str> = matches.values_of("date").unwrap().collect();
2928
let dir_order_inputs: Vec<&str> = matches.values_of("group-dirs").unwrap().collect();
3029

30+
let display = if matches.is_present("all") {
31+
Display::DisplayAll
32+
} else if matches.is_present("almost-all") {
33+
Display::DisplayAlmostAll
34+
} else {
35+
Display::DisplayOnlyVisible
36+
};
37+
3138
let sort_by = if matches.is_present("timesort") {
3239
SortFlag::Time
3340
} else {
@@ -68,8 +75,7 @@ impl Flags {
6875
};
6976

7077
Ok(Self {
71-
display_all: matches.is_present("all"),
72-
display_almost_all: matches.is_present("almost-all"),
78+
display,
7379
layout,
7480
display_indicators: matches.is_present("indicators"),
7581
recursive,
@@ -106,16 +112,15 @@ impl Flags {
106112
impl Default for Flags {
107113
fn default() -> Self {
108114
Self {
109-
display_all: false,
110-
display_almost_all: false,
115+
display: Display::DisplayOnlyVisible,
111116
layout: Layout::Grid,
112117
display_indicators: false,
113118
recursive: false,
114119
recursion_depth: usize::max_value(),
115120
sort_by: SortFlag::Name,
116121
sort_order: SortOrder::Default,
117122
directory_order: DirOrderFlag::None,
118-
size: SizeFlag::Default,
123+
size: SizeFlag::Default,
119124
date: DateFlag::Date,
120125
color: WhenFlag::Auto,
121126
icon: WhenFlag::Auto,
@@ -124,6 +129,13 @@ impl Default for Flags {
124129
}
125130
}
126131

132+
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
133+
pub enum Display {
134+
DisplayAll,
135+
DisplayAlmostAll,
136+
DisplayOnlyVisible,
137+
}
138+
127139
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
128140
pub enum SizeFlag {
129141
Default,

src/meta/mod.rs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use self::owner::Owner;
1515
pub use self::permissions::Permissions;
1616
pub use self::size::Size;
1717
pub use self::symlink::SymLink;
18+
pub use crate::flags::Display;
1819
pub use crate::icon::Icons;
1920

2021
use std::fs::read_link;
@@ -39,8 +40,7 @@ impl Meta {
3940
pub fn from_path_recursive(
4041
path: &PathBuf,
4142
depth: usize,
42-
list_hidden_files: bool,
43-
list_almost_all: bool,
43+
display: Display,
4444
) -> Result<Self, std::io::Error> {
4545
let mut meta = Self::from_path(path)?;
4646

@@ -59,44 +59,49 @@ impl Meta {
5959
}
6060
let mut content = Vec::new();
6161

62-
if list_hidden_files {
63-
let mut current_meta;
64-
let mut parent_meta;
62+
match display {
63+
Display::DisplayAll => {
64+
let mut current_meta;
65+
let mut parent_meta;
6566

66-
let parent_path = match path.parent() {
67-
None => PathBuf::from("/"),
68-
Some(path) => PathBuf::from(path),
69-
};
67+
let parent_path = match path.parent() {
68+
None => PathBuf::from("/"),
69+
Some(path) => PathBuf::from(path),
70+
};
7071

71-
current_meta = Self::from_path(&PathBuf::from(path))?;
72-
current_meta.name.name = ".".to_string();
72+
current_meta = Self::from_path(&PathBuf::from(path))?;
73+
current_meta.name.name = ".".to_string();
7374

74-
parent_meta = Self::from_path(&PathBuf::from(parent_path))?;
75-
parent_meta.name.name = "..".to_string();
75+
parent_meta = Self::from_path(&PathBuf::from(parent_path))?;
76+
parent_meta.name.name = "..".to_string();
7677

77-
content.push(current_meta);
78-
content.push(parent_meta);
79-
}
78+
content.push(current_meta);
79+
content.push(parent_meta);
80+
}
81+
_ => (),
82+
};
8083

8184
for entry in meta.path.read_dir()? {
8285
let path = entry?.path();
8386

84-
if !list_hidden_files
85-
&& !list_almost_all
86-
&& path
87-
.file_name()
88-
.ok_or_else(|| Error::new(ErrorKind::InvalidInput, "invalid file name"))?
89-
.to_string_lossy()
90-
.starts_with('.')
91-
{
92-
continue;
93-
}
87+
match display {
88+
Display::DisplayOnlyVisible => {
89+
if path
90+
.file_name()
91+
.ok_or_else(|| Error::new(ErrorKind::InvalidInput, "invalid file name"))?
92+
.to_string_lossy()
93+
.starts_with('.')
94+
{
95+
continue;
96+
}
97+
}
98+
_ => (),
99+
};
94100

95101
let entry_meta = match Self::from_path_recursive(
96102
&path.to_path_buf(),
97103
depth - 1,
98-
list_hidden_files,
99-
list_almost_all,
104+
display,
100105
) {
101106
Ok(res) => res,
102107
Err(err) => {

0 commit comments

Comments
 (0)