Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
Catto | d341128997 | |
Catto | 46a4c7c541 | |
Catto | 7b4228907d | |
Catto | 2691b59e87 |
|
@ -0,0 +1,35 @@
|
|||
A silly little CLI note app
|
||||
|
||||
[Downloads under Releases](https://git.maidsin.space/catto/snotes/releases)
|
||||
|
||||
# Usage
|
||||
|
||||
```
|
||||
Usage: snotes [OPTIONS]
|
||||
|
||||
Options:
|
||||
-c, --create <CREATE> Create a new note: --create "Hello World" [default: ]
|
||||
-s, --show Show the latest entries
|
||||
--showall Show all entries
|
||||
-t, --tag <TAG> Append or show entries with tag [default: ]
|
||||
-d, --delete Delete the latest entry
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
```
|
||||
|
||||
# Demo
|
||||
|
||||
![demo gif](media/demo.gif)
|
||||
|
||||
# Building (Linux)
|
||||
|
||||
- Clone this repo `git clone "https://git.maidsin.space/catto/snotes.git"`
|
||||
- [Install Rust](https://www.rust-lang.org/learn/get-started)
|
||||
- run `carog build --release` in the root of the project
|
||||
- You now have a working `snotes` binary file under `target/release`
|
||||
|
||||
Windows Builds might be a bit more involved since this dependency builds and bundles sqlite3 [https://github.com/rusqlite/rusqlite](https://github.com/rusqlite/rusqlite)
|
||||
|
||||
# Installation
|
||||
|
||||
You could copy the `snotes` binary into a `/usr/bin` or `~/.local/bin` folder, one that's part of your system PATH so you can run it anywhere.
|
Binary file not shown.
After Width: | Height: | Size: 252 KiB |
100
src/main.rs
100
src/main.rs
|
@ -3,20 +3,24 @@ use clap::Parser;
|
|||
use home::home_dir;
|
||||
use rusqlite::{Connection, Result};
|
||||
|
||||
/// Simple program to greet a person
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
// Quickly throw in a note
|
||||
/// Create a new note: --create "Hello World"
|
||||
#[arg(short, long, default_value_t = String::new())]
|
||||
create: String,
|
||||
// Number of times to greet
|
||||
//#[arg(short, long, default_value_t = 1)]
|
||||
//count: u8,
|
||||
/// Show the latest entries
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
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())]
|
||||
tag: String,
|
||||
/// Delete the latest entry
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
delete: bool,
|
||||
}
|
||||
|
||||
struct Note {
|
||||
|
@ -28,27 +32,54 @@ struct Note {
|
|||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
//for _ in 0..args.count {
|
||||
// println!("Hello {}!", args.name)
|
||||
//}
|
||||
|
||||
match init_db() {
|
||||
Ok(_) => println!(),
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("Failed to initialize DB: {}", e),
|
||||
};
|
||||
|
||||
if args.show {
|
||||
match show_notes() {
|
||||
Ok(_) => println!(),
|
||||
match show_notes(false, &args.tag) {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("Failed to show DB: {}", e),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if args.create != String::new() {
|
||||
match create_note(args.create, args.tag) {
|
||||
Ok(_) => println!(),
|
||||
match create_note(&args.create, &args.tag) {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("Failed to throw into DB: {}", e),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if args.showall {
|
||||
match show_notes(true, &args.tag) {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("Failed to show DB: {}", e),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if args.tag != String::new() && args.create == String::new() {
|
||||
match show_notes(true, &args.tag) {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("Failed to show DB: {}", e),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if args.delete {
|
||||
match delete_latest_note() {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("Failed to delete: {}", e),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
match show_notes(false, &args.tag) {
|
||||
Ok(_) => println!(),
|
||||
Err(e) => println!("Failed to show DB: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,22 +97,27 @@ fn init_db() -> Result<()> {
|
|||
";
|
||||
|
||||
match connection.execute(query_table, []) {
|
||||
Ok(v) => println!("INIT OK {}", v),
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("INIT ERR {}", e),
|
||||
};
|
||||
|
||||
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 connection = Connection::open(home)?;
|
||||
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 == String::new() {
|
||||
String::from("quick")
|
||||
} else {
|
||||
tag.to_string()
|
||||
};
|
||||
|
||||
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),
|
||||
Err(e) => println!("CREATE ERR {}", e),
|
||||
};
|
||||
|
@ -89,13 +125,21 @@ fn create_note(content: String, tag: String) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn show_notes() -> Result<()> {
|
||||
fn show_notes(all: bool, tag: &String) -> Result<()> {
|
||||
let home = home_dir().unwrap().join(".snotes.db");
|
||||
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| {
|
||||
Ok(Note {
|
||||
|
@ -114,3 +158,17 @@ fn show_notes() -> Result<()> {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_latest_note() -> Result<()> {
|
||||
let home = home_dir().unwrap().join(".snotes.db");
|
||||
let connection = Connection::open(home)?;
|
||||
|
||||
let query = String::from("DELETE FROM NOTES WHERE nid = (SELECT MAX(nid) FROM notes)");
|
||||
|
||||
match connection.execute(&query, []) {
|
||||
Ok(v) => println!("DELETE OK {}", v),
|
||||
Err(e) => println!("DELETE ERR {}", e),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue