summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSatyarth Sampath <satyarth.23@gmail.com>2022-05-03 20:19:27 +0530
committerGitHub <noreply@github.com>2022-05-03 15:49:27 +0100
commit4a839dab444d1ceccaeca42a2c4c3594281ee22a (patch)
treec15379da504fc59f46130e4ff47d28fc2b4456dc
parent7a394b0115c0e05a63ae7ac8b41f02a992fe3d45 (diff)
Adds stats summary (#384)
* stat command parsing is handled without subcommands * Updates match clause based on PR review Co-authored-by: Conrad Ludgate <oon@conradludgate.com> * updates value returned by match based on PR review * adds vscode to gitignore * use an if statement instead of match Co-authored-by: Satyarth <satyarth.sampath@gojek.com> Co-authored-by: Conrad Ludgate <oon@conradludgate.com>
-rw-r--r--.gitignore1
-rw-r--r--src/command/client.rs1
-rw-r--r--src/command/client/stats.rs51
3 files changed, 18 insertions, 35 deletions
diff --git a/.gitignore b/.gitignore
index 48f42de3..02edf337 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
*/target
.env
.idea/
+.vscode/
diff --git a/src/command/client.rs b/src/command/client.rs
index 80316ed9..c75872aa 100644
--- a/src/command/client.rs
+++ b/src/command/client.rs
@@ -29,7 +29,6 @@ pub enum Cmd {
Import(import::Cmd),
/// Calculate statistics for your history
- #[clap(subcommand)]
Stats(stats::Cmd),
/// Output shell setup
diff --git a/src/command/client/stats.rs b/src/command/client/stats.rs
index 5f1bab50..80450989 100644
--- a/src/command/client/stats.rs
+++ b/src/command/client/stats.rs
@@ -14,12 +14,9 @@ use atuin_client::{
#[derive(Parser)]
#[clap(infer_subcommands = true)]
-pub enum Cmd {
- /// Compute statistics for all of time
- All,
-
- /// Compute statistics for a single day
- Day { words: Vec<String> },
+pub struct Cmd {
+ /// compute statistics for the specified period, leave blank for statistics since the beginning
+ period: Vec<String>,
}
fn compute_stats(history: &[History]) -> Result<()> {
@@ -28,7 +25,6 @@ fn compute_stats(history: &[History]) -> Result<()> {
for i in history {
*commands.entry(i.command.clone()).or_default() += 1;
}
-
let most_common_command = commands.iter().max_by(|a, b| a.1.cmp(b.1));
if most_common_command.is_none() {
@@ -72,32 +68,19 @@ impl Cmd {
settings: &Settings,
) -> Result<()> {
let context = current_context();
-
- match self {
- Self::Day { words } => {
- let words = if words.is_empty() {
- String::from("yesterday")
- } else {
- words.join(" ")
- };
-
- let start = parse_date_string(&words, Local::now(), settings.dialect.into())?;
- let end = start + Duration::days(1);
-
- let history = db.range(start.into(), end.into()).await?;
-
- compute_stats(&history)?;
-
- Ok(())
- }
-
- Self::All => {
- let history = db.list(FilterMode::Global, &context, None, false).await?;
-
- compute_stats(&history)?;
-
- Ok(())
- }
- }
+ let words = if self.period.is_empty() {
+ String::from("all")
+ } else {
+ self.period.join(" ")
+ };
+ let history = if words.as_str() == "all" {
+ db.list(FilterMode::Global, &context, None, false).await?
+ } else {
+ let start = parse_date_string(&words, Local::now(), settings.dialect.into())?;
+ let end = start + Duration::days(1);
+ db.range(start.into(), end.into()).await?
+ };
+ compute_stats(&history)?;
+ Ok(())
}
}