Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/XTerm.NET.Tests/SelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
7 changes: 5 additions & 2 deletions src/XTerm.NET/Selection/SelectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
tomlm marked this conversation as resolved.
Outdated

var lineText = line.TranslateToString(false, startX, endX + 1);
text.Append(lineText);
Expand Down
Loading