1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-20 11:54:23 +02:00
ironbar/src/cli.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

use crate::error::ExitCode;
2023-06-22 23:06:45 +01:00
use crate::ipc::commands::Command;
use crate::ipc::responses::Response;
use clap::{Parser, ValueEnum};
2023-06-22 23:06:45 +01:00
use serde::{Deserialize, Serialize};
use std::process::exit;
2023-06-22 23:06:45 +01:00
#[derive(Parser, Debug, Serialize, Deserialize)]
#[command(version)]
pub struct Args {
#[command(subcommand)]
pub command: Option<Command>,
/// Prints the config JSON schema to `stdout`
/// and exits.
#[cfg(feature = "schema")]
#[arg(long("print-schema"))]
pub print_schema: bool,
2024-05-18 17:00:27 +01:00
/// Print debug information to stderr
/// TODO: Make bar follow this too
#[arg(long)]
pub debug: bool,
/// Format to output the response as.
#[arg(short, long)]
pub format: Option<Format>,
/// `bar_id` argument passed by `swaybar_command`.
/// Not used.
#[arg(short('b'), hide(true))]
sway_bar_id: Option<String>,
2023-06-22 23:06:45 +01:00
}
#[derive(Debug, Serialize, Deserialize, Default, ValueEnum, Clone, Copy)]
pub enum Format {
#[default]
Plain,
Json,
}
pub fn handle_response(response: Response, format: Format) {
let is_err = matches!(response, Response::Err { .. });
match format {
Format::Plain => match response {
Response::Ok => println!("ok"),
Response::OkValue { value } => println!("{value}"),
Response::Err { message } => eprintln!("error\n{}", message.unwrap_or_default()),
},
Format::Json => println!(
"{}",
serde_json::to_string(&response).expect("to be valid json")
),
2023-06-22 23:06:45 +01:00
}
if is_err {
exit(ExitCode::IpcResponseError as i32)
}
2023-06-22 23:06:45 +01:00
}