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 {
4343 /// Format output as JSON.
4444 #[arg(short, long)]
4545 json: bool,
46+ /// Print the IPC request as JSON instead of sending it.
47+ #[arg(long)]
48+ print_request: bool,
4649 },
4750 /// Validate the config file.
4851 Validate {
@@ -109,6 +112,8 @@ pub enum Msg {
109112 OverviewState,
110113 /// List screencasts.
111114 Casts,
115+ /// Send a raw JSON request to the compositor, reading from stdin.
116+ RawRequest,
112117 }
113118
114119 #[derive(Clone, Debug, clap::ValueEnum)]
src/ipc/client.rsmodified+15 -1
@@ -15,7 +15,7 @@ use serde_json::json;
1515 use crate::cli::Msg;
1616 use crate::utils::version;
1717
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<()> {
1919 // For actions taking paths, prepend the niri CLI's working directory.
2020 if let Msg::Action {
2121 action:
@@ -49,8 +49,18 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
4949 Msg::RequestError => Request::ReturnError,
5050 Msg::OverviewState => Request::OverviewState,
5151 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+ }
5256 };
5357
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+ }
5464 let mut socket = Socket::connect().context("error connecting to the niri socket")?;
5565
5666 let result = socket.send(request);
@@ -550,6 +560,10 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
550560 println!();
551561 }
552562 }
563+ Msg::RawRequest => {
564+ let output = serde_json::to_string(&response).context("error formatting response")?;
565+ println!("{output}");
566+ }
553567 }
554568
555569 Ok(())
src/main.rsmodified+6 -2
@@ -106,8 +106,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
106106 info!("config is valid");
107107 return Ok(());
108108 }
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)?;
111115 return Ok(());
112116 }
113117 Sub::Panic => cause_panic(),