hlctl/src/main.rs

104 lines
2.7 KiB
Rust

#![feature(macro_metavar_expr)]
use std::path::{Path, PathBuf};
use clap::{Parser, Subcommand};
use config::Config;
use log::{error, info};
mod config;
mod hlwm;
#[derive(Parser, Debug, Clone)]
#[command(name = "hlctl")]
struct Args {
#[command(subcommand)]
command: HlctlCommand,
}
#[derive(Subcommand, Debug, Clone)]
enum HlctlCommand {
/// Initialize herbstluftwm, should be run from the autostart script
Init,
#[command(arg_required_else_help = true)]
/// Draws notifications using dzen2
Notify {
/// Amount of seconds to persist for
#[arg(short = 's', long = None, default_value_t = 1)]
seconds: u8,
},
/// Save the currently loaded configuration to file
#[command(long_about = r#"
`save` Tries to find an existing config file. If not present, the default config is used.
Whichever one is loaded, its tags are used. All other values are collected from the environment
(or default values) and this new config is saved.
The configuration file located at $HOME/.config/herbstluftwm/hlctl.toml"#)]
Save,
PrintConfig,
Panel,
}
fn main() {
pretty_env_logger::init();
let args = Args::parse();
match args.command {
HlctlCommand::Init => init(),
HlctlCommand::Notify { seconds } => println!("notify for {seconds}"),
HlctlCommand::Save => save(),
HlctlCommand::PrintConfig => print_config(),
HlctlCommand::Panel => todo!(),
}
}
fn load_or_default_config() -> Config {
Config::from_file(&Path::new(&Config::default_path().unwrap())).unwrap_or(Config::default())
}
fn x_set_root(path: PathBuf) {
match std::process::Command::new(path)
.args(["-solid", "black"])
.spawn()
{
Ok(mut child) => {
if let Err(err) = child.wait() {
error!("running xsetroot: [{err}]");
}
}
Err(err) => error!("running xsetroot: [{err}]"),
}
}
fn init() {
info!("begining herbstluftwm setup via hlctl");
if let Ok(path) = which::which("xsetroot") {
x_set_root(path);
}
info!("loading config");
hlwm::Client::new()
.execute_iter(
load_or_default_config()
.to_command_set()
.expect("marshalling init command set"),
)
.expect("running init command set");
}
fn merged_config() -> Config {
let default = load_or_default_config();
let mut collected = Config::from_herbstluft();
collected.tags = default.tags;
collected
}
fn print_config() {
println!("{}", merged_config().serialize().unwrap())
}
fn save() {
merged_config()
.write_to_file(&Path::new(&Config::default_path().unwrap()))
.expect("failed writing to file");
}