From b1c0c6a3661f9f97f33ecec4293fd01eee7907f5 Mon Sep 17 00:00:00 2001 From: Zhiwei Li <65117011+Levix@users.noreply.github.com> Date: Sun, 18 Jan 2026 21:35:25 +0800 Subject: [PATCH] feat: enhance response output with network request details and cleared status (#117) --- cli/src/commands.rs | 36 ++++++++++++++++++++++++++++++------ cli/src/output.rs | 21 +++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/cli/src/commands.rs b/cli/src/commands.rs index 2053cb4..3595183 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -385,7 +385,11 @@ pub fn parse_command(args: &[String], flags: &Flags) -> Result Ok(json!({ "id": id, "action": "tab_list" })), Some("close") => { - Ok(json!({ "id": id, "action": "tab_close", "index": rest.get(1).and_then(|s| s.parse::().ok()) })) + let mut cmd = json!({ "id": id, "action": "tab_close" }); + if let Some(index) = rest.get(1).and_then(|s| s.parse::().ok()) { + cmd["index"] = json!(index); + } + Ok(cmd) } Some(n) if n.parse::().is_ok() => { Ok(json!({ "id": id, "action": "tab_switch", "index": n.parse::().unwrap() })) @@ -428,7 +432,11 @@ pub fn parse_command(args: &[String], flags: &Flags) -> Result { - Ok(json!({ "id": id, "action": "dialog", "response": "accept", "promptText": rest.get(1) })) + let mut cmd = json!({ "id": id, "action": "dialog", "response": "accept" }); + if let Some(prompt_text) = rest.get(1) { + cmd["promptText"] = json!(prompt_text); + } + Ok(cmd) } Some("dismiss") => Ok(json!({ "id": id, "action": "dialog", "response": "dismiss" })), Some(sub) => Err(ParseError::UnknownSubcommand { @@ -446,8 +454,14 @@ pub fn parse_command(args: &[String], flags: &Flags) -> Result { const VALID: &[&str] = &["start", "stop"]; match rest.get(0).map(|s| *s) { - Some("start") => Ok(json!({ "id": id, "action": "trace_start", "path": rest.get(1) })), - Some("stop") => Ok(json!({ "id": id, "action": "trace_stop", "path": rest.get(1) })), + Some("start") => Ok(json!({ "id": id, "action": "trace_start" })), + Some("stop") => { + let path = rest.get(1).ok_or_else(|| ParseError::MissingArguments { + context: "trace stop".to_string(), + usage: "trace stop ", + })?; + Ok(json!({ "id": id, "action": "trace_stop", "path": path })) + }, Some(sub) => Err(ParseError::UnknownSubcommand { subcommand: sub.to_string(), valid_options: VALID, @@ -934,12 +948,22 @@ fn parse_network(rest: &[&str], id: &str) -> Result { let body = body_idx.and_then(|i| rest.get(i + 1).map(|s| *s)); Ok(json!({ "id": id, "action": "route", "url": url, "abort": abort, "body": body })) } - Some("unroute") => Ok(json!({ "id": id, "action": "unroute", "url": rest.get(1) })), + Some("unroute") => { + let mut cmd = json!({ "id": id, "action": "unroute" }); + if let Some(url) = rest.get(1) { + cmd["url"] = json!(url); + } + Ok(cmd) + }, Some("requests") => { let clear = rest.iter().any(|&s| s == "--clear"); let filter_idx = rest.iter().position(|&s| s == "--filter"); let filter = filter_idx.and_then(|i| rest.get(i + 1).map(|s| *s)); - Ok(json!({ "id": id, "action": "requests", "clear": clear, "filter": filter })) + let mut cmd = json!({ "id": id, "action": "requests", "clear": clear }); + if let Some(f) = filter { + cmd["filter"] = json!(f); + } + Ok(cmd) } Some(sub) => Err(ParseError::UnknownSubcommand { subcommand: sub.to_string(), diff --git a/cli/src/output.rs b/cli/src/output.rs index c6fca30..0889d8d 100644 --- a/cli/src/output.rs +++ b/cli/src/output.rs @@ -118,6 +118,27 @@ pub fn print_response(resp: &Response, json_mode: bool) { } return; } + // Network requests + if let Some(requests) = data.get("requests").and_then(|v| v.as_array()) { + if requests.is_empty() { + println!("No requests captured"); + } else { + for req in requests { + let method = req.get("method").and_then(|v| v.as_str()).unwrap_or("GET"); + let url = req.get("url").and_then(|v| v.as_str()).unwrap_or(""); + let resource_type = req.get("resourceType").and_then(|v| v.as_str()).unwrap_or(""); + println!("{} {} ({})", method, url, resource_type); + } + } + return; + } + // Cleared requests + if let Some(cleared) = data.get("cleared").and_then(|v| v.as_bool()) { + if cleared { + println!("\x1b[32m✓\x1b[0m Request log cleared"); + return; + } + } // Bounding box if let Some(box_data) = data.get("box") { println!(