This commit is contained in:
Andrei Stoica 2024-04-02 10:58:49 -04:00
parent fc7e45cd3f
commit a8e6572a53
5 changed files with 60 additions and 9 deletions

View File

@ -14,3 +14,5 @@ regex = "1.8.4"
serde = { version = "1.0.164", features = ["serde_derive"] }
serde_json = "1.0.97"
resolve-path = "0.1.0"
simple_logger = "4.3.3"
log = "0.4.21"

View File

@ -6,7 +6,6 @@ pub struct Args {
/// set config file to use
#[arg(short, long, value_name = "FILE")]
pub config: Option<String>,
/// show current config file
#[arg(short = 'C', long)]
pub current_config: bool,
@ -23,4 +22,8 @@ pub struct Args {
/// list closest files to date
#[arg(short = 'L', long)]
pub list_all: bool,
/// increase logging level
#[arg(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
}

View File

@ -5,6 +5,7 @@ use chrono::Datelike;
use comrak::nodes::{AstNode, NodeValue};
use comrak::parse_document;
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
use log;
use std::collections::HashMap;
use std::fs::{read, File};
use std::io::Write;
@ -147,14 +148,12 @@ mod test {
let sections = vec!["Sub section".to_string()];
let result = extract_secitons(root, &sections);
println!("{:#?}", root);
assert_eq!(result.keys().count(), 1);
assert!(result.get(sections.first().unwrap()).is_some());
assert_eq!(result.get(sections.first().unwrap()).unwrap().level, 3);
let sections = vec!["Content".to_string()];
let result = extract_secitons(root, &sections);
println!("{:#?}", root);
assert_eq!(result.keys().count(), 1);
assert!(result.get(sections.first().unwrap()).is_some());
assert_eq!(

26
src/logging/mod.rs Normal file
View File

@ -0,0 +1,26 @@
use log::Level;
pub fn get_logging_level(verbose_level: u8) -> Level {
match verbose_level {
..=0 => Level::Error,
1 => Level::Warn,
2 => Level::Info,
3 => Level::Debug,
4.. => Level::Trace,
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_get_logging_level() {
assert_eq!(get_logging_level(0), Level::Error);
assert_eq!(get_logging_level(1), Level::Warn);
assert_eq!(get_logging_level(2), Level::Info);
assert_eq!(get_logging_level(3), Level::Debug);
assert_eq!(get_logging_level(4), Level::Trace);
assert_eq!(get_logging_level(5), Level::Trace);
}
}

View File

@ -1,23 +1,28 @@
mod cli;
mod config;
mod file;
mod logging;
mod todo;
use crate::cli::Args;
use crate::config::Config;
use crate::todo::{File as TodoFile, TaskGroup};
use chrono::naive::NaiveDate;
use chrono::{Local, TimeDelta};
use clap::Parser;
use cli::Args;
use comrak::Arena;
use config::Config;
use log;
use logging::get_logging_level;
use resolve_path::PathResolveExt;
use simple_logger::init_with_level;
use std::fs;
use std::path::Path;
use std::process::Command;
use todo::{File as TodoFile, TaskGroup};
fn main() {
let args = Args::parse();
println!("{:?}", args);
let _logger = init_with_level(get_logging_level(args.verbose)).unwrap();
log::debug!("{:?}", args);
let expected_cfg_files = match Config::expected_locations() {
Ok(cfg_files) => cfg_files,
@ -48,7 +53,7 @@ fn main() {
};
if args.current_config {
println!("{}", &cfg_file);
log::debug!("{}", &cfg_file);
return;
}
@ -56,13 +61,14 @@ fn main() {
Ok(cfg) => cfg,
Err(_e) => panic!("could not load config: {}", cfg_file),
};
log::debug!("{:#?}", cfg);
let data_dir = cfg.notes_dir.resolve().to_path_buf();
if !fs::metadata(&data_dir).is_ok() {
match fs::create_dir_all(&data_dir) {
Err(_e) => panic!("Could not create default directory: {:?}", &data_dir),
_ => (),
_ => log::info!("created dir {}", &data_dir.to_string_lossy()),
};
}
@ -95,14 +101,21 @@ fn main() {
let current_file = match latest_file {
Some(todo_file) if todo_file.date < today && args.previous == 0 => {
let sections = &cfg.sections;
log::info!("looking for sections: {:?}", sections);
let arena = Arena::new();
let root = {
log::info!(
"loading and parsing file: {}",
todo_file.file.to_string_lossy()
);
let contents = file::load_file(&todo_file);
let root = file::parse_todo_file(&contents, &arena);
root
};
log::trace!("file loaded");
let groups = file::extract_secitons(root, sections);
log::trace!("sections extracted");
let level = groups.values().map(|group| group.level).min().unwrap_or(2);
let data = sections
.iter()
@ -114,12 +127,14 @@ fn main() {
let content = file::generate_file_content(&data, &today);
let file_path = file::get_filepath(&data_dir, &today);
log::info!("writing to file: {}", file_path.to_string_lossy());
file::write_file(&file_path, &content);
file_path
}
Some(todo_file) => todo_file.file.clone(),
None => {
let sections = &cfg.sections;
log::info!("creating new empty file with sections: {:?}", sections);
let data = sections
.iter()
.map(|sec| TaskGroup::empty(sec.clone(), 2))
@ -127,10 +142,16 @@ fn main() {
let content = file::generate_file_content(&data, &today);
let file_path = file::get_filepath(&data_dir, &today);
file::write_file(&file_path, &content);
log::info!("writing to file: {}", file_path.to_string_lossy());
file_path
}
};
log::info!(
"Opening {} in {}",
current_file.to_string_lossy(),
cfg.editor
);
Command::new(&cfg.editor)
.args([current_file])
.status()