terminal/new: scrolling viewport

This commit is contained in:
Mitchell Hashimoto
2024-02-24 21:50:52 -08:00
parent e8d548e8d0
commit b053be0164
3 changed files with 57 additions and 0 deletions

View File

@ -2747,6 +2747,7 @@ test "Terminal: disabled wraparound with wide grapheme and half space" {
} }
} }
// X
test "Terminal: print writes to bottom if scrolled" { test "Terminal: print writes to bottom if scrolled" {
var t = try init(testing.allocator, 5, 2); var t = try init(testing.allocator, 5, 2);
defer t.deinit(testing.allocator); defer t.deinit(testing.allocator);

View File

@ -214,6 +214,25 @@ pub fn cursorDownScroll(self: *Screen) !void {
self.cursor.page_cell = page_rac.cell; 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; /// Dump the screen to a string. The writer given should be buffered;
/// this function does not attempt to efficiently write and generally writes /// this function does not attempt to efficiently write and generally writes
/// one byte at a time. /// one byte at a time.

View File

@ -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" { test "Terminal: soft wrap" {
var t = try init(testing.allocator, 3, 80); var t = try init(testing.allocator, 3, 80);
defer t.deinit(testing.allocator); defer t.deinit(testing.allocator);