Skip to content

Commit c577baa

Browse files
committed
made it more clear how the cli works with structopt
1 parent c7b2ad9 commit c577baa

3 files changed

Lines changed: 199 additions & 21 deletions

File tree

Cargo.lock

Lines changed: 164 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nat"
3-
version = "0.1.0"
3+
version = "1.0.3"
44
authors = ["Will Lane <williamlane923@gmail.com>"]
55
edition = "2018"
66

@@ -12,3 +12,4 @@ chrono = "*"
1212
users = "*"
1313
pretty-bytes = "*"
1414
ansi_term = "*"
15+
structopt = "*"

src/main.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1+
extern crate pretty_bytes;
2+
use ansi_term::Style;
13
use chrono::{DateTime, Utc};
4+
use pretty_bytes::converter::convert;
25
use std::os::unix::fs::MetadataExt;
3-
use std::{env, fs, io};
6+
use std::{fs, io};
7+
use structopt::StructOpt;
48
use termion::color;
59
use users::{get_current_uid, get_user_by_uid};
6-
extern crate pretty_bytes;
7-
use ansi_term::Style;
8-
use pretty_bytes::converter::convert;
910

10-
fn main() -> io::Result<()> {
11-
let args: Vec<String> = env::args().collect();
12-
let mut directory = ".";
13-
if args.len() > 1 {
14-
directory = &args[1]
15-
}
11+
#[derive(StructOpt, Debug)]
12+
struct Cli {
13+
#[structopt(default_value = ".")]
14+
path: std::path::PathBuf,
15+
}
16+
17+
fn main() -> Result<(), Box<dyn std::error::Error>> {
18+
let args = Cli::from_args();
19+
let directory = &args.path;
1620

1721
let entries = fs::read_dir(directory)?
1822
.map(|res| res.map(|e| e.path()))
1923
.collect::<Result<Vec<_>, io::Error>>()?;
20-
2124
let mut size_count = 0;
2225
for s in &entries {
2326
if convert(fs::metadata(&s)?.size() as f64).len() > size_count {
@@ -108,7 +111,12 @@ fn main() -> io::Result<()> {
108111
print!(" ")
109112
}
110113
print!("{}", color::Fg(color::Green));
111-
print!(" {}", Style::new().bold().paint(convert(fs::metadata(&e)?.size() as f64)));
114+
print!(
115+
" {}",
116+
Style::new()
117+
.bold()
118+
.paint(convert(fs::metadata(&e)?.size() as f64))
119+
);
112120

113121
if let Ok(time) = e.metadata()?.modified() {
114122
print!("{}", color::Fg(color::LightRed));
@@ -120,11 +128,13 @@ fn main() -> io::Result<()> {
120128
print!("{}", color::Fg(color::Yellow));
121129
print!(
122130
" {} ",
123-
Style::new().bold().paint(get_user_by_uid(get_current_uid())
124-
.unwrap()
125-
.name()
126-
.to_str()
127-
.unwrap())
131+
Style::new().bold().paint(
132+
get_user_by_uid(get_current_uid())
133+
.unwrap()
134+
.name()
135+
.to_str()
136+
.unwrap()
137+
)
128138
);
129139

130140
print!("{}", color::Fg(color::White));
@@ -133,7 +143,12 @@ fn main() -> io::Result<()> {
133143
println!("{}/", &e.file_name().unwrap().to_str().unwrap());
134144
} else {
135145
print!("{}", color::Fg(color::LightGreen));
136-
println!("{}", Style::new().bold().paint(e.file_name().unwrap().to_str().unwrap()));
146+
println!(
147+
"{}",
148+
Style::new()
149+
.bold()
150+
.paint(e.file_name().unwrap().to_str().unwrap())
151+
);
137152
}
138153
}
139154
Ok(())

0 commit comments

Comments
 (0)