screen: row copy

This commit is contained in:
Mitchell Hashimoto
2022-05-21 16:04:18 -07:00
parent 1a31f8c8be
commit 2a657d1ec9
2 changed files with 27 additions and 4 deletions

View File

@ -115,8 +115,15 @@ pub fn scroll(self: *Screen, count: isize) void {
} }
} }
/// Copy row at src to dst.
pub fn copyRow(self: *Screen, dst: usize, src: usize) void {
const src_row = self.getRow(src);
const dst_row = self.getRow(dst);
std.mem.copy(Cell, dst_row, src_row);
}
/// Turns the screen into a string. /// Turns the screen into a string.
fn testString(self: Screen, alloc: Allocator) ![]const u8 { pub fn testString(self: Screen, alloc: Allocator) ![]const u8 {
const buf = try alloc.alloc(u8, self.storage.len + self.rows); const buf = try alloc.alloc(u8, self.storage.len + self.rows);
var i: usize = 0; var i: usize = 0;
var y: usize = 0; var y: usize = 0;
@ -206,3 +213,21 @@ test "Screen: scrolling" {
defer alloc.free(contents); defer alloc.free(contents);
try testing.expectEqualStrings("2EFGH\n3IJKL\n1ABCD", contents); try testing.expectEqualStrings("2EFGH\n3IJKL\n1ABCD", contents);
} }
test "Screen: row copy" {
const testing = std.testing;
const alloc = testing.allocator;
var s = try init(alloc, 3, 5);
defer s.deinit(alloc);
s.testWriteString("1ABCD\n2EFGH\n3IJKL");
// Copy
s.scroll(1);
s.copyRow(2, 0);
// Test our contents
var contents = try s.testString(alloc);
defer alloc.free(contents);
try testing.expectEqualStrings("2EFGH\n3IJKL\n2EFGH", contents);
}

View File

@ -28,8 +28,6 @@ test {
_ = @import("osc.zig"); _ = @import("osc.zig");
_ = @import("parse_table.zig"); _ = @import("parse_table.zig");
_ = @import("Tabstops.zig");
// TODO
_ = @import("Screen.zig"); _ = @import("Screen.zig");
_ = @import("Tabstops.zig");
} }