rowIterator(.screen) now ignores unused lines, fixes shrinking rows

This commit is contained in:
Mitchell Hashimoto
2022-08-08 10:32:42 -07:00
parent 4ef73efeac
commit 57f6986343

View File

@ -126,7 +126,10 @@ pub const RowIndexTag = enum {
/// The max value for the given tag. /// The max value for the given tag.
pub fn max(self: RowIndexTag, screen: *const Screen) usize { pub fn max(self: RowIndexTag, screen: *const Screen) usize {
return switch (self) { return switch (self) {
.screen => screen.totalRows(), // The max of the screen is "bottom" so that we don't read
// past the pre-allocated space.
.screen => screen.bottom,
.viewport => screen.rows, .viewport => screen.rows,
.active => screen.rows, .active => screen.rows,
} - 1; } - 1;
@ -265,7 +268,7 @@ pub fn getCell(self: Screen, row: usize, col: usize) *Cell {
fn rowIndex(self: Screen, idx: RowIndex) usize { fn rowIndex(self: Screen, idx: RowIndex) usize {
const y = switch (idx) { const y = switch (idx) {
.screen => |y| y: { .screen => |y| y: {
assert(y < self.totalRows()); assert(y < self.bottom);
break :y y; break :y y;
}, },
@ -573,8 +576,6 @@ pub fn resize2(self: *Screen, alloc: Allocator, rows: usize, cols: usize) !void
assert(@mod(top_len + bot_len, self.cols) == 0); assert(@mod(top_len + bot_len, self.cols) == 0);
const copied_rows = (top_len + bot_len) / self.cols; const copied_rows = (top_len + bot_len) / self.cols;
//log.warn("bot={} top={} copied={}", .{ bot_len, top_len, copied_rows });
// Modify our storage // Modify our storage
alloc.free(self.storage); alloc.free(self.storage);
self.storage = storage; self.storage = storage;
@ -588,7 +589,8 @@ pub fn resize2(self: *Screen, alloc: Allocator, rows: usize, cols: usize) !void
self.top = 0; self.top = 0;
self.bottom = @maximum(rows, copied_rows); self.bottom = @maximum(rows, copied_rows);
//self.bottom = @minimum(self.bottom, copied_rows); //self.bottom = @minimum(self.bottom, copied_rows);
log.warn("BOTTOM={}", .{self.bottom}); //log.warn("bot={} top={} copied={}", .{ bot_len, top_len, copied_rows });
//log.warn("BOTTOM={}", .{self.bottom});
self.scroll(.{ .bottom = {} }); self.scroll(.{ .bottom = {} });
} }
} }
@ -1427,52 +1429,56 @@ test "Screen: resize less rows no scrollback" {
} }
} }
// test "Screen: resize less rows with empty scrollback" { test "Screen: resize less rows with empty scrollback" {
// const testing = std.testing; const testing = std.testing;
// const alloc = testing.allocator; const alloc = testing.allocator;
//
// var s = try init(alloc, 3, 5, 10);
// defer s.deinit(alloc);
// const str = "1ABCD\n2EFGH\n3IJKL";
// s.testWriteString(str);
// try s.resize2(alloc, 1, 5);
//
// {
// var contents = try s.testString(alloc, .screen);
// defer alloc.free(contents);
// try testing.expectEqualStrings(str, contents);
// }
// {
// var contents = try s.testString(alloc, .viewport);
// defer alloc.free(contents);
// const expected = "3IJKL";
// try testing.expectEqualStrings(expected, contents);
// }
// }
// test "Screen: resize more rows with populated scrollback" { var s = try init(alloc, 3, 5, 10);
// const testing = std.testing; defer s.deinit(alloc);
// const alloc = testing.allocator; const str = "1ABCD\n2EFGH\n3IJKL";
// s.testWriteString(str);
// var s = try init(alloc, 3, 5, 5); try s.resize2(alloc, 1, 5);
// defer s.deinit(alloc);
// const str = "1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH"; {
// s.testWriteString(str); var contents = try s.testString(alloc, .screen);
// { defer alloc.free(contents);
// var contents = try s.testString(alloc, .viewport); try testing.expectEqualStrings(str, contents);
// defer alloc.free(contents); }
// const expected = "3IJKL\n4ABCD\n5EFGH"; {
// try testing.expectEqualStrings(expected, contents); var contents = try s.testString(alloc, .viewport);
// } defer alloc.free(contents);
// const expected = "3IJKL";
// // Resize try testing.expectEqualStrings(expected, contents);
// try s.resize2(alloc, 10, 5); }
// try testing.expectEqual(@as(usize, 15), s.totalRows()); }
//
// { test "Screen: resize less rows with populated scrollback" {
// var contents = try s.testString(alloc, .viewport); const testing = std.testing;
// defer alloc.free(contents); const alloc = testing.allocator;
// try testing.expectEqualStrings(str, contents);
// } var s = try init(alloc, 3, 5, 5);
// } defer s.deinit(alloc);
// const str = "1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH";
s.testWriteString(str);
{
var contents = try s.testString(alloc, .viewport);
defer alloc.free(contents);
const expected = "3IJKL\n4ABCD\n5EFGH";
try testing.expectEqualStrings(expected, contents);
}
// Resize
try s.resize2(alloc, 1, 5);
{
var contents = try s.testString(alloc, .screen);
defer alloc.free(contents);
try testing.expectEqualStrings(str, contents);
}
{
var contents = try s.testString(alloc, .viewport);
defer alloc.free(contents);
const expected = "5EFGH";
try testing.expectEqualStrings(expected, contents);
}
}