From b053be0164c3ddd4dcde453fbc6452f44fdc33c2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 24 Feb 2024 21:50:52 -0800 Subject: [PATCH] terminal/new: scrolling viewport --- src/terminal/Terminal.zig | 1 + src/terminal/new/Screen.zig | 19 ++++++++++++++++++ src/terminal/new/Terminal.zig | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 2e966ce03..7c129f1ed 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -2747,6 +2747,7 @@ test "Terminal: disabled wraparound with wide grapheme and half space" { } } +// X test "Terminal: print writes to bottom if scrolled" { var t = try init(testing.allocator, 5, 2); defer t.deinit(testing.allocator); diff --git a/src/terminal/new/Screen.zig b/src/terminal/new/Screen.zig index 43379a57f..e9fe40b14 100644 --- a/src/terminal/new/Screen.zig +++ b/src/terminal/new/Screen.zig @@ -214,6 +214,25 @@ pub fn cursorDownScroll(self: *Screen) !void { self.cursor.page_cell = page_rac.cell; } +/// Options for scrolling the viewport of the terminal grid. The reason +/// we have this in addition to PageList.Scroll is because we have additional +/// scroll behaviors that are not part of the PageList.Scroll enum. +pub const Scroll = union(enum) { + /// For all of these, see PageList.Scroll. + active, + top, + delta_row: isize, +}; + +/// Scroll the viewport of the terminal grid. +pub fn scroll(self: *Screen, behavior: Scroll) void { + switch (behavior) { + .active => self.pages.scroll(.{ .active = {} }), + .top => self.pages.scroll(.{ .top = {} }), + .delta_row => |v| self.pages.scroll(.{ .delta_row = v }), + } +} + /// Dump the screen to a string. The writer given should be buffered; /// this function does not attempt to efficiently write and generally writes /// one byte at a time. diff --git a/src/terminal/new/Terminal.zig b/src/terminal/new/Terminal.zig index ad423921f..b19df6a68 100644 --- a/src/terminal/new/Terminal.zig +++ b/src/terminal/new/Terminal.zig @@ -1394,6 +1394,43 @@ test "Terminal: overwrite grapheme should clear grapheme data" { } } +test "Terminal: print writes to bottom if scrolled" { + var t = try init(testing.allocator, 5, 2); + defer t.deinit(testing.allocator); + + // Basic grid writing + for ("hello") |c| try t.print(c); + t.setCursorPos(0, 0); + + // Make newlines so we create scrollback + // 3 pushes hello off the screen + try t.index(); + try t.index(); + try t.index(); + { + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("", str); + } + + // Scroll to the top + t.screen.scroll(.{ .top = {} }); + { + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("hello", str); + } + + // Type + try t.print('A'); + t.screen.scroll(.{ .active = {} }); + { + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("\nA", str); + } +} + test "Terminal: soft wrap" { var t = try init(testing.allocator, 3, 80); defer t.deinit(testing.allocator);