memo-cli/src/main.rs

196 lines
5.2 KiB
Rust
Raw Normal View History

mod storage;
mod utils;
2022-03-20 23:48:33 +01:00
mod remote;
2022-03-16 02:07:02 +01:00
use chrono::{DateTime, Local, Utc};
use clap::{Parser, Subcommand};
2022-03-16 02:10:22 +01:00
use colored::Colorize;
2022-03-16 02:07:02 +01:00
use const_format::concatcp;
use git_version::git_version;
use regex::Regex;
use storage::{open_sqlite_storage, Memo, MemoStorage, AuthStorage, StateStorage, SQLiteStorage};
2022-03-16 02:07:02 +01:00
use utils::{find_by_regex, parse_human_duration, HumanDisplay};
2022-03-20 23:48:33 +01:00
use remote::RemoteSync;
2022-03-15 22:17:24 +01:00
const GIT_VERSION: &str = git_version!();
const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const VERSION: &str = concatcp!(PKG_VERSION, "-", GIT_VERSION);
#[derive(Parser)]
2022-03-15 22:17:24 +01:00
#[clap(author, about, long_about = None)]
#[clap(version = VERSION)]
#[clap(disable_colored_help = true)]
#[clap(subcommand_required = false)]
#[clap(disable_help_subcommand = true)]
struct Cli {
#[clap(subcommand)]
command: Option<Commands>,
#[clap(short, long, help = "show memos in a notification")]
notify: bool,
#[clap(long, help = "show completed tasks")]
old: bool,
2022-03-20 23:48:33 +01:00
#[clap(short, long, help = "synchronize memo db")]
sync: bool,
#[clap(short, long, help = "location for database file")]
2022-03-15 22:17:24 +01:00
path: Option<String>,
2022-03-13 20:13:13 +01:00
}
#[derive(Subcommand)]
enum Commands {
2022-03-15 22:17:24 +01:00
/// create a new memo
#[clap(trailing_var_arg = true)]
New {
#[clap(multiple_values = true)]
#[clap(min_values = 1)]
#[clap(required = true)]
body: Vec<String>,
#[clap(short, long, help = "due time relative to now")]
due: Option<String>, // TODO allow to pass date
},
2022-03-15 22:17:24 +01:00
/// mark existing memo as done
Done {
search: String,
#[clap(long, help = "delete more than one task if matched")]
many: bool,
2022-03-16 02:07:02 +01:00
},
/// change existing memo
Edit {
search: String,
#[clap(short, long, help = "set memo message")]
body: Option<String>,
#[clap(short, long, help = "set due time relative to now")]
due: Option<String>, // TODO allow to pass date
},
2022-03-13 20:13:13 +01:00
}
fn main() {
let args = Cli::parse();
2022-03-13 20:13:13 +01:00
let home_path = std::env!("HOME").to_string();
let mut db_path: String = home_path + "/.local/share/memo-cli.db";
2022-03-15 22:17:24 +01:00
if let Some(db) = args.path {
db_path = db;
2022-03-13 20:13:13 +01:00
}
2022-03-20 23:48:33 +01:00
if args.sync {
let res = SQLiteStorage::fetch("asdasd", "http://127.0.0.1:8443");
if res.is_ok() {
println!("[v] downloaded remote db");
} else {
println!("[!] could not fetch db : {}", res.err().unwrap().to_string());
}
}
let storage = open_sqlite_storage(&db_path).unwrap();
2022-03-13 20:13:13 +01:00
match args.command {
Some(Commands::New { body, due }) => {
let mut due_date: Option<DateTime<Utc>> = None;
if let Some(d) = due {
if d.len() > 0 {
due_date = Some(Utc::now() + parse_human_duration(d.as_str()).unwrap());
}
}
let txt = body.join(" ");
storage.add(txt.as_str(), due_date).unwrap();
2022-03-16 02:07:02 +01:00
println!("{} new memo: {}", "[+]".bold(), txt);
}
Some(Commands::Done { search, many }) => {
let rex = Regex::new(search.as_str());
let mut found = false;
let mut to_remove: Option<Memo> = None;
if let Some(re) = rex.ok() {
for memo in storage.all(false).unwrap() {
if re.is_match(memo.body.as_str()) {
if many {
storage.del(memo.id).unwrap();
2022-03-17 01:58:22 +01:00
println!("[-] done task : {}", memo.body);
} else if found {
println!("[!] would remove multiple tasks");
to_remove = None;
break;
} else {
to_remove = Some(memo);
found = true;
}
}
}
if let Some(rm) = to_remove {
storage.del(rm.id).unwrap();
2022-03-17 01:58:22 +01:00
println!("{} done memo: {}", "[-]".bold(), rm.body);
}
2022-03-13 20:13:13 +01:00
} else {
2022-03-17 01:58:22 +01:00
println!("{} invalid regex", format!("[{}]", "!".red()).bold());
}
}
2022-03-16 02:07:02 +01:00
Some(Commands::Edit { search, body, due }) => {
let mut m = find_by_regex(
regex::Regex::new(search.as_str()).unwrap(),
storage.all(false).unwrap(),
)
.unwrap();
if let Some(b) = body {
m.body = b;
}
if let Some(d) = due {
if d == "null" || d == "none" {
m.due = None
} else {
m.due = Some(Utc::now() + parse_human_duration(d.as_str()).unwrap());
}
}
storage.set(&m).unwrap();
2022-03-17 01:58:22 +01:00
println!(
"{} updated memo\n{}",
format!("[{}]", ">".green()).bold().to_string(),
m.colored(),
);
2022-03-16 02:07:02 +01:00
}
None => {
let all = storage.all(args.old).unwrap();
let mut builder = String::new();
let timing = if let Some(state) = storage.get_state().ok() {
let now = Local::now();
format!("last run: {}", state.last_run.with_timezone(&now.timezone()).format("%a %d/%m %H:%M"))
} else { Local::now().format("%a %d/%m, %H:%M").to_string() };
if args.old {
builder.push_str("Archived memos:\n");
}
2022-03-14 03:49:35 +01:00
if all.len() < 1 {
builder.push_str("[ ] nothing to remember\n");
2022-03-14 03:49:35 +01:00
}
for m in all {
2022-03-17 01:58:22 +01:00
let tmp = if args.notify { m.human() } else { m.colored() };
builder.push_str(tmp.as_str());
builder.push('\n');
}
if args.notify {
libnotify::init("memo-cli").unwrap();
let n = libnotify::Notification::new(
format!("memo-cli | {}", timing).as_str(),
Some(builder.as_str()),
2022-03-16 02:07:02 +01:00
None,
);
n.show().unwrap();
libnotify::uninit();
} else {
println!("{} | {}", "memo-cli".bold(), timing);
print!("{}", builder);
2022-03-13 20:13:13 +01:00
}
}
}
2022-03-20 23:48:33 +01:00
if args.sync {
let res = SQLiteStorage::store("asdasd", "http://127.0.0.1:8443");
if res.is_ok() {
println!("[^] uploaded local db");
storage.set_sync_time(Utc::now()).unwrap();
2022-03-20 23:48:33 +01:00
} else {
println!("[!] could not upload db : {}", res.err().unwrap().to_string());
}
}
storage.set_run_time(Utc::now()).unwrap();
2022-03-13 20:13:13 +01:00
}