From cbded6a95af970ce5159ea4850ce6388328c51c2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 25 Jun 2023 09:49:18 -0700 Subject: [PATCH] terminal: horizontalTabBack function --- src/terminal/Terminal.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 0f0fef511..bcc909714 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1218,6 +1218,21 @@ pub fn horizontalTab(self: *Terminal) !void { } } +// Same as horizontalTab but moves to the previous tabstop instead of the next. +pub fn horizontalTabBack(self: *Terminal) !void { + const tracy = trace(@src()); + defer tracy.end(); + + while (true) { + // If we're already at the edge of the screen, then we're done. + if (self.screen.cursor.x == 0) return; + + // Move the cursor left + self.screen.cursor.x -= 1; + if (self.tabstops.get(self.screen.cursor.x)) return; + } +} + /// Clear tab stops. /// TODO: test pub fn tabClear(self: *Terminal, cmd: csi.TabClear) void { @@ -1798,6 +1813,29 @@ test "Terminal: horizontal tabs" { try testing.expectEqual(@as(usize, 19), t.screen.cursor.x); } +test "Terminal: horizontal tabs back" { + const alloc = testing.allocator; + var t = try init(alloc, 20, 5); + defer t.deinit(alloc); + + // Edge of screen + t.screen.cursor.x = 19; + + // HT + try t.horizontalTabBack(); + try testing.expectEqual(@as(usize, 15), t.screen.cursor.x); + + // HT + try t.horizontalTabBack(); + try testing.expectEqual(@as(usize, 7), t.screen.cursor.x); + + // HT + try t.horizontalTabBack(); + try testing.expectEqual(@as(usize, 0), t.screen.cursor.x); + try t.horizontalTabBack(); + try testing.expectEqual(@as(usize, 0), t.screen.cursor.x); +} + test "Terminal: setCursorPosition" { var t = try init(testing.allocator, 80, 80); defer t.deinit(testing.allocator);