From 753a946fd5ec594e1b68fa714d27fb1d7961358c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 18 Apr 2022 12:35:15 -0700 Subject: [PATCH] terminal: C0.BS --- src/terminal/Terminal.zig | 27 +++++++++++++++++++++++++-- src/terminal/ansi.zig | 2 ++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index ef2cbcd51..3ed3ae695 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -115,11 +115,17 @@ fn print(self: *Terminal, alloc: Allocator, c: u8) !void { fn execute(self: *Terminal, c: u8) !void { switch (@intToEnum(ansi.C0, c)) { + .BS => self.backspace(), .LF => self.linefeed(), .CR => self.carriage_return(), } } +/// Backspace moves the cursor back a column (but not less than 0). +pub fn backspace(self: *Terminal) void { + self.cursor.x -|= 1; +} + /// Carriage return moves the cursor to the first column. pub fn carriage_return(self: *Terminal) void { self.cursor.x = 0; @@ -151,7 +157,7 @@ test { _ = Parser; } -test "Terminal: simple input" { +test "Terminal: input with no control characters" { var t = init(80, 80); defer t.deinit(testing.allocator); @@ -166,7 +172,7 @@ test "Terminal: simple input" { } } -test "Terminal: multiline input" { +test "Terminal: C0 control LF and CR" { var t = init(80, 80); defer t.deinit(testing.allocator); @@ -180,3 +186,20 @@ test "Terminal: multiline input" { try testing.expectEqualStrings("hello\nworld", str); } } + +test "Terminal: C0 control BS" { + var t = init(80, 80); + defer t.deinit(testing.allocator); + + // BS + try t.append(testing.allocator, "hello"); + try t.appendChar(testing.allocator, @enumToInt(ansi.C0.BS)); + try t.append(testing.allocator, "y"); + try testing.expectEqual(@as(usize, 0), t.cursor.y); + try testing.expectEqual(@as(usize, 5), t.cursor.x); + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("helly", str); + } +} diff --git a/src/terminal/ansi.zig b/src/terminal/ansi.zig index 35735562b..a4310b82d 100644 --- a/src/terminal/ansi.zig +++ b/src/terminal/ansi.zig @@ -3,6 +3,8 @@ /// This is not complete, control characters are only added to this /// as the terminal emulator handles them. pub const C0 = enum(u7) { + /// Backspace + BS = 0x08, /// Line feed LF = 0x0A, /// Carriage return