Skip to content

Commit

Permalink
🔧 use a proper config file
Browse files Browse the repository at this point in the history
  • Loading branch information
ad2ien committed Feb 24, 2024
1 parent cca25ab commit 1c609cc
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 44 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
dirs = "5.0.1"
lazy_static = "1.4.0"
notify-rust = "4.10.0"
rdev = "0.5.3"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ Only tried on Pop!_OS 22.04 LTS. Should work on any Debian based distri with X11

## TODO

- [ ] uninstall should remove service
- [x] uninstall should remove service
- [x] config file for a debian package?
- [ ] commands : help, edit configuration...
- [ ] check install/remove, upgrade on a clean system
- [ ] config file for a debian package
- [ ] only define project variable once : description version..
- [ ] manage languages special characters
- [ ] debian package lint warnings
Expand All @@ -48,6 +49,5 @@ Only tried on Pop!_OS 22.04 LTS. Should work on any Debian based distri with X11
- [ ] install instructions
- [ ] readme badges
- [ ] man page
- [ ] commands : help, edit configuration...
- [ ] dockerize build
- [ ] logs
1 change: 1 addition & 0 deletions debian/DEBIAN/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ systemctl --machine="[email protected]" --user daemon-reload

rm -f /etc/xdg/systemd/user/capslock-auto-switch.service
rm -f /etc/systemd/user/capslock-auto-switch.service
rm -rf /home/$SUDO_USER/.config/capslock-auto-switch
65 changes: 53 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use dirs;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::fs::{self, File};
use std::io::{Read, Write};

// 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,
pub correct_caps_lock: bool,
pub correct_suspicious_last_word: bool,
pub display_warning: bool,
pub path: String,
}

impl Config {
Expand All @@ -17,15 +18,55 @@ impl Config {
correct_caps_lock: true,
correct_suspicious_last_word: true,
display_warning: true,
path: String::new()
}
}
pub fn default_with_path(path: String) -> Config {
Config {
correct_caps_lock: true,
correct_suspicious_last_word: true,
display_warning: true,
path: path
}
}
}

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)
if let Some(mut config_dir) = dirs::config_dir() {
config_dir.push("capslock-auto-switch");
let _ = fs::create_dir_all(config_dir.clone());
config_dir.push("config.yml");
println!("Config directory should be: {:?}", config_dir);

let file = File::open(config_dir.clone());
match file {
Ok(mut f) => {
let mut contents = String::new();
f.read_to_string(&mut contents)?;
let config: Config = serde_yaml::from_str(&contents)?;
Ok(config)
}
Err(_) => {
println!("Creating default config file {}", config_dir.clone().to_str().unwrap());
let config = Config::default_with_path(config_dir.to_str().unwrap().to_string());
let ff = File::create(config_dir.clone());
match ff {
Ok(mut file) => {
println!("Default config file created");
let contents = serde_yaml::to_string(&config)?;
file.write_all(contents.as_bytes())?;
Ok(config)
}
Err( err ) => {
println!("Unable to create config file {:?}", err);
Ok(Config::default())
}

}
}
}
} else {
println!("Unable to find config directory");
Ok(Config::default())
}
}
38 changes: 9 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use lazy_static::lazy_static;
use std::{env, path::PathBuf, sync::RwLock, thread, time};
use std::{sync::RwLock, thread, time};

use buffer_process::{analyse_str_state, BufferStatus};
use notify_rust::Notification;
use rdev::{listen, simulate, Event, EventType, Key};
use buffer_process::{analyse_str_state, BufferStatus};
use state::InitCell;

mod buffer_process;
Expand All @@ -12,19 +12,13 @@ mod config;
static STATE: InitCell<RwLock<CapsLockAutoSwitchState>> = InitCell::new();

lazy_static! {
#[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(_) => {
println!("Error loading config, using default");
config::Config::default()
}
};

}

struct SingleEvent {
Expand All @@ -51,13 +45,7 @@ impl CapsLockAutoSwitchState {

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

let state = CapsLockAutoSwitchState::new();
STATE.set(RwLock::new(state));
Expand Down Expand Up @@ -108,7 +96,6 @@ fn analyse_state() -> BufferStatus {
analyse_str_state(state.to_string())
}


fn reset_buffer() {
println!("reset buffer");
match STATE.get().try_write() {
Expand All @@ -130,19 +117,12 @@ fn do_notification() {
let state = STATE.get().read().unwrap();
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.to_string(),
template_message,
s
)
}
None => {
format!("{}{}", template_message, state.to_string())
}
};
let message = format!(
"{}{}\nYou can change the settings there : {}",
state.to_string(),
template_message,
CONFIG.path
);

Notification::new()
.summary("Wrong case detected!")
Expand Down

0 comments on commit 1c609cc

Please sign in to comment.