forge
Explore
Log in

Don't you dare go Hollow

msg: add --print-request flag and raw-request subcommand

Browse files at this commit

9a979e6f38f9e2ea1b9184836b87cdba947cdf4fgoatnathParent: 66d04a7
src/cli.rsmodified+5 -0
@@ -43,6 +43,9 @@ pub enum Sub {
43 /// Format output as JSON.43 /// Format output as JSON.
44 #[arg(short, long)]44 #[arg(short, long)]
45 json: bool,45 json: bool,
46+ /// Print the IPC request as JSON instead of sending it.
47+ #[arg(long)]
48+ print_request: bool,
46 },49 },
47 /// Validate the config file.50 /// Validate the config file.
48 Validate {51 Validate {
@@ -109,6 +112,8 @@ pub enum Msg {
109 OverviewState,112 OverviewState,
110 /// List screencasts.113 /// List screencasts.
111 Casts,114 Casts,
115+ /// Send a raw JSON request to the compositor, reading from stdin.
116+ RawRequest,
112 }117 }
113 118
114 #[derive(Clone, Debug, clap::ValueEnum)]119 #[derive(Clone, Debug, clap::ValueEnum)]
src/ipc/client.rsmodified+15 -1
@@ -15,7 +15,7 @@ use serde_json::json;
15 use crate::cli::Msg;15 use crate::cli::Msg;
16 use crate::utils::version;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 // For actions taking paths, prepend the niri CLI's working directory.19 // For actions taking paths, prepend the niri CLI's working directory.
20 if let Msg::Action {20 if let Msg::Action {
21 action:21 action:
@@ -49,8 +49,18 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
49 Msg::RequestError => Request::ReturnError,49 Msg::RequestError => Request::ReturnError,
50 Msg::OverviewState => Request::OverviewState,50 Msg::OverviewState => Request::OverviewState,
51 Msg::Casts => Request::Casts,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 let mut socket = Socket::connect().context("error connecting to the niri socket")?;64 let mut socket = Socket::connect().context("error connecting to the niri socket")?;
55 65
56 let result = socket.send(request);66 let result = socket.send(request);
@@ -550,6 +560,10 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
550 println!();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 Ok(())569 Ok(())
src/main.rsmodified+6 -2
@@ -106,8 +106,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
106 info!("config is valid");106 info!("config is valid");
107 return Ok(());107 return Ok(());
108 }108 }
109- Sub::Msg { msg, json } => {109+ Sub::Msg {
110- handle_msg(msg, json)?;110+ msg,
111+ json,
112+ print_request,
113+ } => {
114+ handle_msg(msg, json, print_request)?;
111 return Ok(());115 return Ok(());
112 }116 }
113 Sub::Panic => cause_panic(),117 Sub::Panic => cause_panic(),