feat: configurable app, implement terminal colors
This commit is contained in:
@@ -6,4 +6,5 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4.31"
|
chrono = "0.4.31"
|
||||||
crossterm = "0.27.0"
|
crossterm = "0.27.0"
|
||||||
|
ini = "1.3.0"
|
||||||
|
|
||||||
|
|||||||
32
config
Normal file
32
config
Normal 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
59
src/config.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/main.rs
18
src/main.rs
@@ -5,6 +5,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use chrono::{Local, Timelike};
|
use chrono::{Local, Timelike};
|
||||||
|
use config::Config;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
cursor,
|
cursor,
|
||||||
event::{self, Event, KeyCode, KeyModifiers},
|
event::{self, Event, KeyCode, KeyModifiers},
|
||||||
@@ -13,9 +14,13 @@ use crossterm::{
|
|||||||
terminal::{self, ClearType},
|
terminal::{self, ClearType},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod config;
|
||||||
mod symbols;
|
mod symbols;
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
|
// Load config
|
||||||
|
let config = config::load_from_file("config");
|
||||||
|
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
|
|
||||||
// Switch to alternate screen, hide the cursor and enable raw mode
|
// 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))?;
|
queue!(stdout, terminal::Clear(ClearType::All))?;
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
render_frame()?;
|
render_frame(&config)?;
|
||||||
|
|
||||||
stdout.flush()?;
|
stdout.flush()?;
|
||||||
|
|
||||||
// 30fps
|
thread::sleep(Duration::from_millis(1000 / config.fps));
|
||||||
thread::sleep(Duration::from_millis(33));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disale raw mode, leave the alternate screen and show the cursor back
|
// 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)?;
|
execute!(stdout, terminal::LeaveAlternateScreen, cursor::Show)?;
|
||||||
|
|
||||||
// Be polite
|
// Be polite
|
||||||
println!("CTRL-C pressed, bye!\n");
|
if config.be_polite {
|
||||||
|
println!("CTRL-C pressed, bye!\n");
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_frame() -> io::Result<()> {
|
fn render_frame(config: &Config) -> io::Result<()> {
|
||||||
let (width, height) = terminal::size()?;
|
let (width, height) = terminal::size()?;
|
||||||
|
|
||||||
let time = Local::now();
|
let time = Local::now();
|
||||||
@@ -73,7 +79,7 @@ fn render_frame() -> io::Result<()> {
|
|||||||
// Display current time
|
// Display current time
|
||||||
let text_width = 6 + 7 + 6 + 7 + 6;
|
let text_width = 6 + 7 + 6 + 7 + 6;
|
||||||
let text_height = 5;
|
let text_height = 5;
|
||||||
let color = Color::White;
|
let color = config.color;
|
||||||
|
|
||||||
let x = width / 2 - text_width / 2;
|
let x = width / 2 - text_width / 2;
|
||||||
let y = height / 2 - text_height / 2;
|
let y = height / 2 - text_height / 2;
|
||||||
|
|||||||
Reference in New Issue
Block a user