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
55 changes: 55 additions & 0 deletions src/XTerm.NET.Tests/OscTerminatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using XTerm;
using XTerm.Events;
using XTerm.Options;
using Xunit;

namespace XTerm.Tests;

/// <summary>
/// <see cref="TerminalEvents.OscReceivedEventArgs.EndedWithBel"/>: which terminator the program used,
/// so a handler can answer in kind.
/// </summary>
public class OscTerminatorTests
{
private static readonly string Esc = ((char)0x1B).ToString();

private static TerminalEvents.OscReceivedEventArgs Receive(string sequence)
{
var terminal = new Terminal(new TerminalOptions { Cols = 20, Rows = 5 });
var seen = new List<TerminalEvents.OscReceivedEventArgs>();
terminal.OscReceived += (_, e) => seen.Add(e);
terminal.Write(sequence);
return Assert.Single(seen);
}

[Fact]
public void A_sequence_ended_with_BEL_says_so()
{
Assert.True(Receive($"{Esc}]1337;SetMark\u0007").EndedWithBel);
}

[Fact]
public void A_sequence_ended_with_ST_says_not()
{
Assert.False(Receive($"{Esc}]1337;SetMark{Esc}\\").EndedWithBel);
}

[Fact]
public void Each_sequence_reports_its_own_terminator()
{
var terminal = new Terminal(new TerminalOptions { Cols = 20, Rows = 5 });
var seen = new List<bool>();
terminal.OscReceived += (_, e) => seen.Add(e.EndedWithBel);

terminal.Write($"{Esc}]1337;A\u0007{Esc}]1337;B{Esc}\\{Esc}]1337;C\u0007");

Assert.Equal(new[] { true, false, true }, seen);
}

[Fact]
public void The_old_constructor_still_builds_one_that_ended_with_ST()
{
var e = new TerminalEvents.OscReceivedEventArgs("1", 1, "", "1", recognized: false);
Assert.False(e.EndedWithBel);
}
}
158 changes: 158 additions & 0 deletions src/XTerm.NET.Tests/WordSeparatorsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using XTerm;
using XTerm.Options;
using XTerm.Selection;
using Xunit;

namespace XTerm.Tests;

/// <summary>
/// <see cref="TerminalOptions.WordSeparators"/>: where a double-click word ends. Null keeps the
/// built-in rule; a string lists the separators, everything else being part of a word.
/// </summary>
public class WordSeparatorsTests
{
private static Terminal Write(string text, string? separators = null)
{
var t = new Terminal(new TerminalOptions { Cols = 40, Rows = 3, WordSeparators = separators });
t.Write(text);
return t;
}

private static string DoubleClick(Terminal t, int col)
{
t.Selection.StartSelection(col, 0, SelectionMode.Word);
return t.Selection.GetSelectionText();
}

[Fact]
public void By_default_punctuation_ends_a_word()
{
var t = Write("cat ~/src/foo-bar.txt");
Assert.Equal("foo", DoubleClick(t, 11));
}

[Fact]
public void Separators_listed_end_a_word_and_nothing_else_does()
{
var t = Write("cat ~/src/foo-bar.txt", " ");
Assert.Equal("~/src/foo-bar.txt", DoubleClick(t, 11));
}

[Fact]
public void Ghosttys_default_list_keeps_a_path_whole_and_stops_at_a_colon()
{
var t = Write("at src/foo.cs:42 here", "\t '\"│`|:;,()[]{}<>$");
Assert.Equal("src/foo.cs", DoubleClick(t, 5));
}

[Fact]
public void A_separator_outside_the_basic_plane_counts_as_one_code_point()
{
var t = Write("ab\U0001F600cd", "\U0001F600");
Assert.Equal("ab", DoubleClick(t, 0));
}

/// <summary>
/// A wide glyph is two cells, the second a width-0 continuation with no content. Expansion walks the
/// glyph whole, from either side and from a click on either half.
/// </summary>
[Theory]
[InlineData("a\U0001F600b", 0)]
[InlineData("a\U0001F600b", 3)]
[InlineData("a\U0001F600b", 2)]
[InlineData("a\u7532b", 0)]
[InlineData("a\u7532b", 3)]
public void A_wide_character_that_is_not_a_separator_stays_inside_the_word(string text, int col)
{
var t = Write(text, " ");
Assert.Equal(text, DoubleClick(t, col));
}

[Fact]
public void A_wide_letter_stays_inside_the_word_under_the_default_rule_too()
{
// The built-in rule counts a CJK letter as a word character; its continuation cell has to agree.
var t = Write("a\u7532b");
Assert.Equal("a\u7532b", DoubleClick(t, 0));
}

[Fact]
public void A_wide_separator_still_ends_the_word()
{
// The space too, so the blank cells after "cd" end it on the right; the emoji ends it on the left.
var t = Write("ab\U0001F600cd", " \U0001F600");
Assert.Equal("cd", DoubleClick(t, 4));
}

[Fact]
public void A_wide_separators_second_cell_is_not_taken_into_the_word_beside_it()
{
// Its continuation adds no text, so only the range shows it: the word starts at "c", not one
// cell earlier on the separator's second half.
var t = Write("ab\U0001F600cd", " \U0001F600");
t.Selection.StartSelection(4, 0, SelectionMode.Word);

Assert.True(t.Selection.TryGetSelection(out var range));
Assert.Equal(4, range.StartX);
}

[Fact]
public void An_empty_cell_that_follows_no_wide_glyph_still_ends_a_word()
{
// Width 0 and no content, but not a continuation: nothing wide comes before it.
var t = Write("ab cd", "");
t.Buffer.Lines[0]![2] = XTerm.Buffer.BufferCell.Empty;

Assert.Equal("ab", DoubleClick(t, 0).TrimEnd());
}

/// <summary>
/// Blank cells hold a space, not nothing, so a gap the cursor skipped is spaces like any other: a
/// list without the space joins across it, and one with it stops there. Ghostty's default has it.
/// </summary>
[Fact]
public void Blank_cells_are_spaces_so_a_list_needs_the_space_to_stop_at_a_gap()
{
var joined = Write("one", "");
joined.Write("\u001b[1;6Htwo");
Assert.StartsWith("one two", DoubleClick(joined, 1));

var stopped = Write("one", " ");
stopped.Write("\u001b[1;6Htwo");
Assert.Equal("one", DoubleClick(stopped, 1));
}

[Fact]
public void A_change_applies_to_the_next_double_click()
{
var t = Write("foo-bar");
Assert.Equal("foo", DoubleClick(t, 1));

t.Options.WordSeparators = " ";
Assert.Equal("foo-bar", DoubleClick(t, 1));

// One list to another, not only to and from null: the parsed set has to follow the string.
t.Options.WordSeparators = "-";
Assert.Equal("foo", DoubleClick(t, 1));

t.Options.WordSeparators = null;
Assert.Equal("foo", DoubleClick(t, 1));
}

[Fact]
public void The_rule_is_public_for_a_host_to_share()
{
var t = Write("", "-");
Assert.True(t.Selection.IsWordChar("."));
Assert.False(t.Selection.IsWordChar("-"));
Assert.False(t.Selection.IsWordChar(""));
Assert.False(t.Selection.IsWordChar(null));
}

[Fact]
public void A_copy_of_the_options_keeps_it()
{
var options = new TerminalOptions { WordSeparators = " :" };
Assert.Equal(" :", options.Clone().WordSeparators);
}
}
15 changes: 15 additions & 0 deletions src/XTerm.NET/Events/TerminalEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,18 @@ public AttentionRequestedEventArgs(string action)
public class OscReceivedEventArgs : EventArgs
{
public OscReceivedEventArgs(string identifier, int code, string data, string raw, bool recognized)
: this(identifier, code, data, raw, recognized, endedWithBel: false)
{
}

public OscReceivedEventArgs(string identifier, int code, string data, string raw, bool recognized, bool endedWithBel)
{
Identifier = identifier;
Code = code;
Data = data;
Raw = raw;
Recognized = recognized;
EndedWithBel = endedWithBel;
}

/// <summary>
Expand Down Expand Up @@ -460,6 +466,15 @@ public OscReceivedEventArgs(string identifier, int code, string data, string raw
/// handler is the only thing that will act on it.
/// </summary>
public bool Recognized { get; }

/// <summary>
/// Whether the program ended the sequence with BEL (0x07) rather than ST (<c>ESC \</c>).
/// </summary>
/// <remarks>
/// For a handler that replies: answering in the terminator the program used is what xterm
/// does, and what the terminal's own replies do.
/// </remarks>
public bool EndedWithBel { get; }
}

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions src/XTerm.NET/Options/TerminalOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,23 @@ public int TabStopWidth
/// </remarks>
public string EnquiryResponse { get; set; } = string.Empty;

/// <summary>
/// The characters that end a word when a double-click selects one. Null, the default, keeps the
/// built-in rule: letters, digits and <c>_</c> are word characters and everything else ends a word.
/// </summary>
/// <remarks>
/// <para>Given a string, every character NOT in it is part of a word, so <c>.</c>, <c>-</c> and
/// <c>/</c> join a path or a URL unless listed. This is Ghostty's <c>selection-word-chars</c>, whose
/// default is <c>"\t '\"│`|:;,()[]{}&lt;&gt;$"</c>. Each code point counts on its own, so characters
/// outside the Basic Multilingual Plane work; a sequence of several code points (an emoji with a
/// modifier, say) cannot be one separator.</para>
/// <para>A cell with no content (code point 0, as NUL is in Ghostty) is always a boundary. Blank
/// cells hold a space rather than nothing, though, so a list without the space joins words across a
/// gap. A cell is judged by its first code point.</para>
/// <para>Read on every expansion, so a change applies to the next double-click.</para>
/// </remarks>
public string? WordSeparators { get; set; }

/// <summary>
/// How OSC colour queries (OSC 4, 5, 10, 11 and 12 with <c>?</c>) are answered.
/// </summary>
Expand Down Expand Up @@ -552,6 +569,7 @@ public TerminalOptions(TerminalOptions other)
KittyKeyboardEnabled = other.KittyKeyboardEnabled;
PointerShapesEnabled = other.PointerShapesEnabled;
EnquiryResponse = other.EnquiryResponse;
WordSeparators = other.WordSeparators;
ColorReportFormat = other.ColorReportFormat;
KeyboardActionModeAllowed = other.KeyboardActionModeAllowed;
MaxImageRegistryBytes = other.MaxImageRegistryBytes;
Expand Down
66 changes: 59 additions & 7 deletions src/XTerm.NET/Selection/SelectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using XTerm.Buffer;
using XTerm.Common;
using XTerm.Options;

namespace XTerm.Selection;

Expand Down Expand Up @@ -289,7 +290,7 @@ private void ExpandSelectionToWord()
var firstLine = first.y >= 0 && first.y < buffer.Lines.Length ? buffer.Lines[first.y] : null;
if (firstLine != null)
{
while (first.x > 0 && IsWordChar(firstLine[first.x - 1].Content))
while (first.x > 0 && IsWordCell(firstLine, first.x - 1))
{
first.x--;
}
Expand All @@ -298,7 +299,7 @@ private void ExpandSelectionToWord()
var lastLine = last.y >= 0 && last.y < buffer.Lines.Length ? buffer.Lines[last.y] : null;
if (lastLine != null)
{
while (last.x < _terminal.Cols - 1 && IsWordChar(lastLine[last.x + 1].Content))
while (last.x < _terminal.Cols - 1 && IsWordCell(lastLine, last.x + 1))
{
last.x++;
}
Expand Down Expand Up @@ -333,15 +334,66 @@ private void ExpandSelectionToLine()
}

/// <summary>
/// Checks if a character is a word character.
/// Whether the cell at <paramref name="x"/> is part of a word, whole glyphs at a time: a wide glyph's
/// second cell is a width-0 continuation with no content of its own, and belongs to the glyph before
/// it. Without that, expansion stopped halfway through a CJK character or an emoji. A cell that is
/// empty without following a wide glyph is still a boundary.
/// </summary>
private bool IsWordChar(string ch)
private bool IsWordCell(BufferLine line, int x)
{
if (string.IsNullOrEmpty(ch))
if (x > 0 && line.GetWidth(x) == 0 && line.GetWidth(x - 1) == 2)
return IsWordChar(line[x - 1].Content);
return IsWordChar(line[x].Content);
}

/// <summary>
/// Whether a cell's content is part of a word, by <see cref="TerminalOptions.WordSeparators"/>: what
/// double-click expansion stops at. Public so a host's own word motion agrees with the mouse.
/// </summary>
/// <param name="content">A cell's content. Null or empty, an empty cell, is never part of a word.</param>
public bool IsWordChar(string? content)
{
if (string.IsNullOrEmpty(content))
Comment thread
JohnCampionJr marked this conversation as resolved.
Resolved
return false;

var c = ch[0];
return char.IsLetterOrDigit(c) || c == '_';
var separators = _terminal.Options.WordSeparators;
if (separators is null)
{
var c = content[0];
return char.IsLetterOrDigit(c) || c == '_';
}

int first = char.IsHighSurrogate(content[0]) && content.Length > 1 && char.IsLowSurrogate(content[1])
? char.ConvertToUtf32(content[0], content[1])
: content[0];
return first != 0 && !SeparatorSet(separators).Contains(first);
}

// The separators as code points, rebuilt only when the option is set to a different string.
private string? _separatorSource;
private HashSet<int> _separatorSet = new();

private HashSet<int> SeparatorSet(string separators)
{
if (!ReferenceEquals(separators, _separatorSource))
{
var set = new HashSet<int>();
for (var i = 0; i < separators.Length; i++)
{
if (char.IsHighSurrogate(separators[i]) && i + 1 < separators.Length && char.IsLowSurrogate(separators[i + 1]))
{
set.Add(char.ConvertToUtf32(separators[i], separators[i + 1]));
i++;
}
else
{
set.Add(separators[i]);
}
}
_separatorSet = set;
_separatorSource = separators;
}
return _separatorSet;
}

private int ToAbsoluteY(int viewportY)
Expand Down
2 changes: 1 addition & 1 deletion src/XTerm.NET/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,7 @@ internal void RaiseAttentionRequested(string action) =>
internal void RaiseKittyNotificationReceived(string? identifier, string? title, string? body, int? urgency, string? icon) =>
NotificationReceived?.Invoke(this, new TerminalEvents.NotificationEventArgs(identifier, title, body, urgency, icon));
internal void RaiseOscReceived(string identifier, int code, string data, string raw, bool recognized) =>
OscReceived?.Invoke(this, new TerminalEvents.OscReceivedEventArgs(identifier, code, data, raw, recognized));
OscReceived?.Invoke(this, new TerminalEvents.OscReceivedEventArgs(identifier, code, data, raw, recognized, _parser.LastOscEndedWithBel));

internal void RaiseWindowMoved(int x, int y) =>
WindowMoved?.Invoke(this, new TerminalEvents.WindowMovedEventArgs(x, y));
Expand Down
Loading