Skip to content

Commit

Permalink
terminal: implement origin mode and left margin handling for CR
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 6, 2023
1 parent e3b4554 commit 3360a88
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/terminal/Terminal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1534,11 +1534,16 @@ pub fn carriageReturn(self: *Terminal) void {
const tracy = trace(@src());
defer tracy.end();

// TODO: left/right margin mode
// TODO: origin mode

self.screen.cursor.x = 0;
// Always reset pending wrap state
self.screen.cursor.pending_wrap = false;

// In origin mode we always move to the left margin
self.screen.cursor.x = if (self.modes.get(.origin))
self.scrolling_region.left
else if (self.screen.cursor.x >= self.scrolling_region.left)
self.scrolling_region.left
else
0;
}

/// Linefeed moves the cursor to the next line.
Expand Down Expand Up @@ -2207,6 +2212,37 @@ test "Terminal: carriage return unsets pending wrap" {
try testing.expect(t.screen.cursor.pending_wrap == false);
}

test "Terminal: carriage return origin mode moves to left margin" {
var t = try init(testing.allocator, 5, 80);
defer t.deinit(testing.allocator);

t.modes.set(.origin, true);
t.screen.cursor.x = 0;
t.scrolling_region.left = 2;
t.carriageReturn();
try testing.expectEqual(@as(usize, 2), t.screen.cursor.x);
}

test "Terminal: carriage return left of left margin moves to zero" {
var t = try init(testing.allocator, 5, 80);
defer t.deinit(testing.allocator);

t.screen.cursor.x = 1;
t.scrolling_region.left = 2;
t.carriageReturn();
try testing.expectEqual(@as(usize, 0), t.screen.cursor.x);
}

test "Terminal: carriage return right of left margin moves to left margin" {
var t = try init(testing.allocator, 5, 80);
defer t.deinit(testing.allocator);

t.screen.cursor.x = 3;
t.scrolling_region.left = 2;
t.carriageReturn();
try testing.expectEqual(@as(usize, 2), t.screen.cursor.x);
}

test "Terminal: backspace" {
var t = try init(testing.allocator, 80, 80);
defer t.deinit(testing.allocator);
Expand Down

0 comments on commit 3360a88

Please sign in to comment.