Skip to content

Commit 134defd

Browse files
committed
added better file searching that doesnt require -f
1 parent cd0baef commit 134defd

File tree

4 files changed

+108
-95
lines changed

4 files changed

+108
-95
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "natls"
3-
version = "1.2.5"
3+
version = "1.2.6"
44
authors = ["Will Lane <[email protected]>"]
55
description = "nat - the 'ls' replacement you never knew you needed"
66
license = "MIT"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ natls <dir>
9999
#### Searching for file
100100

101101
```bash
102-
natls <dir (leave empty if in wanted dir)> -f <file>
102+
natls <file>
103103
```
104104

105105
### To edit the code

src/main.rs

Lines changed: 105 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ pub struct Cli {
2121
#[structopt(parse(from_os_str), default_value = ".")]
2222
path: std::path::PathBuf,
2323

24-
/// File to search for
25-
#[structopt(short, long, default_value)]
26-
file: String,
27-
2824
/// Enables helper headline
2925
#[structopt(short = "l", long = "headline")]
3026
headline_on: bool,
@@ -85,26 +81,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8581
let time_format = &args.time_format;
8682
let colors_on = &args.colors_on;
8783

88-
let entries = fs::read_dir(directory)?
89-
.map(|res| res.map(|e| e.path()))
90-
.collect::<Result<Vec<_>, io::Error>>()?;
91-
92-
let mut size_count = 4;
93-
let mut group_size = 8;
94-
for s in &entries {
95-
if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count {
96-
size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len();
97-
};
98-
99-
let metadata_uid = fs::symlink_metadata(&s)?.uid();
100-
let user_name_len = get_user_name(metadata_uid).len();
101-
if user_name_len > group_size {
102-
group_size = user_name_len;
103-
}
104-
}
105-
106-
let mut found = false;
107-
10884
draw_headlines(
10985
*headline_on,
11086
*perms_on,
@@ -114,70 +90,105 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
11490
*user_on,
11591
);
11692

117-
if &args.file != "" {
118-
for e in &entries {
119-
if e
120-
.file_name()
121-
.unwrap()
122-
.to_str()
123-
.unwrap()
124-
.to_lowercase()
125-
.contains(&args.file.to_lowercase())
126-
{
127-
let _ = single(e, size_count, *wide_mode, time_format);
128-
found = true;
93+
let mut singly_found = false;
94+
if !std::path::Path::new(directory).exists() {
95+
let entries = fs::read_dir(".")?
96+
.map(|res| res.map(|e| e.path()))
97+
.collect::<Result<Vec<_>, io::Error>>()?;
98+
99+
let mut size_count = 4;
100+
for s in &entries {
101+
if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count {
102+
size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len();
103+
};
129104
}
130-
}
131-
if !found {
132-
if !*colors_on {
133-
print!("{}", color::Fg(color::Red));
134-
}
135-
println!(
136-
"{}",
137-
Style::new()
138-
.bold()
139-
.paint(format!("{} could not be found", &args.file))
140-
);
141-
}
105+
for e in &entries {
106+
if e
107+
.file_name()
108+
.unwrap()
109+
.to_str()
110+
.unwrap()
111+
.to_lowercase()
112+
.contains(&args.path.display().to_string().to_lowercase())
113+
{
114+
let _ = single(e, size_count, *wide_mode, time_format);
115+
singly_found = true;
116+
}
117+
}
118+
if !singly_found {
119+
if !*colors_on {
120+
print!("{}", color::Fg(color::Red));
121+
}
122+
println!(
123+
"{}",
124+
Style::new()
125+
.bold()
126+
.paint(format!("{} could not be found", &args.path.display().to_string()))
127+
);
128+
}
129+
std::process::exit(1);
130+
}
131+
132+
if !directory.symlink_metadata()?.is_dir() {
133+
let _ = single(&directory, 4 as usize, *wide_mode, time_format);
142134
std::process::exit(0);
143135
}
144136

145-
let mut dirs: Vec<&std::path::PathBuf> = vec![];
146137

147-
for e in &entries {
148-
if !&e.file_name().unwrap().to_str().unwrap().starts_with(".") || *hidden_files {
149-
if *&e.is_file() && !*is_sorted {
150-
dirs.push(*&e);
151-
} else {
152-
if !perms_on {
153-
let _ = file_perms(&e);
154-
}
138+
let entries = fs::read_dir(directory)?
139+
.map(|res| res.map(|e| e.path()))
140+
.collect::<Result<Vec<_>, io::Error>>()?;
155141

156-
if !size_on {
157-
let _ = file_size(size_count, &e);
158-
}
142+
let mut size_count = 4;
143+
let mut group_size = 8;
144+
for s in &entries {
145+
if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count {
146+
size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len();
147+
};
148+
149+
let metadata_uid = fs::symlink_metadata(&s)?.uid();
150+
let user_name_len = get_user_name(metadata_uid).len();
151+
if user_name_len > group_size {
152+
group_size = user_name_len;
153+
}
154+
}
159155

160-
if !time_on {
161-
let _ = time_mod(e, time_format);
162-
}
156+
let mut dirs: Vec<&std::path::PathBuf> = vec![];
163157

164-
if !group_on {
165-
let _ = show_group_name(e);
166-
}
158+
for e in &entries {
159+
if !&e.file_name().unwrap().to_str().unwrap().starts_with(".") || *hidden_files {
160+
if *&e.is_file() && !*is_sorted {
161+
dirs.push(*&e);
162+
} else {
163+
if !perms_on {
164+
let _ = file_perms(&e);
165+
}
167166

168-
if !user_on {
169-
let _ = show_user_name(e);
170-
}
167+
if !size_on {
168+
let _ = file_size(size_count, &e);
169+
}
171170

172-
let _ = show_file_name(&e, *wide_mode);
171+
if !time_on {
172+
let _ = time_mod(e, time_format);
173+
}
174+
175+
if !group_on {
176+
let _ = show_group_name(e);
177+
}
178+
179+
if !user_on {
180+
let _ = show_user_name(e);
181+
}
182+
183+
let _ = show_file_name(&e, *wide_mode);
184+
}
173185
}
174186
}
175-
}
176-
for e in &dirs {
177-
let _ = single(e, size_count, *wide_mode, time_format);
178-
}
187+
for e in &dirs {
188+
let _ = single(e, size_count, *wide_mode, time_format);
189+
}
179190

180-
Ok(())
191+
Ok(())
181192
}
182193

183194
pub fn draw_headlines(
@@ -222,17 +233,19 @@ pub fn file_size(
222233
size_count: usize,
223234
e: &&std::path::PathBuf,
224235
) -> Result<(), Box<dyn std::error::Error>> {
225-
for _ in 0..(size_count - convert(fs::symlink_metadata(&e)?.size() as f64).len()) {
226-
print!(" ");
236+
if size_count > 4 {
237+
for _ in 0..(size_count - convert(fs::symlink_metadata(&e)?.size() as f64).len()) {
238+
print!(" ");
239+
}
227240
}
228241
if !Cli::from_args().colors_on {
229242
print!("{}", color::Fg(color::Green));
230243
}
231244
print!(
232245
"{} ",
233246
Style::new()
234-
.bold()
235-
.paint(convert(fs::symlink_metadata(&e)?.size() as f64))
247+
.bold()
248+
.paint(convert(fs::symlink_metadata(&e)?.size() as f64))
236249
);
237250
Ok(())
238251
}
@@ -264,7 +277,7 @@ pub fn show_group_name(e: &std::path::PathBuf) -> Result<(), Box<dyn std::error:
264277
match get_group_by_gid(fs::symlink_metadata(e)?.gid()).as_ref() {
265278
Some(n) => n.name().to_str().unwrap(),
266279
None => "",
267-
}
280+
}
268281
)
269282
);
270283
Ok(())
@@ -280,7 +293,7 @@ pub fn show_user_name(e: &std::path::PathBuf) -> Result<(), Box<dyn std::error::
280293
match get_user_by_uid(fs::symlink_metadata(e)?.uid()).as_ref() {
281294
Some(n) => n.name().to_str().unwrap(),
282295
None => "",
283-
}
296+
}
284297
)
285298
);
286299
Ok(())
@@ -311,8 +324,8 @@ pub fn show_file_name(
311324
print!(
312325
"{} -> ",
313326
Style::new()
314-
.bold()
315-
.paint(e.file_name().unwrap().to_str().unwrap())
327+
.bold()
328+
.paint(e.file_name().unwrap().to_str().unwrap())
316329
);
317330
match fs::canonicalize(fs::read_link(e)?) {
318331
Ok(_n) => {
@@ -322,9 +335,9 @@ pub fn show_file_name(
322335
print!(
323336
"{}/",
324337
fs::canonicalize(fs::read_link(e)?)
325-
.unwrap()
326-
.to_str()
327-
.unwrap()
338+
.unwrap()
339+
.to_str()
340+
.unwrap()
328341
)
329342
}
330343
} else {
@@ -333,9 +346,9 @@ pub fn show_file_name(
333346
print!(
334347
"{}",
335348
fs::canonicalize(fs::read_link(e)?)
336-
.unwrap()
337-
.to_str()
338-
.unwrap()
349+
.unwrap()
350+
.to_str()
351+
.unwrap()
339352
)
340353
}
341354
}
@@ -372,8 +385,8 @@ pub fn show_file_name(
372385
print!(
373386
"{}",
374387
Style::new()
375-
.bold()
376-
.paint(e.file_name().unwrap().to_str().unwrap())
388+
.bold()
389+
.paint(e.file_name().unwrap().to_str().unwrap())
377390
);
378391
if !wide_mode {
379392
print!("\n");
@@ -448,7 +461,7 @@ pub fn perms(mode: u16) -> String {
448461
format!("{}{}", color::Fg(color::LightRed), group),
449462
format!("{}{}", color::Fg(color::Yellow), other),
450463
]
451-
.join("")
464+
.join("")
452465
} else {
453466
[user, group, other].join("")
454467
}

0 commit comments

Comments
 (0)