feat: configurable app, implement terminal colors

This commit is contained in:
Pihkaal
2024-01-18 19:59:11 +01:00
parent 0fdd0e1253
commit e31f16d9a8
4 changed files with 104 additions and 6 deletions

View File

@@ -6,4 +6,5 @@ edition = "2021"
[dependencies]
chrono = "0.4.31"
crossterm = "0.27.0"
ini = "1.3.0"

32
config Normal file
View File

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

59
src/config.rs Normal file
View File

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

View File

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