msg: add --print-request flag and raw-request subcommand
Browse files at this commit9a979e6f38f9e2ea1b9184836b87cdba947cdf4fParent: 66d04a73 changed files +26 -3
- src/cli.rs +5 -0
- src/ipc/client.rs +15 -1
- src/main.rs +6 -2
src/cli.rsmodified+5 -0
| @@ -43,6 +43,9 @@ pub enum Sub { | ||
| 43 | 43 | /// Format output as JSON. |
| 44 | 44 | #[arg(short, long)] |
| 45 | 45 | json: bool, |
| 46 | + /// Print the IPC request as JSON instead of sending it. | |
| 47 | + #[arg(long)] | |
| 48 | + print_request: bool, | |
| 46 | 49 | }, |
| 47 | 50 | /// Validate the config file. |
| 48 | 51 | Validate { |
| @@ -109,6 +112,8 @@ pub enum Msg { | ||
| 109 | 112 | OverviewState, |
| 110 | 113 | /// List screencasts. |
| 111 | 114 | Casts, |
| 115 | + /// Send a raw JSON request to the compositor, reading from stdin. | |
| 116 | + RawRequest, | |
| 112 | 117 | } |
| 113 | 118 | |
| 114 | 119 | #[derive(Clone, Debug, clap::ValueEnum)] |
src/ipc/client.rsmodified+15 -1
| @@ -15,7 +15,7 @@ use serde_json::json; | ||
| 15 | 15 | use crate::cli::Msg; |
| 16 | 16 | use crate::utils::version; |
| 17 | 17 | |
| 18 | -pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { | |
| 18 | +pub fn handle_msg(mut msg: Msg, json: bool, print_request: bool) -> anyhow::Result<()> { | |
| 19 | 19 | // For actions taking paths, prepend the niri CLI's working directory. |
| 20 | 20 | if let Msg::Action { |
| 21 | 21 | action: |
| @@ -49,8 +49,18 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { | ||
| 49 | 49 | Msg::RequestError => Request::ReturnError, |
| 50 | 50 | Msg::OverviewState => Request::OverviewState, |
| 51 | 51 | Msg::Casts => Request::Casts, |
| 52 | + Msg::RawRequest => { | |
| 53 | + let stdin = std::io::stdin(); | |
| 54 | + serde_json::from_reader(stdin).context("error parsing request JSON from stdin")? | |
| 55 | + } | |
| 52 | 56 | }; |
| 53 | 57 | |
| 58 | + if print_request { | |
| 59 | + let json_str = | |
| 60 | + serde_json::to_string(&request).context("error formatting request as JSON")?; | |
| 61 | + println!("{json_str}"); | |
| 62 | + return Ok(()); | |
| 63 | + } | |
| 54 | 64 | let mut socket = Socket::connect().context("error connecting to the niri socket")?; |
| 55 | 65 | |
| 56 | 66 | let result = socket.send(request); |
| @@ -550,6 +560,10 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { | ||
| 550 | 560 | println!(); |
| 551 | 561 | } |
| 552 | 562 | } |
| 563 | + Msg::RawRequest => { | |
| 564 | + let output = serde_json::to_string(&response).context("error formatting response")?; | |
| 565 | + println!("{output}"); | |
| 566 | + } | |
| 553 | 567 | } |
| 554 | 568 | |
| 555 | 569 | Ok(()) |
src/main.rsmodified+6 -2
| @@ -106,8 +106,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 106 | 106 | info!("config is valid"); |
| 107 | 107 | return Ok(()); |
| 108 | 108 | } |
| 109 | - Sub::Msg { msg, json } => { | |
| 110 | - handle_msg(msg, json)?; | |
| 109 | + Sub::Msg { | |
| 110 | + msg, | |
| 111 | + json, | |
| 112 | + print_request, | |
| 113 | + } => { | |
| 114 | + handle_msg(msg, json, print_request)?; | |
| 111 | 115 | return Ok(()); |
| 112 | 116 | } |
| 113 | 117 | Sub::Panic => cause_panic(), |