From 700129d07948cac9f5529e9889beb99778532181 Mon Sep 17 00:00:00 2001 From: Joel Christner Date: Mon, 1 Jun 2026 16:01:29 -0700 Subject: [PATCH] Clamp selection text bounds --- src/XTerm.NET.Tests/SelectionTests.cs | 26 +++++++++++++++++++++ src/XTerm.NET/Selection/SelectionManager.cs | 7 ++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/XTerm.NET.Tests/SelectionTests.cs b/src/XTerm.NET.Tests/SelectionTests.cs index 777470c..6cf02da 100644 --- a/src/XTerm.NET.Tests/SelectionTests.cs +++ b/src/XTerm.NET.Tests/SelectionTests.cs @@ -68,6 +68,32 @@ public void SelectAll_IncludesScrollback_NotJustViewport() Assert.Contains("Line7", selectedText); } + [Fact] + public void SelectionText_ClampsNegativeColumns() + { + var terminal = new Terminal(new TerminalOptions { Rows = 3, Cols = 10, Scrollback = 20 }); + terminal.Write("alpha"); + + terminal.Selection.StartSelection(-3, 0); + terminal.Selection.UpdateSelection(4, 0); + terminal.Selection.EndSelection(); + + Assert.Equal("alpha", terminal.Selection.GetSelectionText()); + } + + [Fact] + public void SelectionText_ClampsColumnsPastRightEdge() + { + var terminal = new Terminal(new TerminalOptions { Rows = 3, Cols = 10, Scrollback = 20 }); + terminal.Write("alpha"); + + terminal.Selection.StartSelection(0, 0); + terminal.Selection.UpdateSelection(30, 0); + terminal.Selection.EndSelection(); + + Assert.StartsWith("alpha", terminal.Selection.GetSelectionText()); + } + [Fact] public void Selection_IsCleared_WhenTrimRemovesSelectedLines() { diff --git a/src/XTerm.NET/Selection/SelectionManager.cs b/src/XTerm.NET/Selection/SelectionManager.cs index 0cdfea3..953578c 100644 --- a/src/XTerm.NET/Selection/SelectionManager.cs +++ b/src/XTerm.NET/Selection/SelectionManager.cs @@ -145,8 +145,11 @@ public string GetSelectionText() if (line == null) continue; - int startX = (y == start.y) ? start.x : 0; - int endX = (y == end.y) ? end.x : _terminal.Cols - 1; + int startX = Math.Clamp((y == start.y) ? start.x : 0, 0, _terminal.Cols - 1); + int endX = Math.Clamp((y == end.y) ? end.x : _terminal.Cols - 1, 0, _terminal.Cols - 1); + + if (startX > endX) + continue; var lineText = line.TranslateToString(false, startX, endX + 1); text.Append(lineText);