diff --git a/src/main.rs b/src/main.rs index 8599332..6cfdeee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,9 +15,10 @@ use utils::{parse_human_duration, HumanDisplay}; struct Cli { #[clap(subcommand)] command: Option, - #[clap(short, long, help = "location for database file")] db_path: Option, + #[clap(long, help = "show completed tasks")] + old: bool, } #[derive(Subcommand)] @@ -69,7 +70,7 @@ fn main() { let mut found = false; let mut to_remove: Option = None; if let Some(re) = rex.ok() { - for memo in storage.all().unwrap() { + for memo in storage.all(false).unwrap() { if re.is_match(memo.body.as_str()) { if many { storage.del(memo.id).unwrap(); @@ -97,7 +98,7 @@ fn main() { println!("[-] task #{} deleted", id); } None => { - let all = storage.all().unwrap(); + let all = storage.all(args.old).unwrap(); if all.len() < 1 { println!("[ ] nothing to remember"); } diff --git a/src/storage.rs b/src/storage.rs index f700a90..f708559 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -25,7 +25,7 @@ impl fmt::Display for Memo { } pub trait MemoStorage { - fn all(&self) -> Result, Error>; + fn all(&self, done: bool) -> Result, Error>; fn add(&self, body: &str, due: Option>) -> Result<(), Error>; fn del(&self, id: u32) -> Result; fn get(&self, id: u32) -> Result; @@ -44,7 +44,8 @@ pub fn open_sqlite_storage(path: &str) -> Result { "CREATE TABLE IF NOT EXISTS memo ( id INTEGER PRIMARY KEY, body TEXT NOT NULL, - due DATETIME + due DATETIME, + done BOOL DEFAULT FALSE );", [], )?; @@ -52,7 +53,7 @@ pub fn open_sqlite_storage(path: &str) -> Result { } impl MemoStorage for SQLiteStorage { - fn all(&self) -> Result, Error> { + fn all(&self, done: bool) -> Result, Error> { let mut results = Vec::new(); /* @@ -62,8 +63,10 @@ impl MemoStorage for SQLiteStorage { */ { - let mut statement = self.conn.prepare("SELECT * FROM memo WHERE due IS NOT NULL ORDER BY due, id")?; - let mut rows = statement.query([])?; + let mut statement = self.conn.prepare( + "SELECT * FROM memo WHERE due IS NOT NULL AND done = ? ORDER BY due, id", + )?; + let mut rows = statement.query(params![done as u8])?; while let Some(row) = rows.next()? { results.push(Memo { @@ -75,8 +78,10 @@ impl MemoStorage for SQLiteStorage { } { - let mut statement = self.conn.prepare("SELECT * FROM memo WHERE due IS NULL ORDER BY due, id")?; - let mut rows = statement.query([])?; + let mut statement = self + .conn + .prepare("SELECT * FROM memo WHERE due IS NULL AND done = ? ORDER BY due, id")?; + let mut rows = statement.query(params![done as u8])?; while let Some(row) = rows.next()? { results.push(Memo { @@ -107,7 +112,7 @@ impl MemoStorage for SQLiteStorage { fn del(&self, id: u32) -> Result { let count = self .conn - .execute("DELETE FROM memo WHERE id = ?", params![id])?; + .execute("UPDATE memo SET done = 1 WHERE id = ?", params![id])?; if count > 0 { return Ok(true); } else { @@ -116,14 +121,16 @@ impl MemoStorage for SQLiteStorage { } fn get(&self, id: u32) -> Result { - return Ok(self - .conn - .query_row("SELECT * FROM memo WHERE id = ?", params![id], |row| { + return Ok(self.conn.query_row( + "SELECT * FROM memo WHERE id = ? AND done = 0", + params![id], + |row| { return Ok(Memo { id: row.get(0)?, body: row.get(1)?, due: row.get(2).unwrap_or(None), }); - })?); + }, + )?); } }