Skip to content

Commit e190f30

Browse files
committed
Merge branch 'develop' of tig:gui-cs/Terminal.Gui into develop
2 parents 2fe30c7 + a23c1be commit e190f30

File tree

9 files changed

+114
-75
lines changed

9 files changed

+114
-75
lines changed

Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ public override void Init (Action terminalResized)
907907

908908
Colors.Base.Normal = MakeColor (Color.White, Color.Blue);
909909
Colors.Base.Focus = MakeColor (Color.Black, Color.Gray);
910-
Colors.Base.HotNormal = MakeColor (Color.Cyan, Color.Blue);
910+
Colors.Base.HotNormal = MakeColor (Color.BrightCyan, Color.Blue);
911911
Colors.Base.HotFocus = MakeColor (Color.BrightBlue, Color.Gray);
912912
Colors.Base.Disabled = MakeColor (Color.DarkGray, Color.Blue);
913913

Terminal.Gui/ConsoleDrivers/NetDriver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ public override void Init (Action terminalResized)
13511351

13521352
Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.DarkBlue);
13531353
Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
1354-
Colors.Base.HotNormal = MakeColor (ConsoleColor.DarkCyan, ConsoleColor.DarkBlue);
1354+
Colors.Base.HotNormal = MakeColor (ConsoleColor.Cyan, ConsoleColor.DarkBlue);
13551355
Colors.Base.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
13561356
Colors.Base.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.DarkBlue);
13571357

Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ public override void Init (Action terminalResized)
14111411

14121412
Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.DarkBlue);
14131413
Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
1414-
Colors.Base.HotNormal = MakeColor (ConsoleColor.DarkCyan, ConsoleColor.DarkBlue);
1414+
Colors.Base.HotNormal = MakeColor (ConsoleColor.Cyan, ConsoleColor.DarkBlue);
14151415
Colors.Base.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
14161416
Colors.Base.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.DarkBlue);
14171417

Terminal.Gui/Core/TextFormatter.cs

+15-6
Original file line numberDiff line numberDiff line change
@@ -366,22 +366,28 @@ public List<ustring> Lines {
366366
/// </remarks>
367367
public bool NeedsFormat { get => needsFormat; set => needsFormat = value; }
368368

369-
static ustring StripCRLF (ustring str)
369+
static ustring StripCRLF (ustring str, bool keepNewLine = false)
370370
{
371371
var runes = str.ToRuneList ();
372372
for (int i = 0; i < runes.Count; i++) {
373373
switch (runes [i]) {
374374
case '\n':
375-
runes.RemoveAt (i);
375+
if (!keepNewLine) {
376+
runes.RemoveAt (i);
377+
}
376378
break;
377379

378380
case '\r':
379381
if ((i + 1) < runes.Count && runes [i + 1] == '\n') {
380382
runes.RemoveAt (i);
381-
runes.RemoveAt (i + 1);
383+
if (!keepNewLine) {
384+
runes.RemoveAt (i);
385+
}
382386
i++;
383387
} else {
384-
runes.RemoveAt (i);
388+
if (!keepNewLine) {
389+
runes.RemoveAt (i);
390+
}
385391
}
386392
break;
387393
}
@@ -515,6 +521,7 @@ public static List<ustring> WordWrap (ustring text, int width, bool preserveTrai
515521

516522
int GetNextWhiteSpace (int from, int cWidth, out bool incomplete, int cLength = 0)
517523
{
524+
var lastFrom = from;
518525
var to = from;
519526
var length = cLength;
520527
incomplete = false;
@@ -552,8 +559,10 @@ int GetNextWhiteSpace (int from, int cWidth, out bool incomplete, int cLength =
552559
}
553560
to++;
554561
}
555-
if (cLength > 0 && to < runes.Count && runes [to] != ' ') {
562+
if (cLength > 0 && to < runes.Count && runes [to] != ' ' && runes [to] != '\t') {
556563
return from;
564+
} else if (cLength > 0 && to < runes.Count && (runes [to] == ' ' || runes [to] == '\t')) {
565+
return lastFrom;
557566
} else {
558567
return to;
559568
}
@@ -727,7 +736,7 @@ public static List<ustring> Format (ustring text, int width, bool justify, bool
727736
return lineResult;
728737
}
729738

730-
var runes = text.ToRuneList ();
739+
var runes = StripCRLF (text, true).ToRuneList ();
731740
int runeCount = runes.Count;
732741
int lp = 0;
733742
for (int i = 0; i < runeCount; i++) {

Terminal.Gui/Views/TextView.cs

+2-26
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ void WrapTextModel ()
15191519
out int nStartRow, out int nStartCol,
15201520
currentRow, currentColumn,
15211521
selectionStartRow, selectionStartColumn,
1522-
tabWidth, preserveTrailingSpaces: !ReadOnly);
1522+
tabWidth, preserveTrailingSpaces: true);
15231523
currentRow = nRow;
15241524
currentColumn = nCol;
15251525
selectionStartRow = nStartRow;
@@ -1610,10 +1610,7 @@ public ustring SelectedText {
16101610
}
16111611

16121612
SetWrapModel ();
1613-
var savedCurrentColumn = CurrentColumn;
1614-
currentColumn = GetCurrentColumnReadOnyWrapModel (true);
16151613
var sel = GetRegion ();
1616-
currentColumn = savedCurrentColumn;
16171614
UpdateWrapModel ();
16181615
Adjust ();
16191616

@@ -1993,9 +1990,6 @@ public bool ReadOnly {
19931990
if (value != isReadOnly) {
19941991
isReadOnly = value;
19951992

1996-
SetWrapModel ();
1997-
currentColumn = GetCurrentColumnReadOnyWrapModel ();
1998-
UpdateWrapModel ();
19991993
SetNeedsDisplay ();
20001994
Adjust ();
20011995
}
@@ -2316,7 +2310,7 @@ void UpdateWrapModel ([CallerMemberName] string caller = null)
23162310
wrapManager.UpdateModel (model, out int nRow, out int nCol,
23172311
out int nStartRow, out int nStartCol,
23182312
currentRow, currentColumn,
2319-
selectionStartRow, selectionStartColumn, preserveTrailingSpaces: !ReadOnly);
2313+
selectionStartRow, selectionStartColumn, preserveTrailingSpaces: true);
23202314
currentRow = nRow;
23212315
currentColumn = nCol;
23222316
selectionStartRow = nStartRow;
@@ -2327,21 +2321,6 @@ void UpdateWrapModel ([CallerMemberName] string caller = null)
23272321
throw new InvalidOperationException ($"WordWrap settings was changed after the {currentCaller} call.");
23282322
}
23292323

2330-
int GetCurrentColumnReadOnyWrapModel (bool forcePreserveTrailingSpaces = false)
2331-
{
2332-
if (wordWrap) {
2333-
var wManager = new WordWrapManager (wrapManager.Model);
2334-
if (ReadOnly && !forcePreserveTrailingSpaces) {
2335-
wManager.WrapModel (frameWidth, out _, out _, out _, out _, preserveTrailingSpaces: false);
2336-
} else {
2337-
wManager.WrapModel (frameWidth, out _, out _, out _, out _, preserveTrailingSpaces: true);
2338-
}
2339-
var currentLine = wrapManager.GetWrappedLineColWidth (CurrentRow, CurrentColumn, wManager);
2340-
return currentLine;
2341-
}
2342-
return currentColumn;
2343-
}
2344-
23452324
///<inheritdoc/>
23462325
public override void Redraw (Rect bounds)
23472326
{
@@ -3803,8 +3782,6 @@ bool DeleteTextBackwards ()
38033782
public void Copy ()
38043783
{
38053784
SetWrapModel ();
3806-
var savedCurrentColumn = CurrentColumn;
3807-
currentColumn = GetCurrentColumnReadOnyWrapModel (true);
38083785
if (selecting) {
38093786
SetClipboard (GetRegion ());
38103787
copyWithoutSelection = false;
@@ -3813,7 +3790,6 @@ public void Copy ()
38133790
SetClipboard (ustring.Make (currentLine));
38143791
copyWithoutSelection = true;
38153792
}
3816-
currentColumn = savedCurrentColumn;
38173793
UpdateWrapModel ();
38183794
DoNeededAction ();
38193795
}

UnitTests/ScrollBarViewTests.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
142142
var v = new ScrollBarView (host, true);
143143
var h = new ScrollBarView (host, true);
144144

145-
Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
146-
Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
145+
Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
146+
Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
147147
}
148148

149149
[Fact]
@@ -156,8 +156,8 @@ public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
156156
var v = new ScrollBarView (host, false);
157157
var h = new ScrollBarView (host, false);
158158

159-
Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
160-
Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
159+
Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
160+
Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
161161
}
162162

163163
[Fact]
@@ -745,7 +745,7 @@ public void Hosting_ShowBothScrollIndicator_Invisible ()
745745
Assert.True (textView.WordWrap);
746746
Assert.True (scrollBar.AutoHideScrollBars);
747747
Assert.Equal (7, textView.Lines);
748-
Assert.Equal (23, textView.Maxlength);
748+
Assert.Equal (22, textView.Maxlength);
749749
Assert.Equal (0, textView.LeftColumn);
750750
Assert.Equal (0, scrollBar.Position);
751751
Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
@@ -754,8 +754,8 @@ public void Hosting_ShowBothScrollIndicator_Invisible ()
754754
│This is the help text │
755755
│for the Second Step. │
756756
│ │
757-
│Press the button to see
758-
│ a message box.
757+
│Press the button to
758+
see a message box. │
759759
│ │
760760
│Enter name too. │
761761
│ │
@@ -780,21 +780,21 @@ public void Hosting_ShowBothScrollIndicator_Invisible ()
780780

781781
Assert.True (textView.WordWrap);
782782
Assert.True (scrollBar.AutoHideScrollBars);
783-
Assert.Equal (19, textView.Lines);
783+
Assert.Equal (20, textView.Lines);
784784
Assert.Equal (7, textView.Maxlength);
785785
Assert.Equal (0, textView.LeftColumn);
786786
Assert.Equal (0, scrollBar.Position);
787787
Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
788788
expected = @"
789789
┌ Test ──┐
790-
│This is▲│
791-
│ the ┬│
790+
│This ▲│
791+
is the ┬│
792792
│help ││
793793
│text ┴│
794-
│for the░│
795-
Second░│
796-
Step. ░│
797-
▼│
794+
│for ░│
795+
the ░│
796+
Second ░│
797+
Step. ▼│
798798
└────────┘
799799
";
800800

UnitTests/TextFormatterTests.cs

+57-4
Original file line numberDiff line numberDiff line change
@@ -2006,10 +2006,33 @@ public void WordWrap_preserveTrailingSpaces ()
20062006
wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
20072007
Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
20082008
Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
2009-
Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
2010-
Assert.Equal (" words.", wrappedLines [1].ToString ());
2009+
Assert.Equal ("A sentence ", wrappedLines [0].ToString ());
2010+
Assert.Equal ("has words.", wrappedLines [1].ToString ());
20112011
Assert.True (wrappedLines.Count == 2);
20122012

2013+
maxWidth = 8;
2014+
expectedClippedWidth = 8;
2015+
wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
2016+
Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
2017+
Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
2018+
Assert.Equal ("A ", wrappedLines [0].ToString ());
2019+
Assert.Equal ("sentence", wrappedLines [1].ToString ());
2020+
Assert.Equal (" has ", wrappedLines [2].ToString ());
2021+
Assert.Equal ("words.", wrappedLines [^1].ToString ());
2022+
Assert.True (wrappedLines.Count == 4);
2023+
2024+
maxWidth = 6;
2025+
expectedClippedWidth = 6;
2026+
wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
2027+
Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
2028+
Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
2029+
Assert.Equal ("A ", wrappedLines [0].ToString ());
2030+
Assert.Equal ("senten", wrappedLines [1].ToString ());
2031+
Assert.Equal ("ce ", wrappedLines [2].ToString ());
2032+
Assert.Equal ("has ", wrappedLines [3].ToString ());
2033+
Assert.Equal ("words.", wrappedLines [^1].ToString ());
2034+
Assert.True (wrappedLines.Count == 5);
2035+
20132036
maxWidth = 3;
20142037
expectedClippedWidth = 3;
20152038
wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
@@ -2312,6 +2335,18 @@ public void WordWrap_preserveTrailingSpaces_With_Tab ()
23122335
Assert.Equal ("words.", wrappedLines [2].ToString ());
23132336
Assert.True (wrappedLines.Count == 3);
23142337

2338+
maxWidth = 8;
2339+
expectedClippedWidth = 8;
2340+
wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
2341+
Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
2342+
Assert.Equal ("A ", wrappedLines [0].ToString ());
2343+
Assert.Equal ("sentence", wrappedLines [1].ToString ());
2344+
Assert.Equal ("\t\t", wrappedLines [2].ToString ());
2345+
Assert.Equal ("\t ", wrappedLines [3].ToString ());
2346+
Assert.Equal ("has ", wrappedLines [4].ToString ());
2347+
Assert.Equal ("words.", wrappedLines [^1].ToString ());
2348+
Assert.True (wrappedLines.Count == 6);
2349+
23152350
maxWidth = 3;
23162351
expectedClippedWidth = 3;
23172352
wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
@@ -2873,8 +2908,8 @@ public void Format_WordWrap_preserveTrailingSpaces ()
28732908
Assert.Equal (" A ", list2 [0].ToString ());
28742909
Assert.Equal ("sent", list2 [1].ToString ());
28752910
Assert.Equal ("ence", list2 [2].ToString ());
2876-
Assert.Equal (" has", list2 [3].ToString ());
2877-
Assert.Equal (" ", list2 [4].ToString ());
2911+
Assert.Equal (" ", list2 [3].ToString ());
2912+
Assert.Equal ("has ", list2 [4].ToString ());
28782913
Assert.Equal ("word", list2 [5].ToString ());
28792914
Assert.Equal ("s. ", list2 [6].ToString ());
28802915
Assert.Equal (" ", list2 [7].ToString ());
@@ -3959,5 +3994,23 @@ public void AutoSize_False_View_Width_Zero_Returns_Minimum_Width_With_Wide_Rune
39593994
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
39603995
Assert.Equal (new Rect (0, 0, 4, 10), pos);
39613996
}
3997+
3998+
[Fact]
3999+
public void Format_With_PreserveTrailingSpaces_And_Without_PreserveTrailingSpaces ()
4000+
{
4001+
var text = $"Line1{Environment.NewLine}Line2{Environment.NewLine}Line3{Environment.NewLine}";
4002+
var width = 60;
4003+
var preserveTrailingSpaces = false;
4004+
var formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
4005+
Assert.Equal ("Line1", formated [0]);
4006+
Assert.Equal ("Line2", formated [1]);
4007+
Assert.Equal ("Line3", formated [^1]);
4008+
4009+
preserveTrailingSpaces = true;
4010+
formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
4011+
Assert.Equal ("Line1", formated [0]);
4012+
Assert.Equal ("Line2", formated [1]);
4013+
Assert.Equal ("Line3", formated [^1]);
4014+
}
39624015
}
39634016
}

UnitTests/TextViewTests.cs

+22-21
Original file line numberDiff line numberDiff line change
@@ -1967,17 +1967,16 @@ public void WordWrap_WrapModel_Output ()
19671967

19681968
tv.Redraw (tv.Bounds);
19691969

1970-
string expected = @"
1970+
GraphViewTests.AssertDriverContentsWithFrameAre (@"
19711971
This is
1972-
the first
1973-
line.
1972+
the
1973+
first
1974+
line.
19741975
This is
1975-
the
1976-
second
1977-
line.
1978-
";
1979-
1980-
GraphViewTests.AssertDriverContentsAre (expected, output);
1976+
the
1977+
second
1978+
line.
1979+
", output);
19811980
}
19821981

19831982
[Fact]
@@ -2050,26 +2049,28 @@ public void WordWrap_ReadOnly_CursorPosition_SelectedText_Copy ()
20502049
Application.Top.Add (tv);
20512050

20522051
tv.Redraw (tv.Bounds);
2053-
GraphViewTests.AssertDriverContentsAre (@"
2054-
This is
2052+
GraphViewTests.AssertDriverContentsWithFrameAre (@"
2053+
This is
20552054
the first
2056-
line.
2057-
This is
2058-
the second
2059-
line.
2055+
line.
2056+
This is
2057+
the
2058+
second
2059+
line.
20602060
", output);
20612061

20622062
tv.ReadOnly = true;
20632063
tv.CursorPosition = new Point (6, 2);
20642064
Assert.Equal (new Point (5, 2), tv.CursorPosition);
20652065
tv.Redraw (tv.Bounds);
2066-
GraphViewTests.AssertDriverContentsAre (@"
2067-
This is
2066+
GraphViewTests.AssertDriverContentsWithFrameAre (@"
2067+
This is
20682068
the first
2069-
line.
2070-
This is
2071-
the second
2072-
line.
2069+
line.
2070+
This is
2071+
the
2072+
second
2073+
line.
20732074
", output);
20742075

20752076
tv.SelectionStartRow = 0;

0 commit comments

Comments
 (0)