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:
parent
7413f78e04
commit
a33e0a241a
1 changed files with 22 additions and 5 deletions
27
src/cli.rs
27
src/cli.rs
|
@ -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")
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue