diff --git a/include/ghostty/vt/terminal.h b/include/ghostty/vt/terminal.h index 233389498..6ecb6e62c 100644 --- a/include/ghostty/vt/terminal.h +++ b/include/ghostty/vt/terminal.h @@ -118,6 +118,19 @@ GhosttyResult ghostty_terminal_new(const GhosttyAllocator* allocator, */ void ghostty_terminal_free(GhosttyTerminal terminal); +/** + * Perform a full reset of the terminal (RIS). + * + * Resets all terminal state back to its initial configuration, including + * modes, scrollback, scrolling region, and screen contents. The terminal + * dimensions are preserved. + * + * @param terminal The terminal handle (may be NULL, in which case this is a no-op) + * + * @ingroup terminal + */ +void ghostty_terminal_reset(GhosttyTerminal terminal); + /** * Resize the terminal to the given dimensions. * diff --git a/src/lib_vt.zig b/src/lib_vt.zig index a76ed8446..1eddbd886 100644 --- a/src/lib_vt.zig +++ b/src/lib_vt.zig @@ -145,6 +145,7 @@ comptime { @export(&c.sgr_attribute_value, .{ .name = "ghostty_sgr_attribute_value" }); @export(&c.terminal_new, .{ .name = "ghostty_terminal_new" }); @export(&c.terminal_free, .{ .name = "ghostty_terminal_free" }); + @export(&c.terminal_reset, .{ .name = "ghostty_terminal_reset" }); @export(&c.terminal_resize, .{ .name = "ghostty_terminal_resize" }); @export(&c.terminal_vt_write, .{ .name = "ghostty_terminal_vt_write" }); @export(&c.terminal_scroll_viewport, .{ .name = "ghostty_terminal_scroll_viewport" }); diff --git a/src/terminal/c/main.zig b/src/terminal/c/main.zig index f17b9065e..31e1b40eb 100644 --- a/src/terminal/c/main.zig +++ b/src/terminal/c/main.zig @@ -55,6 +55,7 @@ pub const paste_is_safe = paste.is_safe; pub const terminal_new = terminal.new; pub const terminal_free = terminal.free; +pub const terminal_reset = terminal.reset; pub const terminal_resize = terminal.resize; pub const terminal_vt_write = terminal.vt_write; pub const terminal_scroll_viewport = terminal.scroll_viewport; diff --git a/src/terminal/c/terminal.zig b/src/terminal/c/terminal.zig index ef64e7c0e..0af791f91 100644 --- a/src/terminal/c/terminal.zig +++ b/src/terminal/c/terminal.zig @@ -93,6 +93,11 @@ pub fn resize( return .success; } +pub fn reset(terminal_: Terminal) callconv(.c) void { + const t = terminal_ orelse return; + t.fullReset(); +} + pub fn free(terminal_: Terminal) callconv(.c) void { const t = terminal_ orelse return; @@ -203,6 +208,31 @@ test "scroll_viewport null" { scroll_viewport(null, .{ .tag = .top, .value = undefined }); } +test "reset" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib_alloc.test_allocator, + &t, + .{ + .cols = 80, + .rows = 24, + .max_scrollback = 10_000, + }, + )); + defer free(t); + + vt_write(t, "Hello", 5); + reset(t); + + const str = try t.?.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("", str); +} + +test "reset null" { + reset(null); +} + test "resize" { var t: Terminal = null; try testing.expectEqual(Result.success, new(