feat: custom config path and default config
This commit is contained in:
@@ -5,6 +5,8 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4.31"
|
chrono = "0.4.31"
|
||||||
|
clap = { version = "4.4.18", features = ["derive"] }
|
||||||
crossterm = "0.27.0"
|
crossterm = "0.27.0"
|
||||||
|
dirs = "5.0.1"
|
||||||
ini = "1.3.0"
|
ini = "1.3.0"
|
||||||
|
|
||||||
|
|||||||
37
src/default_config
Normal file
37
src/default_config
Normal 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
|
||||||
|
|
||||||
35
src/main.rs
35
src/main.rs
@@ -1,10 +1,14 @@
|
|||||||
|
use core::panic;
|
||||||
use std::{
|
use std::{
|
||||||
|
fs,
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
|
path::PathBuf,
|
||||||
thread,
|
thread,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use chrono::{Local, Timelike};
|
use chrono::{Local, Timelike};
|
||||||
|
use clap::Parser;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
cursor,
|
cursor,
|
||||||
@@ -13,13 +17,42 @@ use crossterm::{
|
|||||||
style::{self, Attribute, Color},
|
style::{self, Attribute, Color},
|
||||||
terminal::{self, ClearType},
|
terminal::{self, ClearType},
|
||||||
};
|
};
|
||||||
|
use dirs::config_dir;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod symbols;
|
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<()> {
|
fn main() -> io::Result<()> {
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
// Load config
|
// 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();
|
let mut stdout = io::stdout();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user