hlctl/src/main.rs

104 lines
2.6 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;
mod hlwm;
mod panel;
#[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 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,
/// 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_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");
}