horizontal tab off by one, add unit tests

This commit is contained in:
Mitchell Hashimoto
2022-04-27 21:06:28 -07:00
parent 53702343c2
commit f44649476d

View File

@ -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. /// This may allocate if necessary to store the character in the grid.
pub fn appendChar(self: *Terminal, alloc: Allocator, c: u8) !void { pub fn appendChar(self: *Terminal, alloc: Allocator, c: u8) !void {
//log.debug("char: {}", .{c}); log.debug("char: {}", .{c});
const actions = self.parser.next(c); const actions = self.parser.next(c);
for (actions) |action_opt| { for (actions) |action_opt| {
switch (action_opt orelse continue) { switch (action_opt orelse continue) {
@ -163,8 +163,10 @@ pub fn horizontal_tab(self: *Terminal, alloc: Allocator) !void {
// Clear // Clear
try self.print(alloc, ' '); try self.print(alloc, ' ');
// If this is the tabstop, then we're done. // If the last cursor position was a tabstop we return. We do
if (self.tabstops.get(self.cursor.x)) return; // "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); 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);
}