From b47f2e6eef3b2a97a4e5aaa6e1050a489f3bea72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 21 May 2022 20:39:20 -0700 Subject: [PATCH] fixing more bugs --- conformance/csi_decstbm.zig | 15 +++++++++++ src/Window.zig | 2 +- src/terminal/Terminal.zig | 52 +++++++++++++++++++++++++++++-------- src/terminal/stream.zig | 6 ++++- 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 conformance/csi_decstbm.zig diff --git a/conformance/csi_decstbm.zig b/conformance/csi_decstbm.zig new file mode 100644 index 000000000..f8b652427 --- /dev/null +++ b/conformance/csi_decstbm.zig @@ -0,0 +1,15 @@ +//! Set Top and Bottom Margins (DECSTBM) - ESC [ r +const std = @import("std"); + +pub fn main() !void { + const stdout = std.io.getStdOut().writer(); + try stdout.print("A\nB\nC\nD", .{}); + try stdout.print("\x1B[1;3r", .{}); // cursor up + try stdout.print("\x1B[1;1H", .{}); // top-left + try stdout.print("\x1B[M", .{}); // delete line + try stdout.print("E\n", .{}); + try stdout.print("\x1B[7;1H", .{}); // cursor up + + // const stdin = std.io.getStdIn().reader(); + // _ = try stdin.readByte(); +} diff --git a/src/Window.zig b/src/Window.zig index 8d85a358c..950143fb9 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -664,7 +664,7 @@ pub fn insertLines(self: *Window, count: usize) !void { } pub fn deleteLines(self: *Window, count: usize) !void { - self.terminal.deleteLines(self.alloc, count); + self.terminal.deleteLines(count); } pub fn reverseIndex(self: *Window) !void { diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 4823dbde4..4dd352186 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -488,7 +488,7 @@ pub fn insertLines(self: *Terminal, alloc: Allocator, count: usize) !void { /// cleared space is colored according to the current SGR state. /// /// Moves the cursor to the left margin. -pub fn deleteLines(self: *Terminal, alloc: Allocator, count: usize) void { +pub fn deleteLines(self: *Terminal, count: usize) void { // TODO: scroll region bounds // Move the cursor to the left margin @@ -504,7 +504,6 @@ pub fn deleteLines(self: *Terminal, alloc: Allocator, count: usize) void { var i: usize = 0; while (i < count2) : (i += 1) { self.scrollUpRegion( - alloc, self.cursor.y, self.scrolling_region.bottom, ); @@ -526,15 +525,12 @@ pub fn scrollUp(self: *Terminal) void { /// Top and bottom are 0-indexed. fn scrollUpRegion( self: *Terminal, - alloc: Allocator, top: usize, bottom: usize, ) void { const tracy = trace(@src()); defer tracy.end(); - _ = alloc; - // Only go to the end of the region OR the end of our lines. const end = @minimum(bottom, self.screen.rows - 1); @@ -544,10 +540,8 @@ fn scrollUpRegion( } // Blank our last line if we have space. - if (i < self.screen.rows) { - const row = self.screen.getRow(i); - for (row) |*cell| cell.char = 0; - } + const row = self.screen.getRow(i); + for (row) |*cell| cell.char = 0; } /// Scroll the text down by one row. @@ -706,7 +700,7 @@ test "Terminal: setScrollingRegion" { try testing.expectEqual(@as(usize, t.rows - 1), t.scrolling_region.bottom); } -test "Terminal: setScrollingRegion" { +test "Terminal: deleteLines" { const alloc = testing.allocator; var t = try init(alloc, 80, 80); defer t.deinit(alloc); @@ -724,7 +718,7 @@ test "Terminal: setScrollingRegion" { try t.print('D'); t.cursorUp(2); - t.deleteLines(alloc, 1); + t.deleteLines(1); try t.print('E'); t.carriageReturn(); @@ -741,6 +735,42 @@ test "Terminal: setScrollingRegion" { } } +test "Terminal: deleteLines with scroll region" { + const alloc = testing.allocator; + var t = try init(alloc, 80, 80); + defer t.deinit(alloc); + + // Initial value + try t.print('A'); + t.carriageReturn(); + t.linefeed(); + try t.print('B'); + t.carriageReturn(); + t.linefeed(); + try t.print('C'); + t.carriageReturn(); + t.linefeed(); + try t.print('D'); + + t.setScrollingRegion(1, 3); + t.setCursorPos(1, 1); + t.deleteLines(1); + + try t.print('E'); + t.carriageReturn(); + t.linefeed(); + + // We should be + // try testing.expectEqual(@as(usize, 0), t.cursor.x); + // try testing.expectEqual(@as(usize, 2), t.cursor.y); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("E\nC\n\nD", str); + } +} + test "Terminal: insertLines" { const alloc = testing.allocator; var t = try init(alloc, 2, 5); diff --git a/src/terminal/stream.zig b/src/terminal/stream.zig index 3e6dafcfe..581706f9f 100644 --- a/src/terminal/stream.zig +++ b/src/terminal/stream.zig @@ -48,7 +48,11 @@ pub fn Stream(comptime Handler: type) type { //log.debug("char: {x}", .{c}); const actions = self.parser.next(c); for (actions) |action_opt| { - if (action_opt) |action| log.info("action: {}", .{action}); + if (action_opt) |action| { + if (action != .print) { + log.info("action: {}", .{action}); + } + } switch (action_opt orelse continue) { .print => |p| if (@hasDecl(T, "print")) try self.handler.print(p), .execute => |code| try self.execute(code),