monitor: Add `clear` command
The monitor screen can be cluttered after executing commands like `info qtree`. It is useful to have a command to clear current screen, just like linux `clear` command do. This patch has been tested under monitors using stdio, vc, tcp socket, unix socket and serial interfaces. Signed-off-by: Alano Song <AlanoSong@163.com> Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org> Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org> Message-ID: <20260504174914.122607-5-dave@treblig.org> Signed-off-by: Markus Armbruster <armbru@redhat.com>
AlanoSong@163.com committed
May 4, 2026 at 18:49 UTC
ea017f67ce1ca34c7503b6f23f48b433585d37ac
3 files changed
+26
hmp-commands.hx
+14
@@ -20,6 +20,20 @@ SRST
20
Show the help for all commands or just for command *cmd*.
21
ERST
22
23
+ {
24
+ .name = "clear",
25
+ .args_type = "",
26
+ .params = "",
27
+ .help = "clear the monitor screen",
28
+ .cmd = hmp_clear,
29
+ .flags = "p",
30
+ },
31
+
32
+SRST
33
+``clear``
34
+ Clear the monitor screen.
35
+ERST
36
+
37
{
38
.name = "commit",
39
.args_type = "device:B",
include/monitor/hmp.h
+1
@@ -165,6 +165,7 @@ void hmp_trace_event(Monitor *mon, const QDict *qdict);
165
void hmp_trace_file(Monitor *mon, const QDict *qdict);
166
void hmp_info_trace_events(Monitor *mon, const QDict *qdict);
167
void hmp_help(Monitor *mon, const QDict *qdict);
168
+void hmp_clear(Monitor *mon, const QDict *qdict);
169
void hmp_info_help(Monitor *mon, const QDict *qdict);
170
void hmp_info_sync_profile(Monitor *mon, const QDict *qdict);
171
void hmp_info_history(Monitor *mon, const QDict *qdict);
monitor/hmp-cmds.c
+11
@@ -219,6 +219,17 @@ void hmp_help(Monitor *mon, const QDict *qdict)
219
hmp_help_cmd(mon, qdict_get_try_str(qdict, "name"));
220
}
221
222
+void hmp_clear(Monitor *mon, const QDict *qdict)
223
+{
224
+ /*
225
+ * Send an ANSI escape sequence:
226
+ * "\x1b[H" - move cursor to top-left
227
+ * "\x1b[2J" - clear visible screen
228
+ * "\x1b[3J" - clear scrollback
229
+ */
230
+ monitor_printf(mon, "\x1b[H\x1b[2J\x1b[3J");
231
+}
232
+
233
void hmp_info_help(Monitor *mon, const QDict *qdict)
234
{
235
hmp_help_cmd(mon, "info");