From e31f16d9a8816f1bccc76484f0aced243f180383 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Thu, 18 Jan 2024 19:59:11 +0100 Subject: [PATCH] feat: configurable app, implement terminal colors --- Cargo.toml | 1 + config | 32 ++++++++++++++++++++++++++++ src/config.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 18 ++++++++++------ 4 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 config create mode 100644 src/config.rs diff --git a/Cargo.toml b/Cargo.toml index 3873843..2ba217b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" [dependencies] chrono = "0.4.31" crossterm = "0.27.0" +ini = "1.3.0" diff --git a/config b/config new file mode 100644 index 0000000..e70870b --- /dev/null +++ b/config @@ -0,0 +1,32 @@ +# tlock config file + +[general] + +# Say goodbye when you press CTRL-C +# Value: true, false +polite=true + +# FPS +# Value: int +fps=30 + +[styling] + +# Which color mode to use +# Value: "term", "rgb" 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 "rgb" +# Value: 0-255 +color_r=255 +color_g=0 +color_b=0 + +# Loaded if color_mode is set to "ansi" +# Value: 0-255 +color_ansi=100 + diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..71c32b1 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,59 @@ +use core::panic; + +use crossterm::style::Color; +use ini::configparser::ini::Ini; + +pub struct Config { + pub be_polite: bool, + pub fps: u64, + pub color: Color, +} + +pub fn load_from_file(path: &str) -> Config { + let mut ini = Ini::new(); + ini.load(path).unwrap(); + + let config = Config { + be_polite: ini.getbool("general", "polite").unwrap().unwrap(), + fps: ini.getuint("general", "fps").unwrap().unwrap(), + color: load_color(&ini), + }; + + return config; +} + +fn load_color(ini: &Ini) -> Color { + let color_mode = ini.get("styling", "color_mode").unwrap(); + + match color_mode.as_str() { + "term" => { + let color = ini.getint("styling", "color_term").unwrap().unwrap(); + return load_term_color(color); + } + "rgb" => todo!(), + "ansi" => todo!(), + _ => panic!("ERROR: Invalid color mode: {}", color_mode), + } +} + +fn load_term_color(value: i64) -> Color { + match value { + 0 => Color::Black, + 1 => Color::DarkRed, + 2 => Color::DarkGreen, + 3 => Color::DarkYellow, + 4 => Color::DarkBlue, + 5 => Color::DarkMagenta, + 6 => Color::DarkCyan, + 7 => Color::Grey, + 8 => Color::DarkGrey, + 9 => Color::Red, + 10 => Color::Green, + 11 => Color::Yellow, + 12 => Color::Blue, + 13 => Color::Magenta, + 14 => Color::Cyan, + 15 => Color::White, + _ => panic!("ERROR: Invalid terminal color: {}", value), + } +} diff --git a/src/main.rs b/src/main.rs index 48b7dd1..ced1e62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::{ }; use chrono::{Local, Timelike}; +use config::Config; use crossterm::{ cursor, event::{self, Event, KeyCode, KeyModifiers}, @@ -13,9 +14,13 @@ use crossterm::{ terminal::{self, ClearType}, }; +mod config; mod symbols; fn main() -> io::Result<()> { + // Load config + let config = config::load_from_file("config"); + let mut stdout = io::stdout(); // Switch to alternate screen, hide the cursor and enable raw mode @@ -45,12 +50,11 @@ fn main() -> io::Result<()> { queue!(stdout, terminal::Clear(ClearType::All))?; // Render - render_frame()?; + render_frame(&config)?; stdout.flush()?; - // 30fps - thread::sleep(Duration::from_millis(33)); + thread::sleep(Duration::from_millis(1000 / config.fps)); } // Disale raw mode, leave the alternate screen and show the cursor back @@ -58,12 +62,14 @@ fn main() -> io::Result<()> { execute!(stdout, terminal::LeaveAlternateScreen, cursor::Show)?; // Be polite - println!("CTRL-C pressed, bye!\n"); + if config.be_polite { + println!("CTRL-C pressed, bye!\n"); + } return Ok(()); } -fn render_frame() -> io::Result<()> { +fn render_frame(config: &Config) -> io::Result<()> { let (width, height) = terminal::size()?; let time = Local::now(); @@ -73,7 +79,7 @@ fn render_frame() -> io::Result<()> { // Display current time let text_width = 6 + 7 + 6 + 7 + 6; let text_height = 5; - let color = Color::White; + let color = config.color; let x = width / 2 - text_width / 2; let y = height / 2 - text_height / 2;