feat: custom config path and default config

This commit is contained in:
Pihkaal
2024-01-19 12:32:32 +01:00
parent 56ebe073fc
commit cc61c5bcd4
3 changed files with 73 additions and 1 deletions

View File

@@ -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"

37
src/default_config Normal file
View File

@@ -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

View File

@@ -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<String>,
}
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();