fix scrolling and new row calculation

This commit is contained in:
Mitchell Hashimoto
2022-09-03 12:18:45 -07:00
parent d22a323896
commit 41f2b756ae
2 changed files with 55 additions and 55 deletions

View File

@ -422,7 +422,7 @@ pub fn deinit(self: *Screen) void {
/// Returns true if the viewport is scrolled to the bottom of the screen.
pub fn viewportIsBottom(self: Screen) bool {
return self.viewport >= self.history;
return self.viewport == self.history;
}
/// Shortcut for getRow followed by getCell as a quick way to read a cell.
@ -577,13 +577,18 @@ fn scrollDelta(self: *Screen, delta: isize, grow: bool) !void {
self.viewport += @intCast(usize, delta);
if (self.viewport <= self.history) return;
// Our viewport is bigger than our max. The number of new rows we need
// in our buffer is our value minus the max.
const new_rows_needed = self.viewport - self.history;
// If our viewport is past the top of our history then we potentially need
// to write more blank rows. If our viewport is more than our rows written
// then we expand out to there.
const rows_written = self.rowsWritten();
const viewport_bottom = self.viewport + self.rows;
if (viewport_bottom > rows_written) {
// The number of new rows we need is the number of rows off our
// previous bottom we are growing.
const new_rows_needed = viewport_bottom - rows_written;
// If we can't fit into our capacity but we have space, resize the
// buffer to allocate more scrollback.
const rows_written = self.rowsWritten();
const rows_final = rows_written + new_rows_needed;
if (rows_final > self.rowsCapacity()) {
const max_capacity = self.maxCapacity();
@ -625,6 +630,7 @@ fn scrollDelta(self: *Screen, delta: isize, grow: bool) !void {
(rows_written_final - 1) * (self.cols + 1),
self.cols + 1,
);
}
}
/// Returns the raw text associated with a selection. This will unwrap
@ -1202,19 +1208,13 @@ test "Screen: scrollback with large delta" {
}
// Scroll down a ton
try s.scroll(.{ .delta = 5 });
try s.scroll(.{ .delta_no_grow = 5 });
try testing.expect(s.viewportIsBottom());
{
// Test our contents rotated
var contents = try s.testString(alloc, .screen);
defer alloc.free(contents);
try testing.expectEqualStrings("3IJKL\n4ABCD\n5EFGH\n6IJKL", contents);
}
{
// Test our contents rotated
var contents = try s.testString(alloc, .viewport);
defer alloc.free(contents);
try testing.expectEqualStrings("6IJKL", contents);
try testing.expectEqualStrings("4ABCD\n5EFGH\n6IJKL", contents);
}
}

View File

@ -44,14 +44,14 @@ pub const Viewport = struct {
try s.scroll(.{ .delta = 6 });
try testing.expectEqual(ScreenPoint{
.x = 0,
.y = 6,
.y = 3,
}, (Viewport{ .x = 0, .y = 0 }).toScreen(&s));
// Move the viewport a bit up
try s.scroll(.{ .delta = -1 });
try testing.expectEqual(ScreenPoint{
.x = 0,
.y = 5,
.y = 2,
}, (Viewport{ .x = 0, .y = 0 }).toScreen(&s));
// Move the viewport to top