Charlie Kindel 2 лет назад
Родитель
Сommit
ab7d667217

+ 1 - 0
Terminal.Gui/Terminal.Gui.csproj

@@ -15,6 +15,7 @@
     <InformationalVersion>1.9</InformationalVersion>
   </PropertyGroup>
   <ItemGroup>
+    <PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
     <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
     <PackageReference Include="NStack.Core" Version="1.0.7" />
     <InternalsVisibleTo Include="UnitTests" />

+ 4 - 1
Terminal.Gui/Views/TextField.cs

@@ -235,7 +235,10 @@ namespace Terminal.Gui {
 
 		private void HistoryText_ChangeText (HistoryText.HistoryTextItem obj)
 		{
-			Text = ustring.Make (obj.Lines [obj.CursorPosition.Y]);
+			if (obj == null)
+				return;
+
+			Text = ustring.Make (obj?.Lines [obj.CursorPosition.Y]);
 			CursorPosition = obj.CursorPosition.X;
 			Adjust ();
 		}

+ 5 - 5
Terminal.Gui/Views/TextView.cs

@@ -851,7 +851,7 @@ namespace Terminal.Gui {
 			var firstLine = wrappedModelLines.IndexOf (r => r.ModelLine == modelLine);
 			int modelCol = 0;
 
-			for (int i = firstLine; i <= line; i++) {
+			for (int i = firstLine; i <= Math.Min (line, wrappedModelLines.Count - 1); i++) {
 				var wLine = wrappedModelLines [i];
 
 				if (i < line) {
@@ -1165,7 +1165,7 @@ namespace Terminal.Gui {
 		/// Unlike the <see cref="TextChanged"/> event, this event is raised whenever the user types or
 		/// otherwise changes the contents of the <see cref="TextView"/>.
 		/// </remarks>
-		public Action<ContentsChangedEventArgs> ContentsChanged;
+		public event Action<ContentsChangedEventArgs> ContentsChanged;
 
 		/// <summary>
 		/// Invoked with the unwrapped <see cref="CursorPosition"/>.
@@ -1432,7 +1432,7 @@ namespace Terminal.Gui {
 			}
 
 			UpdateWrapModel ();
-			
+
 			Adjust ();
 			OnContentsChanged ();
 		}
@@ -1830,6 +1830,7 @@ namespace Terminal.Gui {
 			try {
 				SetWrapModel ();
 				res = model.LoadFile (path);
+				historyText.Clear (Text);
 				ResetPosition ();
 			} catch (Exception) {
 				throw;
@@ -1837,7 +1838,6 @@ namespace Terminal.Gui {
 				UpdateWrapModel ();
 				SetNeedsDisplay ();
 				Adjust ();
-				OnContentsChanged ();
 			}
 			return res;
 		}
@@ -1850,9 +1850,9 @@ namespace Terminal.Gui {
 		public void LoadStream (Stream stream)
 		{
 			model.LoadStream (stream);
+			historyText.Clear (Text);
 			ResetPosition ();
 			SetNeedsDisplay ();
-			OnContentsChanged ();
 		}
 
 		/// <summary>

+ 3 - 6
UICatalog/UICatalog.cs

@@ -178,7 +178,7 @@ namespace UICatalog {
 							"About UI Catalog", () =>  MessageBox.Query ("About UI Catalog", _aboutMessage.ToString(), "_Ok"), null, null, Key.CtrlMask | Key.A),
 					}),
 				});
-
+				
 				Capslock = new StatusItem (Key.CharMask, "Caps", null);
 				Numlock = new StatusItem (Key.CharMask, "Num", null);
 				Scrolllock = new StatusItem (Key.CharMask, "Scroll", null);
@@ -189,9 +189,6 @@ namespace UICatalog {
 					Visible = true,
 				};
 				StatusBar.Items = new StatusItem [] {
-					Capslock,
-					Numlock,
-					Scrolllock,
 					new StatusItem(Key.Q | Key.CtrlMask, "~CTRL-Q~ Quit", () => {
 						if (_selectedScenario is null){
 							// This causes GetScenarioToRun to return null
@@ -201,7 +198,7 @@ namespace UICatalog {
 							_selectedScenario.RequestStop();
 						}
 					}),
-					new StatusItem(Key.F10, "~F10~ Hide/Show Status Bar", () => {
+					new StatusItem(Key.F10, "~F10~ Status Bar", () => {
 						StatusBar.Visible = !StatusBar.Visible;
 						LeftPane.Height = Dim.Fill(StatusBar.Visible ? 1 : 0);
 						RightPane.Height = Dim.Fill(StatusBar.Visible ? 1 : 0);
@@ -284,7 +281,7 @@ namespace UICatalog {
 				miIsMouseDisabled.Checked = Application.IsMouseDisabled;
 				miHeightAsBuffer.Checked = Application.HeightAsBuffer;
 				DriverName.Title = $"Driver: {Driver.GetType ().Name}";
-				OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem}";
+				OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem} {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion}";
 
 				if (_selectedScenario != null) {
 					_selectedScenario = null;

+ 1 - 1
UICatalog/UICatalog.csproj

@@ -19,7 +19,7 @@
     <DefineConstants>TRACE;DEBUG_IDISPOSABLE</DefineConstants>
   </PropertyGroup>
   <ItemGroup>
-    <PackageReference Include="CsvHelper" Version="30.0.0" />
+    <PackageReference Include="CsvHelper" Version="30.0.1" />
     <PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
   </ItemGroup>
   <ItemGroup>

+ 15 - 0
UnitTests/TextFieldTests.cs

@@ -1281,5 +1281,20 @@ namespace Terminal.Gui.Views {
 			Assert.Equal (0, tf.ScrollOffset);
 			Assert.Equal (16, tf.CursorPosition);
 		}
+
+		[Fact]
+		public void HistoryText_IsDirty_ClearHistoryChanges ()
+		{
+			var text = "Testing";
+			var tf = new TextField (text);
+
+			Assert.Equal (text, tf.Text);
+			tf.ClearHistoryChanges ();
+			Assert.False (tf.IsDirty);
+
+			Assert.True (tf.ProcessKey (new KeyEvent (Key.A, new KeyModifiers ())));
+			Assert.Equal ($"{text}A", tf.Text);
+			Assert.True (tf.IsDirty);
+		}
 	}
 }

+ 87 - 40
UnitTests/TextViewTests.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.Diagnostics.Tracing;
 using System.Linq;
 using System.Reflection;
 using System.Text.RegularExpressions;
@@ -1926,6 +1925,51 @@ namespace Terminal.Gui.Views {
 			Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}", tv.Text);
 		}
 
+		[Fact]
+		public void LoadStream_IsDirty ()
+		{
+			var text = "Testing";
+			using (System.IO.MemoryStream stream = new System.IO.MemoryStream ()) {
+
+				var writer = new System.IO.StreamWriter (stream);
+				writer.Write (text);
+				writer.Flush ();
+				stream.Position = 0;
+
+				var tv = new TextView ();
+				tv.LoadStream (stream);
+
+				Assert.Equal (7, text.Length);
+				Assert.Equal (text.Length, tv.Text.Length);
+				Assert.Equal (text, tv.Text);
+				Assert.False (tv.IsDirty);
+			}
+		}
+
+		[Fact]
+		public void LoadStream_IsDirty_With_Null_On_The_Text ()
+		{
+			var text = "Test\0ing";
+			using (System.IO.MemoryStream stream = new System.IO.MemoryStream ()) {
+
+				var writer = new System.IO.StreamWriter (stream);
+				writer.Write (text);
+				writer.Flush ();
+				stream.Position = 0;
+
+				var tv = new TextView ();
+				tv.LoadStream (stream);
+
+				Assert.Equal (8, text.Length);
+				Assert.Equal (text.Length, tv.Text.Length);
+				Assert.Equal (8, text.Length);
+				Assert.Equal (8, tv.Text.Length);
+				Assert.Equal (text, tv.Text);
+				Assert.False (tv.IsDirty);
+				Assert.Equal ('\u2400', ConsoleDriver.MakePrintable ((Rune)tv.Text [4]));
+			}
+		}
+
 		[Fact]
 		public void StringToRunes_Slipts_CRLF ()
 		{
@@ -6426,16 +6470,17 @@ This is the second line.
 		[Fact, AutoInitShutdown]
 		public void ContentsChanged_Event_NoFires_On_CursorPosition ()
 		{
+			var eventcount = 0;
+
 			var tv = new TextView {
 				Width = 50,
 				Height = 10,
 			};
 
-			var eventcount = 0;
-			Assert.Null (tv.ContentsChanged);
 			tv.ContentsChanged += (e) => {
 				eventcount++;
 			};
+			Assert.Equal (0, eventcount);
 
 			tv.CursorPosition = new Point (0, 0);
 
@@ -6445,19 +6490,19 @@ This is the second line.
 		[Fact, AutoInitShutdown]
 		public void ContentsChanged_Event_Fires_On_InsertText ()
 		{
+			var eventcount = 0;
+
 			var tv = new TextView {
 				Width = 50,
 				Height = 10,
 			};
 			tv.CursorPosition = new Point (0, 0);
 
-			var eventcount = 0;
-
-			Assert.Null (tv.ContentsChanged);
 			tv.ContentsChanged += (e) => {
 				eventcount++;
 			};
 
+			Assert.Equal (0, eventcount);
 
 			tv.InsertText ("a");
 			Assert.Equal (1, eventcount);
@@ -6465,7 +6510,7 @@ This is the second line.
 			tv.CursorPosition = new Point (0, 0);
 			tv.InsertText ("bcd");
 			Assert.Equal (4, eventcount);
-			
+
 			tv.InsertText ("e");
 			Assert.Equal (5, eventcount);
 
@@ -6490,11 +6535,11 @@ This is the second line.
 			var tv = new TextView {
 				Width = 50,
 				Height = 10,
-				ContentsChanged = (e) => {
-					eventcount++;
-					Assert.Equal (expectedRow, e.Row);
-					Assert.Equal (expectedCol, e.Col);
-				}
+			};
+			tv.ContentsChanged += (e) => {
+				eventcount++;
+				Assert.Equal (expectedRow, e.Row);
+				Assert.Equal (expectedCol, e.Col);
 			};
 
 			Application.Top.Add (tv);
@@ -6519,12 +6564,13 @@ This is the second line.
 				// you'd think col would be 3, but it's 0 because TextView sets
 				// row/col = 0 when you set Text
 				Text = "abc",
-				ContentsChanged = (e) => {
-					eventcount++;
-					Assert.Equal (expectedRow, e.Row);
-					Assert.Equal (expectedCol, e.Col);
-				}
 			};
+			tv.ContentsChanged += (e) => {
+				eventcount++;
+				Assert.Equal (expectedRow, e.Row);
+				Assert.Equal (expectedCol, e.Col);
+			};
+
 			Assert.Equal ("abc", tv.Text);
 
 			Application.Top.Add (tv);
@@ -6550,11 +6596,11 @@ This is the second line.
 			var tv = new TextView {
 				Width = 50,
 				Height = 10,
-				ContentsChanged = (e) => {
-					eventcount++;
-					Assert.Equal (expectedRow, e.Row);
-					Assert.Equal (expectedCol, e.Col);
-				}
+			};
+			tv.ContentsChanged += (e) => {
+				eventcount++;
+				Assert.Equal (expectedRow, e.Row);
+				Assert.Equal (expectedCol, e.Col);
 			};
 
 			Application.Top.Add (tv);
@@ -6576,7 +6622,7 @@ This is the second line.
 		{
 			var eventcount = 0;
 
-			_textView.ContentsChanged = (e) => {
+			_textView.ContentsChanged += (e) => {
 				eventcount++;
 			};
 
@@ -6603,7 +6649,7 @@ This is the second line.
 		{
 			var eventcount = 0;
 
-			_textView.ContentsChanged = (e) => {
+			_textView.ContentsChanged += (e) => {
 				eventcount++;
 			};
 
@@ -6652,7 +6698,7 @@ This is the second line.
 			expectedEventCount += 4;
 			Copy_Without_Selection ();
 			Assert.Equal (expectedEventCount, eventcount);
-			
+
 			// reset
 			expectedEventCount += 1;
 			_textView.Text = TextViewTestsAutoInitShutdown.txt;
@@ -6669,7 +6715,7 @@ This is the second line.
 			var eventcount = 0;
 			var expectedEventCount = 0;
 
-			_textView.ContentsChanged = (e) => {
+			_textView.ContentsChanged += (e) => {
 				eventcount++;
 			};
 
@@ -6712,9 +6758,9 @@ This is the second line.
 				Width = 50,
 				Height = 10,
 				Text = text,
-				ContentsChanged = (e) => {
-					eventcount++;
-				}
+			};
+			tv.ContentsChanged += (e) => {
+				eventcount++;
 			};
 
 			Assert.True (tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
@@ -6723,46 +6769,47 @@ This is the second line.
 
 			var expectedEventCount = 1; // for ENTER key
 			Assert.Equal (expectedEventCount, eventcount);
-			
+
 			tv.ClearHistoryChanges ();
 			expectedEventCount = 2;
 			Assert.Equal (expectedEventCount, eventcount);
 		}
 
 		[Fact]
-		public void ContentsChanged_Event_Fires_LoadStream ()
+		public void ContentsChanged_Event_Fires_LoadStream_By_Calling_HistoryText_Clear ()
 		{
 			var eventcount = 0;
 
 			var tv = new TextView {
 				Width = 50,
 				Height = 10,
-				ContentsChanged = (e) => {
-					eventcount++;
-				}
+			};
+			tv.ContentsChanged += (e) => {
+				eventcount++;
 			};
 
 			var text = "This is the first line.\r\nThis is the second line.\r\n";
 			tv.LoadStream (new System.IO.MemoryStream (System.Text.Encoding.ASCII.GetBytes (text)));
 			Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}", tv.Text);
-			
+
 			Assert.Equal (1, eventcount);
 		}
 
 		[Fact]
-		public void ContentsChanged_Event_Fires_LoadFile ()
+		public void ContentsChanged_Event_Fires_On_LoadFile_By_Calling_HistoryText_Clear ()
 		{
 			var eventcount = 0;
 
 			var tv = new TextView {
 				Width = 50,
 				Height = 10,
-				ContentsChanged = (e) => {
-					eventcount++;
-				}
 			};
+			tv.ContentsChanged += (e) => {
+				eventcount++;
+			};
+
 			var fileName = "textview.txt";
-			System.IO.File.WriteAllText (fileName, "This is the first line.\r\nThis is the second line.\r\n") ;
+			System.IO.File.WriteAllText (fileName, "This is the first line.\r\nThis is the second line.\r\n");
 
 			tv.LoadFile (fileName);
 			Assert.Equal (1, eventcount);

+ 1 - 1
UnitTests/UnitTests.csproj

@@ -21,7 +21,7 @@
     <DefineConstants>TRACE;DEBUG_IDISPOSABLE</DefineConstants>
   </PropertyGroup>
   <ItemGroup>
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
     <PackageReference Include="ReportGenerator" Version="5.1.12" />
     <PackageReference Include="System.Collections" Version="4.3.0" />
     <PackageReference Include="xunit" Version="2.4.2" />