From 68260cc3d09e16c377522d40a80ebef5c3a49400 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 4 Aug 2022 14:40:28 -0700 Subject: [PATCH] fix some selection contains logic on single lines --- src/terminal/Selection.zig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/terminal/Selection.zig b/src/terminal/Selection.zig index f501dc994..c37aca743 100644 --- a/src/terminal/Selection.zig +++ b/src/terminal/Selection.zig @@ -22,6 +22,14 @@ pub fn contains(self: Selection, p: ScreenPoint) bool { const tl = self.topLeft(); const br = self.bottomRight(); + // Honestly there is probably way more efficient boolean logic here. + // Look back at this in the future... + + // If tl/br are same line + if (tl.y == br.y) return p.y == tl.y and + p.x >= tl.x and + p.x <= br.x; + // If on top line, just has to be left of X if (p.y == tl.y) return p.x >= tl.x; @@ -84,4 +92,16 @@ test "Selection: contains" { try testing.expect(!sel.contains(.{ .x = 1, .y = 1 })); try testing.expect(!sel.contains(.{ .x = 5, .y = 2 })); } + + // Single line + { + const sel: Selection = .{ + .start = .{ .x = 5, .y = 1 }, + .end = .{ .x = 10, .y = 1 }, + }; + + try testing.expect(sel.contains(.{ .x = 6, .y = 1 })); + try testing.expect(!sel.contains(.{ .x = 2, .y = 1 })); + try testing.expect(!sel.contains(.{ .x = 12, .y = 1 })); + } }