chore: comments

This commit is contained in:
Pihkaal
2024-03-23 05:15:14 +01:00
parent 1bf0555d66
commit 3150fc3e23
4 changed files with 25 additions and 2 deletions

View File

@@ -35,6 +35,7 @@ pub fn load_from_file(path: PathBuf, debug_mode: bool) -> Config {
} }
pub fn write_default_config(path: PathBuf) -> () { pub fn write_default_config(path: PathBuf) -> () {
// Write default config file to target path
let parent = path.parent().unwrap(); let parent = path.parent().unwrap();
let _ = fs::create_dir_all(parent); let _ = fs::create_dir_all(parent);
let _ = fs::write(path, DEFAULT_CONFIG); 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 { fn load_gradient(ini: &Ini, debug_mode: bool) -> ComputableColor {
let mut keys = Vec::new(); 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; let mut i = 0;
while let Some(key) = ini.get("gradient", &format!("gradient_key_{}", i)) { while let Some(key) = ini.get("gradient", &format!("gradient_key_{}", i)) {
keys.push(parse_hex_color(&key)); keys.push(parse_hex_color(&key));
i += 1; i += 1;
} }
// Generate gradient loop if needed
if !debug_mode && ini.getbool("gradient", "gradient_loop").unwrap().unwrap() { if !debug_mode && ini.getbool("gradient", "gradient_loop").unwrap().unwrap() {
let mut loop_keys = keys.clone(); let mut loop_keys = keys.clone();
loop_keys.reverse(); 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 { let steps: usize = if debug_mode {
debug::DEBUG_COLOR_DISPLAY_SIZE * 2 debug::DEBUG_COLOR_DISPLAY_SIZE * 2
} else { } else {

View File

@@ -51,11 +51,12 @@ enum Commands {
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
// Load config // Load config from either given config file
let mut default_generated = false; let mut default_generated = false;
let config_file = if let Some(custom_config) = cli.config { let config_file = if let Some(custom_config) = cli.config {
PathBuf::from(custom_config) PathBuf::from(custom_config)
} else { } else {
// Or default one, located in ~/.config/tlock
let config_file = config_dir().unwrap().join("tlock").join("config"); let config_file = config_dir().unwrap().join("tlock").join("config");
if !config_file.exists() { if !config_file.exists() {
write_default_config(config_file.clone()); write_default_config(config_file.clone());
@@ -65,7 +66,10 @@ fn main() -> io::Result<()> {
config_file config_file
}; };
// Regenerate default config if needed
if cli.regenerate_default { 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 { if !default_generated && config_file.exists() && !cli.yes {
println!("A config file is already located at {:?}", config_file); println!("A config file is already located at {:?}", config_file);
print!("Do you really want to recreate it ? [y/N] "); 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()); write_default_config(config_file.clone());
println!("Done."); println!("Done.");
return Ok(()); return Ok(());
} }
// If no config file was found, throw an error
// NOTE: this should never happen
if !config_file.exists() { if !config_file.exists() {
panic!("ERROR: Configuration file not found"); panic!("ERROR: Configuration file not found");
} }
// Enable debug mode if needed, and load config
let debug_mode = match &cli.command { let debug_mode = match &cli.command {
Some(Commands::Debug {}) => true, Some(Commands::Debug {}) => true,
_ => false, _ => false,
@@ -98,6 +106,7 @@ fn main() -> io::Result<()> {
let mut config = config::load_from_file(config_file, debug_mode); let mut config = config::load_from_file(config_file, debug_mode);
let mut stdout = io::stdout(); let mut stdout = io::stdout();
// Print debug infos
if debug_mode { if debug_mode {
debug::print_debug_infos(&mut config)?; debug::print_debug_infos(&mut config)?;
return Ok(()); return Ok(());
@@ -107,6 +116,7 @@ fn main() -> io::Result<()> {
execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?; execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;
let _ = terminal::enable_raw_mode()?; let _ = terminal::enable_raw_mode()?;
// Start the appropriate mode
match &cli.command { match &cli.command {
Some(Commands::Chrono {}) => modes::chrono::main_loop(&mut config)?, Some(Commands::Chrono {}) => modes::chrono::main_loop(&mut config)?,
Some(Commands::Timer { duration }) => { Some(Commands::Timer { duration }) => {

View File

@@ -172,6 +172,7 @@ fn render_frame(
*scroll_offset = lapses.len() - max_items; *scroll_offset = lapses.len() - max_items;
} }
// Iterate over lapses, skipping with scroll offset and taxing N items
for (i, lapse) in lapses for (i, lapse) in lapses
.iter() .iter()
.rev() .rev()

View File

@@ -27,10 +27,15 @@ pub fn print_debug_infos(config: &mut Config) -> io::Result<()> {
print_debug_label("Color scheme")?; print_debug_label("Color scheme")?;
let width = config.color.get_keys_count(); let width = config.color.get_keys_count();
// If width is one, it is a single color
if width == 1 { if width == 1 {
queue!(stdout, style::SetBackgroundColor(config.color.get_value()))?; queue!(stdout, style::SetBackgroundColor(config.color.get_value()))?;
write!(stdout, "{}", " ".repeat(DEBUG_COLOR_DISPLAY_SIZE))?; 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 { for _ in 0..width / 2 {
queue!(stdout, style::SetForegroundColor(config.color.get_value()))?; queue!(stdout, style::SetForegroundColor(config.color.get_value()))?;
config.color.update(); config.color.update();