Skip to content

Initial version of Windows support #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 @@ -15,3 +15,4 @@ serde_json = { version = "1.0.40", features = ["preserve_order"] }
dirs = "2.0.2"
ttyecho = "0.1.2"
structopt = "0.2.18"
winapi = { version = "0.3.7", features = ["winuser"] }
84 changes: 67 additions & 17 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
use std::io::{ stdin };
#[cfg(target_os = "linux")]
use std::fs;
use std::process::{ exit };
use crate::switch::{ SwitchRegistry, SwitchCategory };
#[cfg(target_os = "linux")]
use ttyecho::{ ttyecho };
#[cfg(target_os = "windows")]
use winapi::{
um::{
winreg::{ RegOpenKeyExA, RegSetValueExA, RegCloseKey, HKEY_CURRENT_USER, LSTATUS },
winuser:: { SendMessageTimeoutA, HWND_BROADCAST, WM_SETTINGCHANGE, SMTO_ABORTIFHUNG },
winnt::{ KEY_ALL_ACCESS, REG_SZ }
},
shared::{
minwindef::{ HKEY },
winerror::{ ERROR_SUCCESS }
}
};

pub fn set(registry: &mut SwitchRegistry, category_name: String, name: String, value: String) {
let category = registry.get_category(&category_name);
Expand Down Expand Up @@ -72,25 +86,61 @@ pub fn apply(registry: &mut SwitchRegistry, mut category_name: String , mut name

let category = registry.get_category(&category_name);

match category {
Some(category) => {
let pts = match fs::canonicalize("/proc/self/fd/0") {
Ok(pts) => pts,
Err(why) => panic!("Could not retrieve pty: {:#?}", why)
};
#[cfg(target_os = "windows")]
{
match category {
Some(category) => {
match category.get_variable(&name) {
Some(env) => {
unsafe {
let mut hkey: HKEY = std::ptr::null_mut();
let env_var_reg_key = "Environment";
let status: LSTATUS = RegOpenKeyExA(HKEY_CURRENT_USER, env_var_reg_key.as_ptr() as *const i8, 0, KEY_ALL_ACCESS, &mut hkey);

match category.get_variable(&name) {
Some(env) => {
ttyecho(pts.to_str().unwrap().to_string(), format!("export {} && clear", env.value), true);
println!("Environment variables from category '{}' applied!", category_name);
}
_ => {
eprintln!("Could not find environment variable with name '{}' in category '{}'", &name, &category_name);
if status == ERROR_SUCCESS as i32 {
let status = RegSetValueExA(hkey, env.key.as_ptr() as *const i8, 0, REG_SZ, env.value.as_ptr(), env.value.len() as u32);
RegCloseKey(hkey);

if status == ERROR_SUCCESS as i32 {
let status = SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment".as_ptr() as isize, SMTO_ABORTIFHUNG, 1000, std::ptr::null_mut());
println!("Environment variable set!");
}
}
}
}
_ => {
eprintln!("Could not find environment variable with name '{}' in category '{}'", &name, &category_name);
}
}
};
},
None => {
eprintln!("Given category name is not registered!");
}
_ => {
eprintln!("Given category name is not registered!");
}
}

}
#[cfg(target_os = "linux")]
{
match category {
Some(category) => {
let pts = match fs::canonicalize("/proc/self/fd/0") {
Ok(pts) => pts,
Err(why) => panic!("Could not retrieve pty: {:#?}", why)
};

match category.get_variable(&name) {
Some(env) => {
ttyecho(pts.to_str().unwrap().to_string(), format!("export {} && clear", env.value), true);
println!("Environment variables from category '{}' applied!", category_name);
}
_ => {
eprintln!("Could not find environment variable with name '{}' in category '{}'", &name, &category_name);
}
};
},
None => {
eprintln!("Given category name is not registered!");
}
}
}
}
Expand Down