From 9a979e6f38f9e2ea1b9184836b87cdba947cdf4f Mon Sep 17 00:00:00 2001 From: goatnath Date: Sun, 5 Jul 2026 23:26:43 +0530 Subject: [PATCH] msg: add --print-request flag and raw-request subcommand --- src/cli.rs | 5 +++++ src/ipc/client.rs | 16 +++++++++++++++- src/main.rs | 8 ++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 308b3127..df69aa14 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -43,6 +43,9 @@ pub enum Sub { /// Format output as JSON. #[arg(short, long)] json: bool, + /// Print the IPC request as JSON instead of sending it. + #[arg(long)] + print_request: bool, }, /// Validate the config file. Validate { @@ -109,6 +112,8 @@ pub enum Msg { OverviewState, /// List screencasts. Casts, + /// Send a raw JSON request to the compositor, reading from stdin. + RawRequest, } #[derive(Clone, Debug, clap::ValueEnum)] diff --git a/src/ipc/client.rs b/src/ipc/client.rs index 216585ce..cf6fb64c 100644 --- a/src/ipc/client.rs +++ b/src/ipc/client.rs @@ -15,7 +15,7 @@ use serde_json::json; use crate::cli::Msg; use crate::utils::version; -pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { +pub fn handle_msg(mut msg: Msg, json: bool, print_request: bool) -> anyhow::Result<()> { // For actions taking paths, prepend the niri CLI's working directory. if let Msg::Action { action: @@ -49,8 +49,18 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { Msg::RequestError => Request::ReturnError, Msg::OverviewState => Request::OverviewState, Msg::Casts => Request::Casts, + Msg::RawRequest => { + let stdin = std::io::stdin(); + serde_json::from_reader(stdin).context("error parsing request JSON from stdin")? + } }; + if print_request { + let json_str = + serde_json::to_string(&request).context("error formatting request as JSON")?; + println!("{json_str}"); + return Ok(()); + } let mut socket = Socket::connect().context("error connecting to the niri socket")?; let result = socket.send(request); @@ -550,6 +560,10 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { println!(); } } + Msg::RawRequest => { + let output = serde_json::to_string(&response).context("error formatting response")?; + println!("{output}"); + } } Ok(()) diff --git a/src/main.rs b/src/main.rs index 8878b56e..f9563ae6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,8 +106,12 @@ fn main() -> Result<(), Box> { info!("config is valid"); return Ok(()); } - Sub::Msg { msg, json } => { - handle_msg(msg, json)?; + Sub::Msg { + msg, + json, + print_request, + } => { + handle_msg(msg, json, print_request)?; return Ok(()); } Sub::Panic => cause_panic(),