search by tag, show all

This commit is contained in:
Catto 2024-04-04 20:36:00 +02:00
parent 68c2fb8741
commit 2691b59e87
1 changed files with 41 additions and 17 deletions

View File

@ -3,18 +3,19 @@ use clap::Parser;
use home::home_dir; use home::home_dir;
use rusqlite::{Connection, Result}; use rusqlite::{Connection, Result};
/// Simple program to greet a person
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Args { struct Args {
// Quickly throw in a note /// Create a new note: --create "Hello World"
#[arg(short, long, default_value_t = String::new())] #[arg(short, long, default_value_t = String::new())]
create: String, create: String,
// Number of times to greet /// Show the latest entries
//#[arg(short, long, default_value_t = 1)]
//count: u8,
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
show: bool, show: bool,
/// Show all entries
#[arg(long, default_value_t = false)]
showall: bool,
/// Append or show entries with tag
#[arg(short, long, default_value_t = String::new())] #[arg(short, long, default_value_t = String::new())]
tag: String, tag: String,
} }
@ -28,28 +29,38 @@ struct Note {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
//for _ in 0..args.count {
// println!("Hello {}!", args.name)
//}
match init_db() { match init_db() {
Ok(_) => println!(), Ok(_) => println!(),
Err(e) => println!("Failed to initialize DB: {}", e), Err(e) => println!("Failed to initialize DB: {}", e),
}; };
if args.show { if args.show {
match show_notes() { match show_notes(false, &args.tag) {
Ok(_) => println!(), Ok(_) => println!(),
Err(e) => println!("Failed to show DB: {}", e), Err(e) => println!("Failed to show DB: {}", e),
} }
} }
if args.create != String::new() { if args.create != String::new() {
match create_note(args.create, args.tag) { match create_note(&args.create, &args.tag) {
Ok(_) => println!(), Ok(_) => println!(),
Err(e) => println!("Failed to throw into DB: {}", e), Err(e) => println!("Failed to throw into DB: {}", e),
} }
} }
if args.showall {
match show_notes(true, &args.tag) {
Ok(_) => println!(),
Err(e) => println!("Failed to show DB: {}", e),
}
}
if args.tag != String::new() && args.create == String::new() {
match show_notes(true, &args.tag) {
Ok(_) => println!(),
Err(e) => println!("Failed to show DB: {}", e),
}
}
} }
fn init_db() -> Result<()> { fn init_db() -> Result<()> {
@ -73,15 +84,20 @@ fn init_db() -> Result<()> {
Ok(()) Ok(())
} }
fn create_note(content: String, tag: String) -> Result<()> { fn create_note(content: &String, tag: &String) -> Result<()> {
let home = home_dir().unwrap().join(".snotes.db"); let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home)?; let connection = Connection::open(home)?;
let date = Local::now(); let date = Local::now();
let date_string = date.format("%m-%d %H:%M").to_string(); let date_string = date.format("%m-%d-%y %H:%M").to_string();
let tag_string = if tag.to_string() == String::new() {
String::from("quick")
} else {
tag.to_string()
};
let query_insert = "INSERT INTO notes (content, date, tag) VALUES (?1, ?2, ?3)"; let query_insert = "INSERT INTO notes (content, date, tag) VALUES (?1, ?2, ?3)";
match connection.execute(query_insert, [&content, &date_string, &tag]) { match connection.execute(query_insert, [&content, &date_string, &tag_string]) {
Ok(v) => println!("CREATE OK {}", v), Ok(v) => println!("CREATE OK {}", v),
Err(e) => println!("CREATE ERR {}", e), Err(e) => println!("CREATE ERR {}", e),
}; };
@ -89,13 +105,21 @@ fn create_note(content: String, tag: String) -> Result<()> {
Ok(()) Ok(())
} }
fn show_notes() -> Result<()> { fn show_notes(all: bool, tag: &String) -> Result<()> {
let home = home_dir().unwrap().join(".snotes.db"); let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home)?; let connection = Connection::open(home)?;
let query_latest_10 = "SELECT * FROM notes LIMIT 10"; let mut query = String::from("SELECT * FROM notes LIMIT 10");
let mut prepare = connection.prepare(query_latest_10)?; if all {
query = String::from("SELECT * FROM notes");
}
if tag != &String::new() {
query = format!("SELECT * FROM notes WHERE tag IS '{}'", tag);
}
let mut prepare = connection.prepare(&query)?;
let notes = prepare.query_map([], |row| { let notes = prepare.query_map([], |row| {
Ok(Note { Ok(Note {