Skip to content

Commit 7e36f60

Browse files
committed
added ability to search for file
1 parent 3282768 commit 7e36f60

3 files changed

Lines changed: 150 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ nat <dir>
3232
#### Searching for file
3333

3434
```bash
35-
nat <dir> | grep "<filename>"
35+
nat <dir (leave empty if in wanted dir)> -f <file>
3636
```
3737
3838
### To edit the code

src/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ use std::{fs, io};
77
use structopt::StructOpt;
88
use termion::color;
99
use users::{get_current_uid, get_user_by_uid};
10+
mod single;
1011

1112
#[derive(StructOpt, Debug)]
1213
#[structopt(name = "nat", about = "the ls replacement you never knew you needed")]
1314
struct Cli {
1415
#[structopt(parse(from_os_str), default_value = ".", help = "Give me a directory")]
1516
path: std::path::PathBuf,
17+
18+
#[structopt(default_value = "", short = "f", long = "file", help = "File to search for")]
19+
file: String,
1620
}
1721

1822
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -22,13 +26,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2226
let entries = fs::read_dir(directory)?
2327
.map(|res| res.map(|e| e.path()))
2428
.collect::<Result<Vec<_>, io::Error>>()?;
29+
2530
let mut size_count = 0;
2631
for s in &entries {
2732
if convert(fs::metadata(&s)?.size() as f64).len() > size_count {
2833
size_count = convert(fs::metadata(&s)?.size() as f64).len();
2934
};
3035
}
3136

37+
if &args.file != "" {
38+
for e in &entries {
39+
if e.file_name().unwrap().to_str().unwrap() == &args.file {
40+
let _ = single::single(e, size_count);
41+
std::process::exit(1)
42+
}
43+
}
44+
print!("File could not be found");
45+
std::process::exit(1)
46+
}
47+
3248
print!("{}", Style::new().underline().paint("permissions"));
3349
for _ in 0..2 {
3450
print!("{}", Style::new().underline().paint(" "))

src/single.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
extern crate pretty_bytes;
2+
use ansi_term::Style;
3+
use chrono::{DateTime, Utc};
4+
use pretty_bytes::converter::convert;
5+
use std::os::unix::fs::MetadataExt;
6+
use std::{fs};
7+
use termion::color;
8+
use users::{get_current_uid, get_user_by_uid};
9+
10+
pub fn single(e: &std::path::PathBuf, size_count: usize) -> Result<(), Box<dyn std::error::Error>> {
11+
print!("{}", Style::new().underline().paint("permissions"));
12+
for _ in 0..2 {
13+
print!("{}", Style::new().underline().paint(" "))
14+
}
15+
print!(" {}", Style::new().underline().paint("size"));
16+
for _ in 0..(size_count - 4) {
17+
print!("{}", Style::new().underline().paint(" "))
18+
}
19+
20+
print!(" {}", Style::new().underline().paint("modified"));
21+
22+
for _ in 0..11 {
23+
print!("{}", Style::new().underline().paint(" "))
24+
}
25+
26+
print!(" {}", Style::new().underline().paint("user"));
27+
28+
for _ in 0..(get_user_by_uid(get_current_uid())
29+
.unwrap()
30+
.name()
31+
.to_str()
32+
.unwrap()
33+
.len()
34+
- 4)
35+
{
36+
print!("{}", Style::new().underline().paint(" "))
37+
}
38+
39+
print!(" {}", Style::new().underline().paint("name"));
40+
41+
print!("\n");
42+
43+
let meta = fs::metadata(&e)?;
44+
let mode = meta.mode();
45+
let user_has_write_access = mode & 0o200;
46+
let user_has_read_write_access = mode & 0o600;
47+
let group_has_read_access = mode & 0o040;
48+
let others_have_exec_access = mode & 0o001;
49+
let mut mode_count = 0;
50+
if user_has_write_access == 128 {
51+
print!("{}", color::Fg(color::Red));
52+
print!("w");
53+
print!("{}", color::Fg(color::White));
54+
print!("-");
55+
mode_count += 2;
56+
}
57+
if user_has_read_write_access == 384 {
58+
print!("{}", color::Fg(color::LightYellow));
59+
print!("r");
60+
print!("{}", color::Fg(color::LightRed));
61+
print!("w");
62+
print!("{}", color::Fg(color::White));
63+
print!("-");
64+
mode_count += 3;
65+
}
66+
if group_has_read_access == 32 {
67+
print!("{}", color::Fg(color::Green));
68+
print!("x");
69+
print!("{}", color::Fg(color::LightYellow));
70+
print!("a");
71+
print!("{}", color::Fg(color::White));
72+
print!("-");
73+
mode_count += 3;
74+
}
75+
if others_have_exec_access == 1 {
76+
print!("{}", color::Fg(color::Yellow));
77+
print!("xw");
78+
print!("{}", color::Fg(color::White));
79+
print!("-");
80+
mode_count += 3;
81+
}
82+
print!("{}", color::Fg(color::White));
83+
print!("-@");
84+
mode_count += 2;
85+
for _ in 0..(13 - mode_count) {
86+
print!(" ")
87+
}
88+
89+
for _ in 0..(size_count - convert(fs::metadata(&e)?.size() as f64).len()) {
90+
print!(" ")
91+
}
92+
print!("{}", color::Fg(color::Green));
93+
print!(
94+
" {}",
95+
Style::new()
96+
.bold()
97+
.paint(convert(fs::metadata(&e)?.size() as f64))
98+
);
99+
100+
if let Ok(time) = e.metadata()?.modified() {
101+
print!("{}", color::Fg(color::LightRed));
102+
let datetime: DateTime<Utc> = time.into();
103+
print!(" {} ", datetime.format("%d-%m-%Y"));
104+
print!("{}", datetime.format("%T"))
105+
}
106+
107+
print!("{}", color::Fg(color::Yellow));
108+
print!(
109+
" {} ",
110+
Style::new().bold().paint(
111+
get_user_by_uid(get_current_uid())
112+
.unwrap()
113+
.name()
114+
.to_str()
115+
.unwrap()
116+
)
117+
);
118+
119+
print!("{}", color::Fg(color::White));
120+
if e.metadata()?.is_dir() {
121+
print!("{}", color::Fg(color::LightBlue));
122+
println!("{}/", &e.file_name().unwrap().to_str().unwrap());
123+
} else {
124+
print!("{}", color::Fg(color::LightGreen));
125+
println!(
126+
"{}",
127+
Style::new()
128+
.bold()
129+
.paint(e.file_name().unwrap().to_str().unwrap())
130+
);
131+
}
132+
Ok(())
133+
}

0 commit comments

Comments
 (0)