hlctl/src/main.rs

117 lines
2.9 KiB
Rust

#![feature(macro_metavar_expr)]
use std::path::{Path, PathBuf};
use clap::{Parser, Subcommand};
use config::Config;
use log::{error, info};
pub mod cmd;
mod config;
pub mod environ;
mod hlwm;
pub mod logerr;
mod panel;
pub mod split;
#[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,
/// 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 mousebinds and attribute set 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,
/// Print the toml config to stdout
PrintConfig,
/// Start the top panel
Panel,
}
fn main() {
pretty_env_logger::init();
let args = Args::parse();
match args.command {
HlctlCommand::Init => init(),
HlctlCommand::Save => save(),
HlctlCommand::PrintConfig => print_config(),
HlctlCommand::Panel => {
if let Err(err) = panel::panel(&merged_config()) {
error!("panel: {err}");
}
}
}
}
fn load_config() -> Config {
match Config::from_file(&Path::new(&Config::default_path().unwrap())) {
Ok(cfg) => cfg,
Err(err) => {
error!("Could not load config. Error: {err}");
error!("");
error!("Loading default config");
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_config()
.to_command_set()
.expect("marshalling init command set"),
)
.expect("running init command set");
}
fn merged_config() -> Config {
let default = load_config();
let mut collected = Config::from_herbstluft();
collected.mousebinds = default.mousebinds;
collected.attributes = default.attributes;
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");
}