Skip to content

Commit

Permalink
Merge pull request #28 from jfding/config_blink_or_not
Browse files Browse the repository at this point in the history
Config blink or not by new key in toml config file
  • Loading branch information
jfding authored Sep 29, 2024
2 parents 7499743 + 641bac1 commit 930fb77
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ colored = "2.1.0"
crossterm = "0.28.1"
dirs = "5.0.1"
inquire = "0.7.5"
lazy_static = "1.5.0"
regex = "1.10.6"
serde = { version = "1.0.210", features = ["derive"] }
toml = "0.8.19"
Expand Down
78 changes: 34 additions & 44 deletions src/conf.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,48 @@
use std::path::PathBuf;
use std::sync::RwLock;
use dirs;
use toml;
use serde::Deserialize;
use lazy_static::lazy_static;

use crate::util::*;

const DEF_CONFIG: &str = r#"# config for todor in toml
const DEF_CONFIG_CONTENT: &str = r#"# config for todor in toml
## base directory for todor data, if not set, use default as below
#basedir = "~/.local/share/todor"
#blink = true
"#;

lazy_static! {
pub static ref CONFIG: RwLock<Config> = RwLock::new(Config::load(None));
}

#[derive(Deserialize, Debug, Default)]
pub struct Config {
/// base directory for todor data
pub basedir: Option<String>,

/// blink the icons of items or not
pub blink: Option<bool>,
}

impl Config {
fn conf_path_normalize(path_str: String) -> PathBuf {
let conf_path;
if path_str.starts_with("~/") {
conf_path = PathBuf::from(path_str
.replace("~", dirs::home_dir()
.expect("cannot get home dir")
.to_str()
.unwrap()));

} else if path_str.starts_with("./") {
conf_path = std::env::current_dir()
.expect("cannot get current dir")
.join(path_str.to_string().strip_prefix("./").unwrap());
} else if !path_str.starts_with("/") {
conf_path = std::env::current_dir()
.expect("cannot get current dir")
.join(path_str);
} else {
conf_path = PathBuf::from(path_str);
}
conf_path
}

fn check_and_touch(conf_path: &PathBuf) {
if !conf_path.exists() {
std::fs::create_dir_all(conf_path.parent().unwrap())
.expect("Failed to create base directory");

std::fs::write(conf_path, DEF_CONFIG)
.expect("cannot create config file");
pub fn update_with(&mut self, aconf: &Config) {
if let Some(basedir) = &aconf.basedir {
self.basedir = Some(basedir.clone());
}
}

fn values_normalize(&mut self) {
if let Some(basedir) = &self.basedir {
if basedir.starts_with("~/") {
self.basedir = Some(basedir.replace("~", dirs::home_dir()
.expect("cannot get home dir")
.to_str()
.unwrap()));
}
if let Some(blink) = aconf.blink {
self.blink = Some(blink);
}
}

pub fn load(path_str: Option<String>) -> Self {
let confp;
if let Some(path_str) = path_str {
confp = Config::conf_path_normalize(path_str);
confp = util::path_normalize(path_str);
if !confp.exists() {
eprintln!("specified config file not found, ignore it");
return Config::default();
Expand All @@ -75,16 +53,28 @@ impl Config {
.expect("cannot get home dir")
.join(rel_base);

Config::check_and_touch(&confp);
}
if !confp.exists() {
std::fs::create_dir_all(confp.parent().unwrap())
.expect("Failed to create base directory");

std::fs::write(confp.clone(), DEF_CONFIG_CONTENT)
.expect("cannot create config file");
}
}

let mut conf :Config = toml::from_str(
&std::fs::read_to_string(&confp)
.expect("cannot read config file"))
.expect("cannot parse config file");

conf.values_normalize();
if let Some(basedir) = conf.basedir {
conf.basedir = Some(util::path_normalize(basedir)
.to_str()
.unwrap()
.to_string()
)
}

conf
}
}
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crossterm::cursor::SetCursorStyle::*;

use todor::taskbox::*;
use todor::cli::*;
use todor::conf::Config;
use todor::conf::*;
use todor::util;
use todor::util::*;

Expand All @@ -20,8 +20,13 @@ fn main() {
inbox = Some(get_tomorrow())
}

let conf = Config::load(args.config);
let inbox_path = get_inbox_file(args.dir.or(conf.basedir), inbox);
if args.config.is_some() {
let mut g_conf = CONFIG.write().unwrap();
let conf = Config::load(args.config);
g_conf.update_with(&conf);
}

let inbox_path = get_inbox_file(args.dir.or(CONFIG.read().unwrap().basedir.clone()), inbox);

match args.command {
Some(Commands::List) | None => {
Expand Down
7 changes: 4 additions & 3 deletions src/taskbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::ops::*;

use crate::util;
use crate::util::*;
use crate::conf::*;

pub fn get_inbox_file(dir: Option<String>, inbox: Option<String>) -> PathBuf {
// for windows compatibility
Expand Down Expand Up @@ -270,11 +271,11 @@ impl TaskBox {
let mut last_is_sub = false;

for (t, done) in &self.tasks {
msg = format!("{} ", S_checkbox!(CHECKBOX).blink());
msg = format!("{} ", S_blink!(S_checkbox!(CHECKBOX)));
if t.starts_with(SUB_PREFIX) {
if *done { continue }

msg = format!("{} {}", SUBTASK.to_string().blink(), msg);
msg = format!("{} {}", S_blink!(SUBTASK), msg);
msg += t.strip_prefix(SUB_PREFIX).unwrap();
last_is_sub = true;

Expand Down Expand Up @@ -474,7 +475,7 @@ impl TaskBox {
print!("{} {}",S_checkbox!(TASKBOX), b);
let tbox = TaskBox::new(basedir.join(b).with_extension("md"));
if tbox.alias.is_some() {
println!(" ({})", S_hints!(tbox.alias.unwrap()).blink())
println!(" ({})", S_hints!(tbox.alias.unwrap()))
} else {
println!()
}
Expand Down
38 changes: 36 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};
use std::env;
use cmd_lib::*;
use colored::Colorize;
Expand Down Expand Up @@ -31,12 +31,22 @@ macro_rules! S_movefrom { ($e:expr) => { $e.to_string().green() }; }
#[macro_export]
macro_rules! S_moveto { ($e:expr) => { $e.to_string().red() }; }
#[macro_export]
macro_rules! S_hints { ($e:expr) => { $e.to_string().bright_black() }; }
macro_rules! S_hints { ($e:expr) => { $e.to_string().bright_black().blink() }; }
#[macro_export]
macro_rules! S_success { ($e:expr) => { $e.to_string().green().bold() }; }
#[macro_export]
macro_rules! S_failure { ($e:expr) => { $e.to_string().red().blink() }; }

#[macro_export]
macro_rules! S_blink { ($e:expr) => {
if std::env::var("NO_BLINK").is_ok() ||
CONFIG.read().unwrap().blink.unwrap_or(true) == false {
$e.to_string().bold()
} else {
$e.to_string().blink()
}
};}

// for 'clap'
pub fn get_usage_styles() -> styling::Styles {
styling::Styles::styled()
Expand Down Expand Up @@ -153,3 +163,27 @@ pub fn pick_file() -> String {
std::process::exit(1)
)
}

pub fn path_normalize(path_str: String) -> PathBuf {
let conf_path;
if path_str.starts_with("~/") {
conf_path = PathBuf::from(path_str
.replace("~", dirs::home_dir()
.expect("cannot get home dir")
.to_str()
.unwrap()));

} else if path_str.starts_with("./") {
conf_path = std::env::current_dir()
.expect("cannot get current dir")
.join(path_str.to_string().strip_prefix("./").unwrap());
} else if !path_str.starts_with("/") {
conf_path = std::env::current_dir()
.expect("cannot get current dir")
.join(path_str);
} else {
conf_path = PathBuf::from(path_str);
}

conf_path
}

0 comments on commit 930fb77

Please sign in to comment.