refactor: abstract away app mode

This commit is contained in:
Pihkaal
2024-01-20 21:46:26 +01:00
parent 93a8b76459
commit f5ae5df561
4 changed files with 27 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
atomic_enum = "0.2.0"
chrono = "0.4.31"
clap = { version = "4.4.18", features = ["derive", "cargo"] }
crossterm = "0.27.0"

View File

@@ -6,7 +6,7 @@ use ini::configparser::ini::Ini;
use crate::{
color::{generate_gradient, parse_hex_color, ComputableColor},
debug,
debug, get_app_mode, AppMode,
};
pub struct Config {
@@ -107,7 +107,9 @@ fn load_gradient(ini: &Ini) -> ComputableColor {
i += 1;
}
if !debug::is_debug() && ini.getbool("gradient", "gradient_loop").unwrap().unwrap() {
if get_app_mode() != AppMode::Debug
&& ini.getbool("gradient", "gradient_loop").unwrap().unwrap()
{
let mut loop_keys = keys.clone();
loop_keys.reverse();
for i in 1..loop_keys.len() {
@@ -115,7 +117,7 @@ fn load_gradient(ini: &Ini) -> ComputableColor {
}
}
let steps: usize = if debug::is_debug() {
let steps: usize = if get_app_mode() == AppMode::Debug {
debug::DEBUG_COLOR_DISPLAY_SIZE * 2
} else {
ini.getuint("gradient", "gradient_steps")

View File

@@ -1,7 +1,4 @@
use std::{
io::{self, Write},
sync::atomic::{AtomicBool, Ordering},
};
use std::io::{self, Write};
use clap::crate_version;
use crossterm::{
@@ -13,8 +10,6 @@ use crate::config::Config;
pub const DEBUG_COLOR_DISPLAY_SIZE: usize = 50;
static DEBUG_MODE: AtomicBool = AtomicBool::new(false);
pub fn print_debug_infos(config: &mut Config) -> io::Result<()> {
let mut stdout = io::stdout();
@@ -62,11 +57,3 @@ fn print_debug_label(key: &str) -> io::Result<()> {
return Ok(());
}
pub fn enable_debug_mode() -> () {
DEBUG_MODE.store(true, Ordering::Relaxed);
}
pub fn is_debug() -> bool {
return DEBUG_MODE.load(Ordering::Relaxed);
}

View File

@@ -2,10 +2,12 @@ use core::panic;
use std::{
io::{self, Write},
path::PathBuf,
sync::atomic::Ordering,
thread,
time::Duration,
};
use atomic_enum::atomic_enum;
use chrono::Local;
use clap::{Parser, Subcommand};
use config::{write_default_config, Config};
@@ -46,12 +48,29 @@ enum Commands {
Debug {},
}
#[atomic_enum]
#[derive(PartialEq)]
pub enum AppMode {
Clock = 0,
Debug,
}
static APP_MODE: AtomicAppMode = AtomicAppMode::new(AppMode::Debug);
pub fn get_app_mode() -> AppMode {
return APP_MODE.load(Ordering::Relaxed);
}
pub fn set_app_mode(mode: AppMode) {
return APP_MODE.store(mode, Ordering::Relaxed);
}
fn main() -> io::Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Debug {}) => {
debug::enable_debug_mode();
set_app_mode(AppMode::Debug);
}
_ => {}
}