use crate::{
args::GapsVariant,
utils::{exec, get_pwd},
};
use serde_json::Value;
pub fn handle_gaps_command(variant: GapsVariant) {
match variant {
GapsVariant::Increase => increase_gaps(),
GapsVariant::Decrease => decrease_gaps(),
GapsVariant::Reset => reset_gaps(),
}
}
fn increase_gaps() {
let target_gap = get_current_gap_size() + 5;
match target_gap {
x if x > 20 => set_inner_gap(5),
x if x > 0 => set_inner_gap(2),
_ => set_inner_gap(0),
}
set_outer_gap(target_gap);
}
fn decrease_gaps() {
let target_gap = get_current_gap_size() - 5;
if target_gap < 0 {
return;
}
match target_gap {
x if x > 20 => set_inner_gap(5),
x if x > 0 => set_inner_gap(2),
_ => set_inner_gap(0),
}
set_outer_gap(target_gap);
}
fn reset_gaps() {
set_inner_gap(0);
set_outer_gap(0);
}
fn get_current_gap_size() -> i8 {
match exec(
"hyprctl getoption general:gaps_out -j",
get_pwd(None).as_path(),
) {
Ok(o) => {
let output = match String::from_utf8(o.stdout) {
Ok(output) => output,
Err(err) => {
println!("Error parsing hyprctl output: {err}");
std::process::exit(1);
}
};
let json_value: Value = serde_json::from_str(&output).unwrap();
let value: i8 = json_value["int"]
.as_i64()
.unwrap()
.try_into()
.unwrap_or_default();
return value;
}
Err(err) => {
println!("Error executing hyprctl: {err}");
std::process::exit(1);
}
}
}
fn set_inner_gap(target_gap: i8) {
let command = format!("hyprctl keyword general:gaps_in {target_gap}");
match exec(command.as_str(), get_pwd(None).as_path()) {
Ok(_) => {
println!("ok");
}
Err(err) => {
println!("Failed to set gaps: {err}");
std::process::exit(1);
}
}
}
fn set_outer_gap(target_gap: i8) {
let command = format!("hyprctl keyword general:gaps_out {target_gap}");
match exec(command.as_str(), get_pwd(None).as_path()) {
Ok(_) => {
println!("ok");
}
Err(err) => {
println!("Failed to set gaps: {err}");
std::process::exit(1);
}
}
}