diff --git a/src/config.rs b/src/config.rs index 3ecf8bd..1eb2b26 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,6 +35,7 @@ pub fn load_from_file(path: PathBuf, debug_mode: bool) -> Config { } pub fn write_default_config(path: PathBuf) -> () { + // Write default config file to target path let parent = path.parent().unwrap(); let _ = fs::create_dir_all(parent); let _ = fs::write(path, DEFAULT_CONFIG); @@ -101,12 +102,17 @@ fn load_ansi_color(value: i64) -> Color { fn load_gradient(ini: &Ini, debug_mode: bool) -> ComputableColor { let mut keys = Vec::new(); + // Iterate over all gradient keys, they are defined like that in the config file: + // gradient_key_1=... + // gradient_key_2=... + // gradient_key_N=... let mut i = 0; while let Some(key) = ini.get("gradient", &format!("gradient_key_{}", i)) { keys.push(parse_hex_color(&key)); i += 1; } + // Generate gradient loop if needed if !debug_mode && ini.getbool("gradient", "gradient_loop").unwrap().unwrap() { let mut loop_keys = keys.clone(); loop_keys.reverse(); @@ -115,6 +121,7 @@ fn load_gradient(ini: &Ini, debug_mode: bool) -> ComputableColor { } } + // I use half characters for debug mode rendering, so we take display size * 2 let steps: usize = if debug_mode { debug::DEBUG_COLOR_DISPLAY_SIZE * 2 } else { diff --git a/src/main.rs b/src/main.rs index 1366569..0517de5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,11 +51,12 @@ enum Commands { fn main() -> io::Result<()> { let cli = Cli::parse(); - // Load config + // Load config from either given config file let mut default_generated = false; let config_file = if let Some(custom_config) = cli.config { PathBuf::from(custom_config) } else { + // Or default one, located in ~/.config/tlock let config_file = config_dir().unwrap().join("tlock").join("config"); if !config_file.exists() { write_default_config(config_file.clone()); @@ -65,7 +66,10 @@ fn main() -> io::Result<()> { config_file }; + // Regenerate default config if needed if cli.regenerate_default { + // If a config file already exists and it's not the first time the config + // is being generated, then ask for confirmation if !default_generated && config_file.exists() && !cli.yes { println!("A config file is already located at {:?}", config_file); print!("Do you really want to recreate it ? [y/N] "); @@ -82,15 +86,19 @@ fn main() -> io::Result<()> { } } + // Otherwhise, just write default config to target path write_default_config(config_file.clone()); println!("Done."); return Ok(()); } + // If no config file was found, throw an error + // NOTE: this should never happen if !config_file.exists() { panic!("ERROR: Configuration file not found"); } + // Enable debug mode if needed, and load config let debug_mode = match &cli.command { Some(Commands::Debug {}) => true, _ => false, @@ -98,6 +106,7 @@ fn main() -> io::Result<()> { let mut config = config::load_from_file(config_file, debug_mode); let mut stdout = io::stdout(); + // Print debug infos if debug_mode { debug::print_debug_infos(&mut config)?; return Ok(()); @@ -107,6 +116,7 @@ fn main() -> io::Result<()> { execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?; let _ = terminal::enable_raw_mode()?; + // Start the appropriate mode match &cli.command { Some(Commands::Chrono {}) => modes::chrono::main_loop(&mut config)?, Some(Commands::Timer { duration }) => { diff --git a/src/modes/chrono.rs b/src/modes/chrono.rs index 372e9b6..b240c25 100644 --- a/src/modes/chrono.rs +++ b/src/modes/chrono.rs @@ -172,6 +172,7 @@ fn render_frame( *scroll_offset = lapses.len() - max_items; } + // Iterate over lapses, skipping with scroll offset and taxing N items for (i, lapse) in lapses .iter() .rev() diff --git a/src/modes/debug.rs b/src/modes/debug.rs index e2ed57b..98cce79 100644 --- a/src/modes/debug.rs +++ b/src/modes/debug.rs @@ -27,10 +27,15 @@ pub fn print_debug_infos(config: &mut Config) -> io::Result<()> { print_debug_label("Color scheme")?; let width = config.color.get_keys_count(); + // If width is one, it is a single color if width == 1 { queue!(stdout, style::SetBackgroundColor(config.color.get_value()))?; write!(stdout, "{}", " ".repeat(DEBUG_COLOR_DISPLAY_SIZE))?; - } else { + } + // Otherwhise, it's a gradient + else { + // Use half characters to display two colors in one character using background + // and foreground for _ in 0..width / 2 { queue!(stdout, style::SetForegroundColor(config.color.get_value()))?; config.color.update();