terminal: C0.BS

This commit is contained in:
Mitchell Hashimoto
2022-04-18 12:35:15 -07:00
parent fc8bd859db
commit 753a946fd5
2 changed files with 27 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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