From 8c6fc35450e2251cdfcd9bf656ece47d377c6059 Mon Sep 17 00:00:00 2001 From: xuyongliang <478439790@qq.com> Date: Wed, 25 Mar 2026 23:27:28 +0800 Subject: [PATCH] fix: handle --clear flag in console command (#1015) The console and errors commands parsed --clear from CLI args but the action handlers silently ignored the flag. The handlers did not accept the cmd parameter so they had no way to read the clear field. Changes: - Add clear_console() method to EventTracker in network.rs - Update handle_console to accept cmd, read the clear field, and clear the buffer when --clear is passed (returns {cleared: true}) - Update call site in execute_command to pass cmd Co-authored-by: xuyongliang --- cli/src/native/actions.rs | 13 ++++++++++--- cli/src/native/network.rs | 4 ++++ cli/src/output.rs | 3 ++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cli/src/native/actions.rs b/cli/src/native/actions.rs index 4dc0efa..44543ff 100644 --- a/cli/src/native/actions.rs +++ b/cli/src/native/actions.rs @@ -1076,7 +1076,7 @@ pub async fn execute_command(cmd: &Value, state: &mut DaemonState) -> Value { "setcontent" => handle_setcontent(cmd, state).await, "headers" => handle_headers(cmd, state).await, "offline" => handle_offline(cmd, state).await, - "console" => handle_console(state).await, + "console" => handle_console(cmd, state).await, "errors" => handle_errors(state).await, "state_save" => handle_state_save(cmd, state).await, "state_load" => handle_state_load(cmd, state).await, @@ -2887,8 +2887,15 @@ async fn handle_offline(cmd: &Value, state: &DaemonState) -> Result Result { - Ok(state.event_tracker.get_console_json()) +async fn handle_console(cmd: &Value, state: &mut DaemonState) -> Result { + let clear = cmd.get("clear").and_then(|v| v.as_bool()).unwrap_or(false); + if clear { + state.event_tracker.clear_console(); + Ok(json!({ "cleared": true })) + } else { + let result = state.event_tracker.get_console_json(); + Ok(result) + } } async fn handle_errors(state: &DaemonState) -> Result { diff --git a/cli/src/native/network.rs b/cli/src/native/network.rs index 3d5bad4..4994a74 100644 --- a/cli/src/native/network.rs +++ b/cli/src/native/network.rs @@ -322,6 +322,10 @@ impl EventTracker { }); } + pub fn clear_console(&mut self) { + self.console_entries.clear(); + } + pub fn get_console_json(&self) -> Value { let messages: Vec = self .console_entries diff --git a/cli/src/output.rs b/cli/src/output.rs index 5d70ff2..5c275e6 100644 --- a/cli/src/output.rs +++ b/cli/src/output.rs @@ -430,11 +430,12 @@ pub fn print_response_with_opts(resp: &Response, action: Option<&str>, opts: &Ou } return; } - // Cleared (cookies or request log) + // Cleared (cookies, console, or request log) if let Some(cleared) = data.get("cleared").and_then(|v| v.as_bool()) { if cleared { let label = match action { Some("cookies_clear") => "Cookies cleared", + Some("console") => "Console log cleared", _ => "Request log cleared", }; println!("{} {}", color::success_indicator(), label);