فهرست منبع

fixed word wrapping to not strip spaces; ended up fixing other ww bugs too!

Charlie Kindel 5 سال پیش
والد
کامیت
c3395f41ab
3فایلهای تغییر یافته به همراه65 افزوده شده و 44 حذف شده
  1. 9 2
      Terminal.Gui/Core/TextFormatter.cs
  2. 43 22
      UICatalog/Scenarios/TextFormatterDemo.cs
  3. 13 20
      UnitTests/TextFormatterTests.cs

+ 9 - 2
Terminal.Gui/Core/TextFormatter.cs

@@ -162,6 +162,8 @@ namespace Terminal.Gui {
 						runes.RemoveAt (i);
 						runes.RemoveAt (i + 1);
 						i++;
+					} else {
+						runes.RemoveAt (i);
 					}
 					break;
 				}
@@ -182,6 +184,8 @@ namespace Terminal.Gui {
 						runes [i] = (Rune)' ';
 						runes.RemoveAt (i + 1);
 						i++;
+					} else {
+						runes [i] = (Rune)' ';
 					}
 					break;
 				}
@@ -223,12 +227,15 @@ namespace Terminal.Gui {
 					end -= 1;
 				if (end == start)
 					end = start + width;
-				lines.Add (ustring.Make (runes.GetRange (start, end - start)).TrimSpace ());
+				lines.Add (ustring.Make (runes.GetRange (start, end - start))); //.TrimStart ((rune) => rune == ' '));
 				start = end;
+				if (runes[end] == ' ') {
+					start++;
+				}
 			}
 
 			if (start < text.RuneCount) {
-				lines.Add (ustring.Make (runes.GetRange (start, runes.Count - start)).TrimSpace ());
+				lines.Add (ustring.Make (runes.GetRange (start, runes.Count - start))); //.TrimStart ((rune) => rune == ' '));
 			}
 
 			return lines;

+ 43 - 22
UICatalog/Scenarios/TextFormatterDemo.cs

@@ -1,40 +1,60 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Text;
 using Terminal.Gui;
+using Rune = System.Rune;
 
 namespace UICatalog {
 	[ScenarioMetadata (Name: "TextFormatter Demo", Description: "Demos and tests the TextFormatter class.")]
 	[ScenarioCategory ("Text")]
 	[ScenarioCategory ("POC")]
 	class TextFormatterDemo : Scenario {
-		public override void Init (Toplevel top, ColorScheme colorScheme)
-		{
-			Application.Init ();
+		//public override void Init (Toplevel top, ColorScheme colorScheme)
+		//{
+		//	Application.Init ();
 
-			Top = top;
-			if (Top == null) {
-				Top = Application.Top;
-			}
-			Win = null;
-		}
+		//	Top = top;
+		//	if (Top == null) {
+		//		Top = Application.Top;
+		//	}
+		//	Win = null;
+		//}
 
 		public override void Setup ()
 		{
-			Top.Text = "Press CTRL-Q to Quit. This is the Text for the TopLevel View. TextAlignment.Centered was specified. It is intentionally very long to illustrate word wrap.\n" +
-				"<-- There is a new line here to show a hard line break. You should see this text bleed underneath the subviews, which start at Y = 3.";
-			Top.TextAlignment = TextAlignment.Centered;
-			Top.ColorScheme = Colors.Base;
+			//Top.Text = "Press CTRL-Q to Quit. This is the Text for the TopLevel View. TextAlignment.Centered was specified. It is intentionally very long to illustrate word wrap.\n" +
+			//	"<-- There is a new line here to show a hard line break. You should see this text bleed underneath the subviews, which start at Y = 3.";
+			//Top.TextAlignment = TextAlignment.Centered;
+			//Top.ColorScheme = Colors.Base;
+
+			// Make Win smaller so sizing the window horizontally will make the
+			// labels shrink to zero-width
+			Win.X = 10;
+			Win.Width = Dim.Fill (10);
 
 			string text = "Hello world, how are you today? Pretty neat!\nSecond line\n\nFourth Line.";
 			string unicode = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ\nτὸ σπίτι φτωχικὸ στὶς ἀμμουδιὲς τοῦ Ὁμήρου.\nΜονάχη ἔγνοια ἡ γλῶσσα μου στὶς ἀμμουδιὲς τοῦ Ὁμήρου.";
 
+			Label blockText = new Label () { ColorScheme = Colors.TopLevel, X = 0, Y = 3, Height = 7, Width = Dim.Fill (0) };
+
+			var block = new StringBuilder ();
+			block.AppendLine ("                        _/                       ");
+			block.AppendLine ("     _/_/_/  _/    _/            _/_/_/    _/_/_/");
+			block.AppendLine ("  _/    _/  _/    _/  _/      _/        _/_/     ");
+			block.AppendLine (" _/    _/  _/    _/  _/      _/            _/_/  ");
+			block.AppendLine ("  _/_/_/    _/_/_/  _/  _/    _/_/_/  _/_/_/     ");
+			block.AppendLine ("     _/                                          ");
+			block.AppendLine ("_ /_/                                            ");
+			blockText.Text = block.ToString ();
+			Win.Add (blockText);
+
 			var unicodeCheckBox = new CheckBox ("Unicode", Top.HotKeySpecifier == (Rune)' ') {
 				X = 0,
-				Y = 3,
+				Y = Pos.Bottom (blockText) + 1,
 			};
 
-			Top.Add (unicodeCheckBox);
+			Win.Add (unicodeCheckBox);
 
 			var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
 			var singleLines = new Label [alignments.Count];
@@ -48,22 +68,22 @@ namespace UICatalog {
 			}
 
 			var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (unicodeCheckBox) + 1 };
-			Top.Add (label);
+			Win.Add (label);
 			foreach (var alignment in alignments) {
 				label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
-				Top.Add (label);
+				Win.Add (label);
 				singleLines [(int)alignment].Y = Pos.Bottom (label);
-				Top.Add (singleLines [(int)alignment]);
+				Win.Add (singleLines [(int)alignment]);
 				label = singleLines [(int)alignment];
 			}
 
 			label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) };
-			Top.Add (label);
+			Win.Add (label);
 			foreach (var alignment in alignments) {
 				label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
-				Top.Add (label);
+				Win.Add (label);
 				multipleLines [(int)alignment].Y = Pos.Bottom (label);
-				Top.Add (multipleLines [(int)alignment]);
+				Win.Add (multipleLines [(int)alignment]);
 				label = multipleLines [(int)alignment];
 			}
 
@@ -75,4 +95,5 @@ namespace UICatalog {
 			};
 		}
 	}
-}
+}
+

+ 13 - 20
UnitTests/TextFormatterTests.cs

@@ -1674,16 +1674,12 @@ namespace Terminal.Gui {
 			//Assert.True (wrappedLines.Count == );
 			Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
 			Assert.Equal ("A", wrappedLines [0].ToString ());
-			// BUGBUG: WordWrap breaks down with small widths. It should not
-			// the following line should be "sen"...
-			Assert.Equal ("se", wrappedLines [1].ToString ());
-			Assert.Equal ("nte", wrappedLines [2].ToString ());
-			Assert.Equal ("nce", wrappedLines [3].ToString ());
-			Assert.Equal ("ha", wrappedLines [4].ToString ());
-			Assert.Equal ("s", wrappedLines [5].ToString ());
-			Assert.Equal ("wo", wrappedLines [6].ToString ());
-			Assert.Equal ("rds", wrappedLines [7].ToString ());
-			Assert.Equal (".", wrappedLines [^1].ToString ());
+			Assert.Equal ("sen", wrappedLines [1].ToString ());
+			Assert.Equal ("ten", wrappedLines [2].ToString ());
+			Assert.Equal ("ce", wrappedLines [3].ToString ());
+			Assert.Equal ("has", wrappedLines [4].ToString ());
+			Assert.Equal ("wor", wrappedLines [5].ToString ());
+			Assert.Equal ("ds.", wrappedLines [6].ToString ());
 
 			maxWidth = 2;
 			expectedClippedWidth = 2;
@@ -1691,10 +1687,10 @@ namespace Terminal.Gui {
 			//Assert.True (wrappedLines.Count == );
 			Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
 			Assert.Equal ("A", wrappedLines [0].ToString ());
-			Assert.Equal ("s", wrappedLines [1].ToString ());
-			Assert.Equal ("en", wrappedLines [2].ToString ());
-			Assert.Equal ("te", wrappedLines [3].ToString ());
-			Assert.Equal (".", wrappedLines [^1].ToString ());
+			Assert.Equal ("se", wrappedLines [1].ToString ());
+			Assert.Equal ("nt", wrappedLines [2].ToString ());
+			Assert.Equal ("en", wrappedLines [3].ToString ());
+			Assert.Equal ("s.", wrappedLines [^1].ToString ());
 
 			maxWidth = 1;
 			expectedClippedWidth = 1;
@@ -1702,12 +1698,9 @@ namespace Terminal.Gui {
 			//Assert.True (wrappedLines.Count == );
 			Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
 			Assert.Equal ("A", wrappedLines [0].ToString ());
-			// BUGBUG: WordWrap breaks down with a width of one. It should not
-			// provide blank lines like it does.
-			Assert.Equal ("", wrappedLines [1].ToString ());
-			Assert.Equal ("s", wrappedLines [2].ToString ());
-			Assert.Equal ("e", wrappedLines [3].ToString ());
-			Assert.Equal ("n", wrappedLines [4].ToString ());
+			Assert.Equal ("s", wrappedLines [1].ToString ());
+			Assert.Equal ("e", wrappedLines [2].ToString ());
+			Assert.Equal ("n", wrappedLines [3].ToString ());
 			Assert.Equal (".", wrappedLines [^1].ToString ());
 
 		}