Skip to content

Commit

Permalink
✨ add a config file, mention it in the notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ad2ien committed Feb 6, 2024
1 parent de94bcf commit 9926ce5
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ lazy_static = "1.4.0"
notify-rust = "4.10.0"
rdev = "0.5.3"
regex = "1.10.3"
serde = "1.0.196"
serde_yaml = "0.9.31"
state = "0.6.0"
7 changes: 7 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# caps lock config
correct_caps_lock: true
correct_suspicious_last_word: true
display_warning: true

# If everything is set to false,
# consider uninstalling this package ..
31 changes: 31 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::Read;
use std::path::Path;

// Define a struct that matches your YAML structure
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
correct_caps_lock: bool,
correct_suspicious_last_word: bool,
display_warning: bool,
}

impl Config {
pub fn default() -> Config {
Config {
correct_caps_lock: true,
correct_suspicious_last_word: true,
display_warning: true,
}
}
}

pub fn get_config() -> Result<Config, Box<dyn std::error::Error>> {
let config_path = Path::new("./config.yaml");
let mut file = File::open(config_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config: Config = serde_yaml::from_str(&contents)?;
Ok(config)
}
48 changes: 45 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::{sync::RwLock, thread, time};
use std::{env, path::PathBuf, sync::RwLock, thread, time};

use notify_rust::Notification;
use rdev::{listen, simulate, Event, EventType, Key};
use state::InitCell;
mod config;

static STATE: InitCell<RwLock<CapsLockAutoSwitchState>> = InitCell::new();

lazy_static! {
static ref WRONG_CAPS_DETECTION: Regex = Regex::new(r"[a-z]{1}[A-Z]{2,} ").unwrap();
static ref NOT_WORD: Regex = Regex::new(r"[^a-zA-Z]{1,}").unwrap();
#[derive(Debug)]
static ref CURRENT_EXE: PathBuf = match env::current_exe() {
Ok(exe_path) => exe_path,
Err(_e) => PathBuf::from(""),
};
static ref CONFIG: config::Config = match config::get_config() {
Ok(cfg) => cfg,
Err(_) => {
print!("Error loading config, using default");
config::Config::default()
}
};

}

struct CapsLockAutoSwitchState {
Expand All @@ -25,6 +39,13 @@ enum BufferStatus {

fn main() {
println!("Start Caps-Lock Auto Switch!");
println!(
"Current exe: {}",
match CURRENT_EXE.to_str() {
Some(s) => s,
None => "unknown",
}
);

let state = CapsLockAutoSwitchState {
input: String::new(),
Expand Down Expand Up @@ -88,17 +109,38 @@ fn wrong_case_detected() {
let state = STATE.get().read().unwrap();

correct_caps(state.input.clone());
do_notification(state);
}

fn do_notification(state: std::sync::RwLockReadGuard<'_, CapsLockAutoSwitchState>) {
let template_message = ": are you sure about the case of this word?";

let message = match CURRENT_EXE.to_str() {
Some(s) => {
format!(
"{}{}\nYou can change the settings there : {}/config.yaml",
state.input, template_message, s
)
}
None => {
format!("{}{}", template_message, state.input)
}
};

Notification::new()
.summary("Wrong case detected!")
.body(format!("Are you sure about the case of this word : {}", state.input).as_str())
.body(message.as_str())
.icon("dialog-information")
.timeout(10000)
.show()
.unwrap();
}

fn correct_caps(problematic_word: String) {
println!("correct caps send event. problematic_word: {}", problematic_word);
println!(
"correct caps send event. problematic_word: {}",
problematic_word
);

let mut first = true;

Expand Down

0 comments on commit 9926ce5

Please sign in to comment.