feat(config): use from arg or build default

This commit is contained in:
Dmitriy Pleshevskiy 2022-02-18 23:37:37 +03:00
parent 8db3ab3c12
commit 83b55ed323
3 changed files with 58 additions and 14 deletions

View File

@ -1,5 +1,25 @@
use serde::Deserialize;
pub fn default() -> Env {
Env {
micro_timer: BreakTimer {
duration: mins(5.0),
postpone_duration: mins(2.5),
rest_duration: mins(0.5),
},
rest_timer: BreakTimer {
duration: mins(45.0),
postpone_duration: mins(5.0),
rest_duration: mins(10.0),
},
notifier: Notifier { duration: 10.0 },
}
}
fn mins(m: f64) -> f64 {
return m * 60.0;
}
#[derive(Deserialize)]
pub struct Env {
pub micro_timer: BreakTimer,

View File

@ -1,4 +1,4 @@
use crate::state;
use crate::config;
use druid::{Color, Env, Key};
pub const TIMER_DURATION: Key<f64> = Key::new("hwt.env.comp.timer.duration");
@ -17,7 +17,7 @@ pub const REST_BREAK_TIMER_REST_DURATION: Key<f64> =
pub const BREAK_NOTIFIER_TIMER_DURATION: Key<f64> = Key::new("hwt.env.widget.notifier.duration");
pub fn configure(env: &mut Env, _data: &state::App) {
pub fn configure(env: &mut Env, config: &config::Env) {
let col_def_white = hsl(0.0, 0.0, 1.0);
// let col_def_black = hsl(0.0, 0.0, 0.0);
let col_graphite = hsl(0.0, 0.0, 0.13);
@ -35,19 +35,27 @@ pub fn configure(env: &mut Env, _data: &state::App) {
env.set(druid::theme::BUTTON_LIGHT, col_faded);
// timers
env.set(MICRO_BREAK_TIMER_DURATION, mins(5.0));
env.set(MICRO_BREAK_TIMER_POSTPONE_DURATION, mins(2.5));
env.set(MICRO_BREAK_TIMER_REST_DURATION, 30.0);
env.set(MICRO_BREAK_TIMER_DURATION, config.micro_timer.duration);
env.set(
MICRO_BREAK_TIMER_POSTPONE_DURATION,
config.micro_timer.postpone_duration,
);
env.set(
MICRO_BREAK_TIMER_REST_DURATION,
config.micro_timer.rest_duration,
);
env.set(REST_BREAK_TIMER_DURATION, mins(45.0));
env.set(REST_BREAK_TIMER_POSTPONE_DURATION, mins(5.0));
env.set(REST_BREAK_TIMER_REST_DURATION, mins(10.0));
env.set(REST_BREAK_TIMER_DURATION, config.rest_timer.duration);
env.set(
REST_BREAK_TIMER_POSTPONE_DURATION,
config.rest_timer.postpone_duration,
);
env.set(
REST_BREAK_TIMER_REST_DURATION,
config.rest_timer.rest_duration,
);
env.set(BREAK_NOTIFIER_TIMER_DURATION, 10.0);
}
fn mins(m: f64) -> f64 {
return m * 60.0;
env.set(BREAK_NOTIFIER_TIMER_DURATION, config.notifier.duration);
}
fn hsl(h: f64, s: f64, l: f64) -> Color {

View File

@ -14,6 +14,22 @@ use std::sync::mpsc::channel;
use std::thread;
fn main() {
let cfg = if let Some(config_path) = std::env::args().skip(1).next() {
log::debug!("[cli] config_path={}", config_path);
let file_content = std::fs::read_to_string(config_path).unwrap_or_else(|err| {
eprintln!("[ERROR] [fs] {}", err);
std::process::exit(0);
});
toml::from_str(&file_content).unwrap_or_else(|err| {
eprintln!("[ERROR] [toml] {}", err);
std::process::exit(0);
})
} else {
log::debug!("use default config");
config::default()
};
let (tx, rx) = channel::<sound::Type>();
thread::spawn(move || loop {
@ -26,7 +42,7 @@ fn main() {
let initial_state = state::App::new(tx, true);
AppLauncher::with_window(win::status::create(initial_state.sound_sender.clone()))
.delegate(Delegate::default())
.configure_env(env::configure)
.configure_env(move |env, _data| env::configure(env, &cfg))
.launch(initial_state)
.expect("Failed to launch application");
}