From 89d07fcd83379a0c5c93391e2402982110438bb1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 3 Mar 2023 09:27:47 -0800 Subject: [PATCH] clear_history binding, default Cmd+K --- src/Surface.zig | 7 +++++++ src/config.zig | 6 ++++++ src/input/Binding.zig | 3 +++ src/termio/Exec.zig | 13 +++++++++++++ src/termio/Thread.zig | 1 + src/termio/message.zig | 6 ++++++ 6 files changed, 36 insertions(+) diff --git a/src/Surface.zig b/src/Surface.zig index 10d614365..202f398d2 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -896,6 +896,13 @@ pub fn keyCallback( self.setFontSize(size); }, + .clear_screen => { + _ = self.io_thread.mailbox.push(.{ + .clear_screen = .{ .history = true }, + }, .{ .forever = {} }); + try self.io_thread.wakeup.notify(); + }, + .toggle_dev_mode => if (DevMode.enabled) { DevMode.instance.visible = !DevMode.instance.visible; try self.queueRender(); diff --git a/src/config.zig b/src/config.zig index 97987020d..06adb6910 100644 --- a/src/config.zig +++ b/src/config.zig @@ -192,6 +192,12 @@ pub const Config = struct { .{ .paste_from_clipboard = {} }, ); + try result.keybind.set.put( + alloc, + .{ .key = .k, .mods = .{ .super = true } }, + .{ .clear_screen = {} }, + ); + try result.keybind.set.put(alloc, .{ .key = .up }, .{ .cursor_key = .{ .normal = "\x1b[A", .application = "\x1bOA", diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 394e62c8c..a788d0c5e 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -146,6 +146,9 @@ pub const Action = union(enum) { /// Reset the font size to the original configured size reset_font_size: void, + /// Clear the screen. This also clears all scrollback. + clear_screen: void, + /// Dev mode toggle_dev_mode: void, diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index d8d4d9cee..642dbef0a 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -194,6 +194,19 @@ pub fn resize( } } +/// Clear the screen. +pub fn clearScreen(self: *Exec, history: bool) !void { + // Queue form-feed ASCII code to clear the visible page. + try self.queueWrite(&[_]u8{0x0C}); + + // Clear our scrollback + if (history) { + self.renderer_state.mutex.lock(); + defer self.renderer_state.mutex.unlock(); + self.terminal.screen.clearHistory(); + } +} + pub inline fn queueWrite(self: *Exec, data: []const u8) !void { const ev = self.data.?; diff --git a/src/termio/Thread.zig b/src/termio/Thread.zig index aa2a9c568..49ae661cc 100644 --- a/src/termio/Thread.zig +++ b/src/termio/Thread.zig @@ -130,6 +130,7 @@ fn drainMailbox(self: *Thread) !void { log.debug("mailbox message={}", .{message}); switch (message) { .resize => |v| try self.impl.resize(v.grid_size, v.screen_size, v.padding), + .clear_screen => |v| try self.impl.clearScreen(v.history), .write_small => |v| try self.impl.queueWrite(v.data[0..v.len]), .write_stable => |v| try self.impl.queueWrite(v), .write_alloc => |v| { diff --git a/src/termio/message.zig b/src/termio/message.zig index 008093da0..a9f9a9006 100644 --- a/src/termio/message.zig +++ b/src/termio/message.zig @@ -29,6 +29,12 @@ pub const Message = union(enum) { padding: renderer.Padding, }, + /// Clear the screen. + clear_screen: struct { + /// Include clearing the history + history: bool, + }, + /// Write where the data fits in the union. write_small: WriteReq.Small,