From 68c5ca9ecedb6001c61e933f2b7069c2e677213d Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Mon, 15 Feb 2021 09:07:49 +0000 Subject: [PATCH] use database trait instead of sqlite impl (#10) small improvements --- src/command/history.rs | 4 ++-- src/command/import.rs | 26 +++++++++++++------------- src/command/mod.rs | 4 ++-- src/command/stats.rs | 10 +++++----- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/command/history.rs b/src/command/history.rs index e40af4d..5d2a805 100644 --- a/src/command/history.rs +++ b/src/command/history.rs @@ -3,7 +3,7 @@ use std::env; use eyre::Result; use structopt::StructOpt; -use crate::local::database::{Database, Sqlite}; +use crate::local::database::Database; use crate::local::history::History; #[derive(StructOpt)] @@ -41,7 +41,7 @@ fn print_list(h: &[History]) { } impl Cmd { - pub fn run(&self, db: &mut Sqlite) -> Result<()> { + pub fn run(&self, db: &mut impl Database) -> Result<()> { match self { Self::Start { command: words } => { let command = words.join(" "); diff --git a/src/command/import.rs b/src/command/import.rs index 8810840..4c3714b 100644 --- a/src/command/import.rs +++ b/src/command/import.rs @@ -5,7 +5,7 @@ use directories::UserDirs; use eyre::{eyre, Result}; use structopt::StructOpt; -use crate::local::database::{Database, Sqlite}; +use crate::local::database::Database; use crate::local::history::History; use crate::local::import::Zsh; use indicatif::ProgressBar; @@ -26,13 +26,13 @@ pub enum Cmd { } impl Cmd { - pub fn run(&self, db: &mut Sqlite) -> Result<()> { - println!(" A'Tuin "); - println!("====================="); - println!(" \u{1f30d} "); - println!(" \u{1f418}\u{1f418}\u{1f418}\u{1f418} "); - println!(" \u{1f422} "); - println!("====================="); + pub fn run(&self, db: &mut impl Database) -> Result<()> { + println!(" A'Tuin "); + println!("======================"); + println!(" \u{1f30d} "); + println!(" \u{1f418}\u{1f418}\u{1f418}\u{1f418} "); + println!(" \u{1f422} "); + println!("======================"); println!("Importing history..."); match self { @@ -53,7 +53,7 @@ impl Cmd { } } -fn import_zsh(db: &mut Sqlite) -> Result<()> { +fn import_zsh(db: &mut impl Database) -> Result<()> { // oh-my-zsh sets HISTFILE=~/.zhistory // zsh has no default value for this var, but uses ~/.zhistory. // we could maybe be smarter about this in the future :) @@ -65,8 +65,8 @@ fn import_zsh(db: &mut Sqlite) -> Result<()> { if !histpath.exists() { return Err(eyre!( - "Could not find history file at {}", - histpath.to_str().unwrap() + "Could not find history file {:?}. try updating $HISTFILE", + histpath )); } @@ -89,7 +89,7 @@ fn import_zsh(db: &mut Sqlite) -> Result<()> { } }; - let zsh = Zsh::new(histpath.to_str().unwrap())?; + let zsh = Zsh::new(histpath)?; let progress = ProgressBar::new(zsh.loc); @@ -106,7 +106,7 @@ fn import_zsh(db: &mut Sqlite) -> Result<()> { db.save_bulk(&buf)?; progress.inc(buf.len() as u64); - buf = Vec::::with_capacity(buf_size); + buf.clear(); } } diff --git a/src/command/mod.rs b/src/command/mod.rs index a5dd039..78e55a0 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -2,7 +2,7 @@ use eyre::Result; use structopt::StructOpt; use uuid::Uuid; -use crate::local::database::Sqlite; +use crate::local::database::Database; mod history; mod import; @@ -35,7 +35,7 @@ pub fn uuid_v4() -> String { } impl AtuinCmd { - pub fn run(self, db: &mut Sqlite) -> Result<()> { + pub fn run(self, db: &mut impl Database) -> Result<()> { match self { Self::History(history) => history.run(db), Self::Import(import) => import.run(db), diff --git a/src/command/stats.rs b/src/command/stats.rs index ea5893f..19079b8 100644 --- a/src/command/stats.rs +++ b/src/command/stats.rs @@ -1,14 +1,14 @@ use std::collections::HashMap; use chrono::prelude::*; -use chrono::{Duration, Utc}; +use chrono::Duration; use chrono_english::{parse_date_string, Dialect}; use cli_table::{format::Justify, print_stdout, Cell, Style, Table}; use eyre::{eyre, Result}; use structopt::StructOpt; -use crate::local::database::{Database, Sqlite}; +use crate::local::database::Database; use crate::local::history::History; #[derive(StructOpt)] @@ -70,7 +70,7 @@ fn compute_stats(history: &[History]) -> Result<()> { } impl Cmd { - pub fn run(&self, db: &mut Sqlite) -> Result<()> { + pub fn run(&self, db: &mut impl Database) -> Result<()> { match self { Self::Day { words } => { let words = if words.is_empty() { @@ -79,10 +79,10 @@ impl Cmd { words.join(" ") }; - let start = parse_date_string(words.as_str(), Local::now(), Dialect::Us)?; + let start = parse_date_string(&words, Local::now(), Dialect::Us)?; let end = start + Duration::days(1); - let history = db.range(start.with_timezone(&Utc), end.with_timezone(&Utc))?; + let history = db.range(start.into(), end.into())?; compute_stats(&history)?;