fix: sqlite storage, add max deptth in list

This commit is contained in:
əlemi 2022-12-15 02:52:05 +01:00
parent cfe29f0c12
commit 6ec8ad3ab5
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
4 changed files with 20 additions and 12 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "memo-cli"
version = "0.1.1"
version = "0.2.0"
authors = ["alemidev <me@alemi.dev>"]
description = "A simple CLI tool to help you remember things"
repository = "https://github.com/alemidev/memo-cli"

View file

@ -20,7 +20,8 @@ impl Tree {
}
}
pub fn format_tree(node: &MemoNode, depth: i32, colored: bool) -> String {
pub fn format_tree(node: &MemoNode, depth: i32, colored: bool, max_depth: i32) -> String {
if depth >= max_depth { return "".to_string(); }
let mut out = "".to_string();
let mut prefix = "".to_string(); // TODO string builder?
for i in 0..depth {
@ -45,7 +46,7 @@ pub fn format_tree(node: &MemoNode, depth: i32, colored: bool) -> String {
for sub in &node.sub {
let sep = if colored { "".bright_black().to_string() } else { "".to_string() };
out.push_str(format!("{}{} #{}\n", prefix, sep, sub.tag).as_str());
out.push_str(format_tree(sub, depth + 1, colored).as_str());
out.push_str(format_tree(sub, depth + 1, colored, max_depth).as_str());
}
// if prefix.len() > 0 {

View file

@ -62,6 +62,8 @@ enum Commands {
tag: Option<String>,
#[clap(short, long, help = "show memos in a notification")]
notify: bool,
#[clap(short, long, help = "limit how deep the memo tree is traversed", default_value_t = 20)]
depth: i32,
#[clap(long, help = "show completed tasks")]
old: bool,
},
@ -216,13 +218,13 @@ where T: StorageDriver,
m_str,
);
}
Some(Commands::List { tag, notify, old }) => {
Some(Commands::List { tag, notify, depth, old }) => {
let all = storage.all(old).unwrap();
display_memos(all, tag, &ctx, notify);
display_memos(all, tag, &ctx, notify, depth);
},
None => {
let all = storage.all(false).unwrap();
display_memos(all, None, &ctx, false);
display_memos(all, None, &ctx, false, 5);
}
}
@ -245,7 +247,7 @@ where T: StorageDriver,
Ok(())
}
fn display_memos(all: Vec<Memo>, query: Option<String>, ctx: &Context, notify: bool) {
fn display_memos(all: Vec<Memo>, query: Option<String>, ctx: &Context, notify: bool, max_depth: i32) {
// let all = storage.all(args.old).unwrap();
let mut builder = String::new();
let timing = format!(
@ -264,7 +266,7 @@ fn display_memos(all: Vec<Memo>, query: Option<String>, ctx: &Context, notify: b
if let Some(q) = query {
tree_ref = traverse(tree_ref, q);
}
builder.push_str(format_tree(tree_ref, 0, !notify).as_str());
builder.push_str(format_tree(tree_ref, 0, !notify, max_depth).as_str());
if notify {
Notification::new()
.summary(format!("memo-cli | {}", timing).as_str())

View file

@ -37,8 +37,8 @@ impl SQLiteStorage {
)?;
connection.execute(
"CREATE TABLE IF NOT EXISTS state (
last_edit DATETIME DEFAULT NULL,
last_sync DATETIME DEFAULT NULL
last_edit DATETIME NOT NULL,
last_sync DATETIME DEFAULT NULL,
last_memo TEXT DEFAULT NULL
);",
[],
@ -51,6 +51,11 @@ impl SQLiteStorage {
// );",
// [],
// )?;
connection.execute(
"INSERT INTO state (last_edit) VALUES (?)",
params![Utc::now()]
)?;
}
return Ok(SQLiteStorage { conn: connection , tmpfile: None });
@ -131,8 +136,8 @@ impl StorageDriver for SQLiteStorage {
let tx = self.conn.transaction()?;
tx.execute("DELETE FROM memo WHERE id = ?", params![m.id.to_string()])?;
tx.execute(
"INSERT INTO memo (id, body, due, done, last_edit) VALUES (?, ?, ?, ?, ?)",
params![m.id.to_string(), m.body, m.due, m.done, m.last_edit],
"INSERT INTO memo (id, tag, body, due, done, last_edit) VALUES (?, ?, ?, ?, ?, ?)",
params![m.id.to_string(), m.tag, m.body, m.due, m.done, m.last_edit],
)?;
tx.execute("UPDATE state SET last_edit = ?", params![Utc::now()])?;
tx.commit()?;