Browse Source

initial commit

Tigger Kindel 2 years ago
parent
commit
86fc997abc

+ 0 - 17
Terminal.Gui/Core/Thickness.cs

@@ -281,21 +281,4 @@ namespace Terminal.Gui {
 			return !(left == right);
 		}
 	}
-
-	internal static class StringExtensions {
-		public static string Repeat (this string instr, int n)
-		{
-			if (n <= 0) {
-				return null;
-			}
-
-			if (string.IsNullOrEmpty (instr) || n == 1) {
-				return instr;
-			}
-
-			return new StringBuilder (instr.Length * n)
-				.Insert (0, instr, n)
-				.ToString ();
-		}
-	}
 }

+ 83 - 0
Terminal.Gui/Drawing/Ruler.cs

@@ -0,0 +1,83 @@
+using NStack;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Text;
+using System.Text.Json.Serialization;
+using Terminal.Gui.Configuration;
+using Terminal.Gui.Graphs;
+
+namespace Terminal.Gui {
+	/// <summary>
+	/// Draws a ruler on the screen.
+	/// </summary>
+	/// <remarks>
+	/// <para>
+	/// </para>
+	/// </remarks>
+	public class Ruler {
+
+		/// <summary>
+		/// Gets or sets whether the ruler is drawn horizontally or vertically. The default is horizontally.
+		/// </summary>
+		public Orientation Orientation { get; set; }
+
+		/// <summary>
+		/// Gets or sets the lenght of the ruler. The default is 0.
+		/// </summary>
+		public int Length { get; set; }
+
+		/// <summary>
+		/// Gets or sets the foreground and backgrond color to use.
+		/// </summary>
+		public Attribute Attribute { get; set; }
+
+		/// <summary>
+		/// Gets or sets the ruler template. This will be repeated in the ruler. The default is "0123456789".
+		/// </summary>
+		public string Template { get; set; } = "0123456789";
+
+
+		/// <summary>
+		/// Draws the <see cref="Ruler"/>. 
+		/// </summary>
+		/// <param name="location">The location to start drawing the ruler, in screen-relative coordinates.</param>
+		public void Draw (Point location)
+		{
+			if (Length < 1) {
+				return;
+			}
+
+			if (Orientation == Orientation.Horizontal) {
+				var hrule = Template.Repeat ((int)Math.Ceiling ((double)Length / (double)Template.Length)) [0..Length];
+				// Top
+				Application.Driver.Move (location.X, location.Y);
+				Application.Driver.AddStr (hrule);
+
+			} else {
+				var vrule = Template.Repeat ((int)Math.Ceiling ((double)(Length * 2) / (double)Template.Length)) [0..(Length * 2)];
+				for (var r = location.Y; r < location.Y + Length; r++) {
+					Application.Driver.Move (location.X, r);
+					Application.Driver.AddRune (vrule [r - location.Y]);
+				}
+			}
+		}
+	}
+
+	internal static class StringExtensions {
+		public static string Repeat (this string instr, int n)
+		{
+			if (n <= 0) {
+				return null;
+			}
+
+			if (string.IsNullOrEmpty (instr) || n == 1) {
+				return instr;
+			}
+
+			return new StringBuilder (instr.Length * n)
+				.Insert (0, instr, n)
+				.ToString ();
+		}
+	}
+}

+ 10 - 0
UnitTests/Application/ApplicationTests.cs

@@ -461,6 +461,16 @@ namespace Terminal.Gui.ApplicationTests {
 		}
 		#endregion
 
+		[Fact, AutoInitShutdown]
+		public void Begin_Sets_Application_Top_To_Console_Size()
+		{
+			Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
+
+			((FakeDriver)Application.Driver).SetBufferSize (5, 5);
+			Application.Begin (Application.Top);
+			Assert.Equal (new Rect (0, 0, 5, 5), Application.Top.Frame);
+		}
+
 		[Fact]
 		[AutoInitShutdown]
 		public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()

+ 160 - 0
UnitTests/Drawing/RulerTests.cs

@@ -0,0 +1,160 @@
+using Terminal.Gui;
+using NStack;
+using System;
+using System.Collections.Generic;
+using System.Xml.Linq;
+using Terminal.Gui.Graphs;
+using Xunit;
+using Xunit.Abstractions;
+//using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
+
+// Alias Console to MockConsole so we don't accidentally use Console
+using Console = Terminal.Gui.FakeConsole;
+
+namespace Terminal.Gui.DrawingTests {
+	public class RulerTests {
+
+		readonly ITestOutputHelper output;
+
+		public RulerTests (ITestOutputHelper output)
+		{
+			this.output = output;
+		}
+
+		[Fact ()]
+		public void Constructor_Defaults ()
+		{
+			var r = new Ruler ();
+			Assert.Equal (0, r.Length);
+			Assert.Equal ("0123456789", r.Template);
+			Assert.Equal (Orientation.Horizontal, r.Orientation);
+			Assert.Equal (default, r.Attribute);
+		}
+
+
+		[Fact ()]
+		public void Orientation_set ()
+		{
+			var r = new Ruler ();
+			Assert.Equal (Orientation.Horizontal, r.Orientation);
+			r.Orientation = Orientation.Vertical;
+			Assert.Equal (Orientation.Vertical, r.Orientation);
+		}
+		
+		[Fact ()]
+		public void Length_set ()
+		{
+			var r = new Ruler ();
+			Assert.Equal (0, r.Length);
+			r.Length = 42;
+			Assert.Equal (42, r.Length);
+		}
+
+		[Fact ()]
+		public void Template_set ()
+		{
+			var newTemplate = ")!@#$$%^&*(";
+
+			var r = new Ruler ();
+			Assert.Equal ("0123456789", r.Template);
+			r.Template = newTemplate;
+			Assert.Equal (newTemplate, r.Template);
+		}
+
+		[Fact ()]
+		public void Attribute_set ()
+		{
+			var newAttribute = new Attribute (Color.Red, Color.Green);
+
+			var r = new Ruler ();
+			Assert.Equal (default, r.Attribute);
+			r.Attribute = newAttribute;
+			Assert.Equal (newAttribute, r.Attribute);
+		}
+
+		[Fact (), AutoInitShutdown]
+		public void Draw_Default ()
+		{
+			((FakeDriver)Application.Driver).SetBufferSize (25, 25);
+
+			var r = new Ruler ();
+			r.Draw (new Point (0, 0));
+			TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
+		}
+
+		[Fact (), AutoInitShutdown]
+		public void Draw_Horizontal ()
+		{
+			var len = 15;
+			((FakeDriver)Application.Driver).SetBufferSize (len + 5, 5);
+
+			// Add a frame so we can see the ruler
+			var f = new FrameView () {
+				X = 0,
+				Y = 0,
+				Width = Dim.Fill (),
+				Height = Dim.Fill (),
+			};
+			Application.Top.Add (f);
+			Application.Begin (Application.Top);
+			Assert.Equal (new Rect (0, 0, len + 5, 5), f.Frame);
+			
+			var r = new Ruler ();
+			Assert.Equal (Orientation.Horizontal, r.Orientation);
+
+			r.Length = len;
+			r.Draw (new Point(0,0));
+			TestHelpers.AssertDriverContentsWithFrameAre (@"012345678901234", output);
+
+			// Postive offset
+			((FakeDriver)Application.Driver).FillRect (new Rect (0, 0, len * 2, len * 2), ' ');
+			r.Draw (new Point (1, 1));
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+
+012345678901234", output);
+		}
+
+		[Fact (), AutoInitShutdown]
+		public void Draw_Vertical ()
+		{
+			var len = 25;
+			((FakeDriver)Application.Driver).SetBufferSize (len * 2, len * 2);
+
+			var r = new Ruler ();
+			r.Orientation = Orientation.Vertical;
+			Assert.Equal (Orientation.Vertical, r.Orientation);
+
+			r.Length = len;
+			r.Draw (new Point (0, 0));
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+0
+1
+2
+3
+4
+5
+6
+7
+8
+9
+0
+1
+2
+3
+4
+5
+6
+7
+8
+9
+0
+1
+2
+3
+4", output);
+		}
+		
+	}
+}
+
+

+ 0 - 1
UnitTests/TopLevels/ToplevelTests.cs

@@ -39,7 +39,6 @@ namespace Terminal.Gui.TopLevelTests {
 			Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
 		}
 
-
 		[Fact]
 		[AutoInitShutdown]
 		public void Application_Top_EnsureVisibleBounds_To_Driver_Rows_And_Cols ()

+ 64 - 1
UnitTests/Views/FrameViewTests.cs

@@ -1,12 +1,21 @@
-using System;
+using Microsoft.VisualStudio.TestPlatform.Utilities;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Xunit;
+using Xunit.Abstractions;
 
 namespace Terminal.Gui.ViewTests {
 	public class FrameViewTests {
+		readonly ITestOutputHelper output;
+
+		public FrameViewTests (ITestOutputHelper output)
+		{
+			this.output = output;
+		}
+
 		[Fact]
 		public void Constuctors_Defaults ()
 		{
@@ -28,5 +37,59 @@ namespace Terminal.Gui.ViewTests {
 			fv.EndInit ();
 			Assert.Equal (new Rect (1, 2, 10, 20), fv.Frame);
 		}
+
+		[Fact, AutoInitShutdown]
+		public void Draw_Defaults ()
+		{
+			((FakeDriver)Application.Driver).SetBufferSize (10, 10);
+			var fv = new FrameView ();
+			Assert.Equal (string.Empty, fv.Title);
+			Assert.Equal (string.Empty, fv.Text);
+			Assert.NotNull (fv.Border);
+			Application.Top.Add (fv);
+			Application.Begin (Application.Top);
+			Assert.Equal (new Rect (0, 0, 0, 0), fv.Frame);
+			TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
+
+			fv.Height = 5;
+			fv.Width = 5;
+			Assert.Equal (new Rect (0, 0, 5, 5), fv.Frame);
+			Application.Refresh ();
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+┌───┐
+│   │
+│   │
+│   │
+└───┘", output);
+
+
+			fv.X = 1;
+			fv.Y = 2;
+			Assert.Equal (new Rect (1, 2, 5, 5), fv.Frame);
+			Application.Refresh ();
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+ ┌───┐
+ │   │
+ │   │
+ │   │
+ └───┘", output);
+
+			fv.X = -1;
+			fv.Y = -2;
+			Assert.Equal (new Rect (-1, -2, 5, 5), fv.Frame);
+			Application.Refresh ();
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+   │
+   │
+───┘", output);
+
+			fv.X = 7;
+			fv.Y = 8;
+			Assert.Equal (new Rect (7, 8, 5, 5), fv.Frame);
+			Application.Refresh ();
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+       ┌──
+       │  ", output);
+		}
 	}
 }