1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +02:00

feat(cli): format flag, json output format

This commit is contained in:
Jake Stanger 2024-05-18 17:01:17 +01:00
parent 7413f78e04
commit a33e0a241a
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61

View file

@ -20,16 +20,33 @@ pub struct Args {
#[arg(long)] #[arg(long)]
pub debug: bool, pub debug: bool,
/// Format to output the response as.
#[arg(short, long)]
pub format: Option<Format>,
/// `bar_id` argument passed by `swaybar_command`. /// `bar_id` argument passed by `swaybar_command`.
/// Not used. /// Not used.
#[arg(short('b'), hide(true))] #[arg(short('b'), hide(true))]
sway_bar_id: Option<String>, sway_bar_id: Option<String>,
} }
pub fn handle_response(response: Response) { #[derive(Debug, Serialize, Deserialize, Default, ValueEnum, Clone, Copy)]
match response { pub enum Format {
Response::Ok => println!("ok"), #[default]
Response::OkValue { value } => println!("ok\n{value}"), Plain,
Response::Err { message } => eprintln!("error\n{}", message.unwrap_or_default()), Json,
}
pub fn handle_response(response: Response, format: Format) {
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")
),
} }
} }