Skip to content

Commit f1c59d1

Browse files
committed
output
1 parent 9dfdd45 commit f1c59d1

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

cli/grrs/src/main.rs

+43-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// use std::env;
21
use std::fs::{self, File};
32
use std::io::{self, BufReader, prelude::*};
43
use std::path::Path;
54
use structopt::StructOpt;
65

76
// Search for a pattern in a file and display the lines that contain it.
8-
#[derive(StructOpt)]
7+
#[derive(StructOpt, Debug)]
98
struct Cli {
109
/// The pattern to look for
1110
pattern: String,
@@ -15,16 +14,40 @@ struct Cli {
1514
}
1615

1716
fn main() {
17+
eprintln!("This is an error! :(");
1818
let args = Cli::from_args();
19-
if let Ok(lines) = read_line(&args.path) {
20-
for line in lines {
21-
if let Ok(_line) = line {
22-
if _line.contains(&args.pattern) {
23-
println!("{:?}", _line);
19+
println!("======= args =======");
20+
println!("{:#?}", args);
21+
// Option 1.
22+
let result = read_line(&args.path);
23+
match result {
24+
Ok(content) => {
25+
// println!("file content: {:?}", content);
26+
for (index, line) in content.enumerate() {
27+
if let Ok(_line) = line {
28+
if _line.contains(&args.pattern) {
29+
println!("======= match line [{}] =======", index);
30+
println!("{}", _line);
31+
}
2432
}
2533
}
26-
}
34+
},
35+
Err(err) => println!("Oh noes: {}", err)
2736
}
37+
38+
// read_content(&args.path);
39+
40+
// // Option 2.
41+
// if let Ok(lines) = read_line(&args.path) {
42+
// for (index, line) in lines.enumerate() {
43+
// if let Ok(_line) = line {
44+
// if _line.contains(&args.pattern) {
45+
// println!("======= match line [{}] =======", index);
46+
// println!("{}", _line);
47+
// }
48+
// }
49+
// }
50+
// }
2851
}
2952

3053
/// @see: https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html
@@ -36,6 +59,7 @@ where P: AsRef<Path> {
3659

3760
// fn main() {
3861
// let args = Cli::from_args();
62+
// // let mut content = fs::read_to_string(&args.path)?;
3963
// let mut content = fs::read_to_string(&args.path).expect("could not read file");
4064

4165
// for line in content.lines() {
@@ -44,3 +68,14 @@ where P: AsRef<Path> {
4468
// }
4569
// }
4670
// }
71+
72+
fn read_content<P>(filename: P) -> Result<(), Box<dyn std::error::Error>>
73+
where P: AsRef<Path> {
74+
let result = fs::read_to_string(filename);
75+
let content = match result {
76+
Ok(content) => { content },
77+
Err(err) => { return Err(err.into()); },
78+
};
79+
// println!("file content: {}", content);
80+
Ok(())
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "output_for_humans_and_machines"
3+
version = "0.1.0"
4+
authors = ["lencx <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
indicatif = "0.14.0"
11+
log = "0.4.0"
12+
env_logger = "0.7.1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[macro_use]
2+
extern crate log;
3+
4+
fn main() {
5+
// RUST_LOG=info cargo run --bin output-log
6+
env_logger::init();
7+
info!("starting up");
8+
warn!("oops, nothing implemented!");
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::io::{self, Write};
2+
3+
fn main() {
4+
// get the global stdout entity
5+
let stdout = io::stdout();
6+
7+
// acquire a lock on it
8+
let mut handle = stdout.lock();
9+
writeln!(handle, "foo: {}", 42);
10+
11+
let pb = indicatif::ProgressBar::new(100);
12+
for i in 0..100 {
13+
pb.println(format!("[+] finished #{}", i));
14+
pb.inc(1);
15+
}
16+
pb.finish_with_message("done");
17+
}

0 commit comments

Comments
 (0)