Pārlūkot izejas kodu

Merge pull request #487 from BDisp/text-view-close-file

Text view close file
Charlie Kindel 5 gadi atpakaļ
vecāks
revīzija
d70ad5deac
1 mainītis faili ar 28 papildinājumiem un 0 dzēšanām
  1. 28 0
      Terminal.Gui/Views/TextView.cs

+ 28 - 0
Terminal.Gui/Views/TextView.cs

@@ -39,6 +39,7 @@ namespace Terminal.Gui {
 			if (file == null)
 			if (file == null)
 				throw new ArgumentNullException (nameof (file));
 				throw new ArgumentNullException (nameof (file));
 			try {
 			try {
+				FilePath = file;
 				var stream = File.OpenRead (file);
 				var stream = File.OpenRead (file);
 			} catch {
 			} catch {
 				return false;
 				return false;
@@ -47,6 +48,19 @@ namespace Terminal.Gui {
 			return true;
 			return true;
 		}
 		}
 
 
+		public bool CloseFile ()
+		{
+			if (FilePath == null)
+				throw new ArgumentNullException (nameof (FilePath));
+			try {
+				FilePath = null;
+				lines = new List<List<Rune>> ();
+			} catch {
+				return false;
+			}
+			return true;
+		}
+
 		// Turns the ustring into runes, this does not split the 
 		// Turns the ustring into runes, this does not split the 
 		// contents on a newline if it is present.
 		// contents on a newline if it is present.
 		internal static List<Rune> ToRunes (ustring str)
 		internal static List<Rune> ToRunes (ustring str)
@@ -120,6 +134,8 @@ namespace Terminal.Gui {
 			return sb.ToString ();
 			return sb.ToString ();
 		}
 		}
 
 
+		public string FilePath { get; set; }
+
 		/// <summary>
 		/// <summary>
 		/// The number of text lines in the model
 		/// The number of text lines in the model
 		/// </summary>
 		/// </summary>
@@ -351,6 +367,18 @@ namespace Terminal.Gui {
 			SetNeedsDisplay ();
 			SetNeedsDisplay ();
 		}
 		}
 
 
+		/// <summary>
+		/// Closes the contents of the stream into the TextView.
+		/// </summary>
+		/// <returns><c>true</c>, if stream was closed, <c>false</c> otherwise.</returns>
+		public bool CloseFile()
+		{
+			ResetPosition ();
+			var res = model.CloseFile ();
+			SetNeedsDisplay ();
+			return res;
+		}
+
 		/// <summary>
 		/// <summary>
 		///    The current cursor row.
 		///    The current cursor row.
 		/// </summary>
 		/// </summary>