refactor: abstract away app mode
This commit is contained in:
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
atomic_enum = "0.2.0"
|
||||||
chrono = "0.4.31"
|
chrono = "0.4.31"
|
||||||
clap = { version = "4.4.18", features = ["derive", "cargo"] }
|
clap = { version = "4.4.18", features = ["derive", "cargo"] }
|
||||||
crossterm = "0.27.0"
|
crossterm = "0.27.0"
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use ini::configparser::ini::Ini;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
color::{generate_gradient, parse_hex_color, ComputableColor},
|
color::{generate_gradient, parse_hex_color, ComputableColor},
|
||||||
debug,
|
debug, get_app_mode, AppMode,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@@ -107,7 +107,9 @@ fn load_gradient(ini: &Ini) -> ComputableColor {
|
|||||||
i += 1;
|
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();
|
let mut loop_keys = keys.clone();
|
||||||
loop_keys.reverse();
|
loop_keys.reverse();
|
||||||
for i in 1..loop_keys.len() {
|
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
|
debug::DEBUG_COLOR_DISPLAY_SIZE * 2
|
||||||
} else {
|
} else {
|
||||||
ini.getuint("gradient", "gradient_steps")
|
ini.getuint("gradient", "gradient_steps")
|
||||||
|
|||||||
15
src/debug.rs
15
src/debug.rs
@@ -1,7 +1,4 @@
|
|||||||
use std::{
|
use std::io::{self, Write};
|
||||||
io::{self, Write},
|
|
||||||
sync::atomic::{AtomicBool, Ordering},
|
|
||||||
};
|
|
||||||
|
|
||||||
use clap::crate_version;
|
use clap::crate_version;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
@@ -13,8 +10,6 @@ use crate::config::Config;
|
|||||||
|
|
||||||
pub const DEBUG_COLOR_DISPLAY_SIZE: usize = 50;
|
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<()> {
|
pub fn print_debug_infos(config: &mut Config) -> io::Result<()> {
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
|
|
||||||
@@ -62,11 +57,3 @@ fn print_debug_label(key: &str) -> io::Result<()> {
|
|||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enable_debug_mode() -> () {
|
|
||||||
DEBUG_MODE.store(true, Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_debug() -> bool {
|
|
||||||
return DEBUG_MODE.load(Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
|
|||||||
21
src/main.rs
21
src/main.rs
@@ -2,10 +2,12 @@ use core::panic;
|
|||||||
use std::{
|
use std::{
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
sync::atomic::Ordering,
|
||||||
thread,
|
thread,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use atomic_enum::atomic_enum;
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use config::{write_default_config, Config};
|
use config::{write_default_config, Config};
|
||||||
@@ -46,12 +48,29 @@ enum Commands {
|
|||||||
Debug {},
|
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<()> {
|
fn main() -> io::Result<()> {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
match &cli.command {
|
match &cli.command {
|
||||||
Some(Commands::Debug {}) => {
|
Some(Commands::Debug {}) => {
|
||||||
debug::enable_debug_mode();
|
set_app_mode(AppMode::Debug);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user