terminal/new: cursorRight

This commit is contained in:
Mitchell Hashimoto
2024-02-25 22:17:06 -08:00
parent a41239fddc
commit d30f50d5f0
2 changed files with 87 additions and 0 deletions

View File

@ -6936,6 +6936,7 @@ test "Terminal: cursorUp resets wrap" {
}
}
// X
test "Terminal: cursorRight resets wrap" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
@ -6954,6 +6955,7 @@ test "Terminal: cursorRight resets wrap" {
}
}
// X
test "Terminal: cursorRight to the edge of screen" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
@ -6969,6 +6971,7 @@ test "Terminal: cursorRight to the edge of screen" {
}
}
// X
test "Terminal: cursorRight left of right margin" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
@ -6985,6 +6988,7 @@ test "Terminal: cursorRight left of right margin" {
}
}
// X
test "Terminal: cursorRight right of right margin" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);

View File

@ -619,6 +619,23 @@ pub fn cursorDown(self: *Terminal, count_req: usize) void {
self.screen.cursorDown(@intCast(count));
}
/// Move the cursor right amount columns. If amount is greater than the
/// maximum move distance then it is internally adjusted to the maximum.
/// This sequence will not scroll the screen or scroll region. If amount is
/// 0, adjust it to 1.
pub fn cursorRight(self: *Terminal, count_req: usize) void {
// Always resets pending wrap
self.screen.cursor.pending_wrap = false;
// The max the cursor can move to depends where the cursor currently is
const max = if (self.screen.cursor.x <= self.scrolling_region.right)
self.scrolling_region.right - self.screen.cursor.x
else
self.cols - self.screen.cursor.x - 1;
const count = @min(max, @max(count_req, 1));
self.screen.cursorRight(@intCast(count));
}
/// Move the cursor to the left amount cells. If amount is 0, adjust it to 1.
pub fn cursorLeft(self: *Terminal, count_req: usize) void {
// Wrapping behavior depends on various terminal modes
@ -3661,6 +3678,72 @@ test "Terminal: cursorDown resets wrap" {
}
}
test "Terminal: cursorRight resets wrap" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);
for ("ABCDE") |c| try t.print(c);
try testing.expect(t.screen.cursor.pending_wrap);
t.cursorRight(1);
try testing.expect(!t.screen.cursor.pending_wrap);
try t.print('X');
{
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABCDX", str);
}
}
test "Terminal: cursorRight to the edge of screen" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);
t.cursorRight(100);
try t.print('X');
{
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings(" X", str);
}
}
test "Terminal: cursorRight left of right margin" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);
t.scrolling_region.right = 2;
t.cursorRight(100);
try t.print('X');
{
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings(" X", str);
}
}
test "Terminal: cursorRight right of right margin" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);
t.scrolling_region.right = 2;
t.setCursorPos(1, 4);
t.cursorRight(100);
try t.print('X');
{
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings(" X", str);
}
}
test "Terminal: deleteLines simple" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);