Skip to content

Commit

Permalink
Add option to calculate program duration
Browse files Browse the repository at this point in the history
  • Loading branch information
tasnimAlam committed Sep 2, 2021
1 parent 7081449 commit ef9309b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ brew install tasnimAlam/word-counter/word_counter
## Usage

```console
$ ./word_counter poem.txt --top 4 --search lover
$ ./word_counter poem.txt --top 4 --search lover --max --duration
+---------------+-------+---+
| Search result | lover | 3 |
+---------------+-------+---+
Expand All @@ -44,6 +44,7 @@ $ ./word_counter poem.txt --top 4 --search lover
+-------+-------+
| lover | 3 |
+-------+-------+
Duration : 2ms
```

## Options
Expand All @@ -56,10 +57,11 @@ USAGE:
word_counter [FLAGS] [OPTIONS] <input>

FLAGS:
-h, --help Prints help information
-r, --reverse Reverse order
-m, --max Show most counted word
-V, --version Prints version information
-d, --duration Duration of all the calculations
-h, --help Prints help information
-r, --reverse Reverse order
-m, --max Show most counted word
-V, --version Prints version information

OPTIONS:
-o, --output <output> Output file
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::time::Instant;
use structopt::StructOpt;
#[macro_use]
extern crate prettytable;
Expand Down Expand Up @@ -38,12 +39,17 @@ pub struct Opt {
#[structopt(short = "m", long = "--max")]
show_max: bool,

/// Duration of all the calculations
#[structopt(short = "d", long = "--duration")]
duration: bool,

/// Output file
#[structopt(short, long, parse(from_os_str))]
output: Option<PathBuf>,
}

pub fn run(opt: Opt) -> Result<(), Box<dyn Error>> {
let time = Instant::now();
// Read contents from file
let mut f = File::open(opt.input)?;
let mut contents = String::new();
Expand Down Expand Up @@ -90,6 +96,11 @@ pub fn run(opt: Opt) -> Result<(), Box<dyn Error>> {
// Print count table
print_counts(&result);

// Print program duration
if opt.duration {
println!("Duration : {}ms", time.elapsed().as_millis());
}

Ok(())
}

Expand Down

0 comments on commit ef9309b

Please sign in to comment.