terminal: cursor back handles reverse wrap (mode 45)

This commit is contained in:
Mitchell Hashimoto
2023-10-06 08:35:02 -07:00
parent 7fe1a61ae8
commit 9e506ac7e1
3 changed files with 92 additions and 2 deletions

View File

@ -350,6 +350,10 @@ pub const Row = struct {
return self.storage[0].header.flags.dirty;
}
pub inline fn isWrapped(self: Row) bool {
return self.storage[0].header.flags.wrap;
}
/// Set the semantic prompt state for this row.
pub fn setSemanticPrompt(self: Row, p: RowHeader.SemanticPrompt) void {
self.storage[0].header.flags.semantic_prompt = p;

View File

@ -1358,6 +1358,15 @@ pub fn cursorLeft(self: *Terminal, count_req: usize) void {
const tracy = trace(@src());
defer tracy.end();
// Wrapping behavior depends on various terminal modes
const WrapMode = enum { none, reverse, reverse_extended };
const wrap_mode: WrapMode = wrap_mode: {
if (!self.modes.get(.wraparound)) break :wrap_mode .none;
if (self.modes.get(.reverse_wrap)) break :wrap_mode .reverse;
if (self.modes.get(.reverse_wrap_extended)) break :wrap_mode .reverse_extended;
break :wrap_mode .none;
};
// If we have a pending wrap state on, we reset it and reduce our count.
var count: usize = @max(count_req, 1);
if (self.screen.cursor.pending_wrap) {
@ -1365,8 +1374,42 @@ pub fn cursorLeft(self: *Terminal, count_req: usize) void {
count -= 1;
}
// TODO: scroll region
self.screen.cursor.x -|= count;
// If we are in no wrap mode, then we move the cursor left and exit
// since this is the fastest and most typical path.
if (wrap_mode == .none) {
self.screen.cursor.x -|= count;
return;
}
// The leftmost column we can move left to.
const left_margin = 0;
const right_margin = self.cols - 1;
const top = self.scrolling_region.top;
//const bottom = self.screen.scroll_region.bottom;
while (true) {
// We can move at most to the left margin.
const max = self.screen.cursor.x - left_margin;
// We want to move at most the number of columns we have left
// or our remaining count. Do the move.
const amount = @min(max, count);
count -= amount;
self.screen.cursor.x -= amount;
// If we have no more to move, then we're done.
if (count == 0) break;
// If we are at the top, then we are done.
if (self.screen.cursor.y == top) break;
// If our previous line is not wrapped then we are done.
const row = self.screen.getRow(.{ .active = self.screen.cursor.y - 1 });
if (!row.isWrapped()) break;
self.screen.cursor.y -= 1;
self.screen.cursor.x = right_margin;
count -= 1;
}
}
/// Move the cursor right amount columns. If amount is greater than the
@ -3353,3 +3396,45 @@ test "Terminal: cursorLeft unsets pending wrap state with longer jump" {
try testing.expectEqualStrings("ABXDE", str);
}
}
test "Terminal: cursorLeft reverse wrap" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);
t.modes.set(.wraparound, true);
t.modes.set(.reverse_wrap, true);
for ("ABCDE1") |c| try t.print(c);
t.cursorLeft(2);
try t.print('X');
try testing.expect(t.screen.cursor.pending_wrap);
{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABCDX\n1", str);
}
}
test "Terminal: cursorLeft reverse wrap with no soft wrap" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);
t.modes.set(.wraparound, true);
t.modes.set(.reverse_wrap, true);
for ("ABCDE") |c| try t.print(c);
t.carriageReturn();
try t.linefeed();
try t.print('1');
t.cursorLeft(2);
try t.print('X');
{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABCDE\nX", str);
}
}

View File

@ -170,6 +170,7 @@ const entries: []const ModeEntry = &.{
.{ .name = "mouse_format_sgr_pixels", .value = 1016 },
.{ .name = "alt_esc_prefix", .value = 1036, .default = true },
.{ .name = "alt_sends_escape", .value = 1039 },
.{ .name = "reverse_wrap_extended", .value = 1045 },
.{ .name = "alt_screen_save_cursor_clear_enter", .value = 1049 },
.{ .name = "bracketed_paste", .value = 2004 },
.{ .name = "synchronized_output", .value = 2026 },