From f44649476d1dfec45bac1f704fe77c93f0ac89fd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 27 Apr 2022 21:06:28 -0700 Subject: [PATCH] horizontal tab off by one, add unit tests --- src/terminal/Terminal.zig | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index dfb78adac..e0525cd73 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -114,7 +114,7 @@ pub fn append(self: *Terminal, alloc: Allocator, str: []const u8) !void { /// /// This may allocate if necessary to store the character in the grid. pub fn appendChar(self: *Terminal, alloc: Allocator, c: u8) !void { - //log.debug("char: {}", .{c}); + log.debug("char: {}", .{c}); const actions = self.parser.next(c); for (actions) |action_opt| { switch (action_opt orelse continue) { @@ -163,8 +163,10 @@ pub fn horizontal_tab(self: *Terminal, alloc: Allocator) !void { // Clear try self.print(alloc, ' '); - // If this is the tabstop, then we're done. - if (self.tabstops.get(self.cursor.x)) return; + // If the last cursor position was a tabstop we return. We do + // "last cursor position" because we want a space to be written + // at the tabstop unless we're at the end (the while condition). + if (self.tabstops.get(self.cursor.x - 1)) return; } } @@ -246,3 +248,16 @@ test "Terminal: C0 control BS" { try testing.expectEqualStrings("helly", str); } } + +test "Terminal: horizontal tabs" { + var t = try init(testing.allocator, 80, 5); + defer t.deinit(testing.allocator); + + // HT + try t.append(testing.allocator, "1\t"); + try testing.expectEqual(@as(usize, 8), t.cursor.x); + + // HT + try t.append(testing.allocator, "\t"); + try testing.expectEqual(@as(usize, 16), t.cursor.x); +}