Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions src/XTerm.NET.Tests/SelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,43 @@ public void SelectAll_IncludesScrollback_NotJustViewport()
}

[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 SelectionText_ReturnsEmpty_WhenTerminalHasNoColumns()
{
var terminal = new Terminal(new TerminalOptions { Rows = 3, Cols = 0, Scrollback = 20 });

terminal.Selection.StartSelection(0, 0);
terminal.Selection.UpdateSelection(0, 0);
terminal.Selection.EndSelection();

Assert.Equal(string.Empty, terminal.Selection.GetSelectionText());
}

public void SelectionText_UsesLineFeedLineEndings()
{
var terminal = new Terminal(new TerminalOptions { Rows = 3, Cols = 80, Scrollback = 20 });
Expand Down
11 changes: 9 additions & 2 deletions src/XTerm.NET/Selection/SelectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,15 @@ 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 lastColumn = _terminal.Cols - 1;
if (lastColumn < 0)
continue;

int startX = Math.Clamp((y == start.y) ? start.x : 0, 0, lastColumn);
int endX = Math.Clamp((y == end.y) ? end.x : lastColumn, 0, lastColumn);

if (startX > endX)
continue;

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