diff --git a/Cargo.toml b/Cargo.toml index 2ba217b..fa35376 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" [dependencies] chrono = "0.4.31" +clap = { version = "4.4.18", features = ["derive"] } crossterm = "0.27.0" +dirs = "5.0.1" ini = "1.3.0" diff --git a/src/default_config b/src/default_config new file mode 100644 index 0000000..e1ae028 --- /dev/null +++ b/src/default_config @@ -0,0 +1,37 @@ +# tlock config file + +[general] + +# Say goodbye when you press CTRL-C +# Value: true, false +polite=true + +# FPS +# Value: int +fps=30 + + +[format] + +# Date format +# Value: TODO +date=%Y-%m-%d + +[styling] + +# Which color mode to use +# Value: "term", "hex" or "ansi" +color_mode=term + +# Loaded if color_mode is set to "term" +# Value: 0-15 +color_term=7 + +# Loaded if color_mode is set to "hex" +# Value: 0-255 +color_hex=e6ecfe + +# Loaded if color_mode is set to "ansi" +# Value: 0-255 +color_ansi=100 + diff --git a/src/main.rs b/src/main.rs index 128e179..f17f496 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,14 @@ +use core::panic; use std::{ + fs, io::{self, Write}, + path::PathBuf, thread, time::Duration, }; use chrono::{Local, Timelike}; +use clap::Parser; use config::Config; use crossterm::{ cursor, @@ -13,13 +17,42 @@ use crossterm::{ style::{self, Attribute, Color}, terminal::{self, ClearType}, }; +use dirs::config_dir; mod config; mod symbols; +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + #[arg(short, long, value_name = "FILE")] + config: Option, +} + +const DEFAULT_CONFIG: &str = include_str!("default_config"); + fn main() -> io::Result<()> { + let args = Args::parse(); + // Load config - let config = config::load_from_file("config"); + let config_file = if let Some(custom_config) = args.config { + PathBuf::from(custom_config) + } else { + let config_dir = config_dir().unwrap().join("tlock"); + let config_file = config_dir.clone().join("config"); + if !config_file.exists() { + // Generate default config + let _ = fs::create_dir(config_dir); + let _ = fs::write(config_file.clone(), DEFAULT_CONFIG); + } + + config_file + }; + if !config_file.exists() { + panic!("ERROR: Configuration file not found"); + } + + let config = config::load_from_file(&config_file.to_str().unwrap()); let mut stdout = io::stdout();