From 1f01b2c4c94b03866bd41f8f80418a77026187de Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 5 Mar 2024 22:10:34 -0800 Subject: [PATCH] terminal2: selection adjust right --- src/terminal2/PageList.zig | 9 ++- src/terminal2/Selection.zig | 149 +++++++++++++++++++----------------- 2 files changed, 87 insertions(+), 71 deletions(-) diff --git a/src/terminal2/PageList.zig b/src/terminal2/PageList.zig index b5569d935..5327e49ea 100644 --- a/src/terminal2/PageList.zig +++ b/src/terminal2/PageList.zig @@ -1546,7 +1546,14 @@ pub fn cellIterator( ) CellIterator { var row_it = self.rowIterator(direction, tl_pt, bl_pt); var cell = row_it.next() orelse return .{ .row_it = row_it }; - if (direction == .left_up) cell.x = cell.page.data.size.cols - 1; + cell.x = switch (direction) { + .right_down => tl_pt.coord().x, + .left_up => if (bl_pt) |pt| + pt.coord().x + else + cell.page.data.size.cols - 1, + }; + return .{ .row_it = row_it, .cell = cell }; } diff --git a/src/terminal2/Selection.zig b/src/terminal2/Selection.zig index 440177604..4f6fd8b0e 100644 --- a/src/terminal2/Selection.zig +++ b/src/terminal2/Selection.zig @@ -120,9 +120,6 @@ pub fn adjust( s: *const Screen, adjustment: Adjustment, ) void { - _ = self; - _ = s; - //const screen_end = Screen.RowIndexTag.screen.maxLen(screen) - 1; // Note that we always adjusts "end" because end always represents @@ -162,28 +159,24 @@ pub fn adjust( // } // }, - // .right => { - // // Step right, wrapping to the next row down at the start of each new line, - // // until we find a non-empty cell. - // var iterator = result.end.iterator(screen, .right_down); - // _ = iterator.next(); - // while (iterator.next()) |next| { - // if (next.y > screen_end) break; - // if (screen.getCell( - // .screen, - // next.y, - // next.x, - // ).char != 0) { - // if (next.y > screen_end) { - // result.end.y = screen_end; - // } else { - // result.end = next; - // } - // break; - // } - // } - // }, - // + .right => { + // Step right, wrapping to the next row down at the start of each new line, + // until we find a non-empty cell. + var it = s.pages.cellIterator( + .right_down, + s.pages.pointFromPin(.screen, self.end.*).?, + null, + ); + _ = it.next(); + while (it.next()) |next| { + const rac = next.rowAndCell(); + if (rac.cell.hasText()) { + self.end.* = next; + break; + } + } + }, + // .page_up => if (screen.rows > result.end.y) { // result.end.y = 0; // result.end.x = 0; @@ -218,52 +211,68 @@ test "Selection: adjust right" { defer s.deinit(); try s.testWriteString("A1234\nB5678\nC1234\nD5678"); - // // Simple movement right - // { - // var sel = try Selection.init( - // &s, - // s.pages.pin(.{ .screen = .{ .x = 5, .y = 1 } }).?, - // s.pages.pin(.{ .screen = .{ .x = 3, .y = 3 } }).?, - // false, - // ); - // defer sel.deinit(&s); - // sel.adjust(&s, .right); - // - // try testing.expectEqual(point.Point{ .screen = .{ - // .x = 5, - // .y = 1, - // } }, s.pages.pointFromPin(.screen, sel.start.*).?); - // try testing.expectEqual(point.Point{ .screen = .{ - // .x = 4, - // .y = 3, - // } }, s.pages.pointFromPin(.screen, sel.end.*).?); - // } + // Simple movement right + { + var sel = try Selection.init( + &s, + s.pages.pin(.{ .screen = .{ .x = 5, .y = 1 } }).?, + s.pages.pin(.{ .screen = .{ .x = 3, .y = 3 } }).?, + false, + ); + defer sel.deinit(&s); + sel.adjust(&s, .right); - // // Already at end of the line. - // { - // const sel = (Selection{ - // .start = .{ .x = 5, .y = 1 }, - // .end = .{ .x = 4, .y = 2 }, - // }).adjust(&screen, .right); - // - // try testing.expectEqual(Selection{ - // .start = .{ .x = 5, .y = 1 }, - // .end = .{ .x = 0, .y = 3 }, - // }, sel); - // } - // - // // Already at end of the screen - // { - // const sel = (Selection{ - // .start = .{ .x = 5, .y = 1 }, - // .end = .{ .x = 4, .y = 3 }, - // }).adjust(&screen, .right); - // - // try testing.expectEqual(Selection{ - // .start = .{ .x = 5, .y = 1 }, - // .end = .{ .x = 4, .y = 3 }, - // }, sel); - // } + try testing.expectEqual(point.Point{ .screen = .{ + .x = 5, + .y = 1, + } }, s.pages.pointFromPin(.screen, sel.start.*).?); + try testing.expectEqual(point.Point{ .screen = .{ + .x = 4, + .y = 3, + } }, s.pages.pointFromPin(.screen, sel.end.*).?); + } + + // Already at end of the line. + { + var sel = try Selection.init( + &s, + s.pages.pin(.{ .screen = .{ .x = 4, .y = 1 } }).?, + s.pages.pin(.{ .screen = .{ .x = 4, .y = 2 } }).?, + false, + ); + defer sel.deinit(&s); + sel.adjust(&s, .right); + + try testing.expectEqual(point.Point{ .screen = .{ + .x = 4, + .y = 1, + } }, s.pages.pointFromPin(.screen, sel.start.*).?); + try testing.expectEqual(point.Point{ .screen = .{ + .x = 0, + .y = 3, + } }, s.pages.pointFromPin(.screen, sel.end.*).?); + } + + // Already at end of the screen + { + var sel = try Selection.init( + &s, + s.pages.pin(.{ .screen = .{ .x = 5, .y = 1 } }).?, + s.pages.pin(.{ .screen = .{ .x = 4, .y = 3 } }).?, + false, + ); + defer sel.deinit(&s); + sel.adjust(&s, .right); + + try testing.expectEqual(point.Point{ .screen = .{ + .x = 5, + .y = 1, + } }, s.pages.pointFromPin(.screen, sel.start.*).?); + try testing.expectEqual(point.Point{ .screen = .{ + .x = 4, + .y = 3, + } }, s.pages.pointFromPin(.screen, sel.end.*).?); + } } test "Selection: order, standard" {