Browse Source

modified api and updated Thickness to use

Tigger Kindel 2 years ago
parent
commit
87d8375723

+ 16 - 20
Terminal.Gui/Core/Thickness.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Text;
 using System.Text.Json.Serialization;
 using Terminal.Gui.Configuration;
+using Terminal.Gui.Graphs;
 
 namespace Terminal.Gui {
 	/// <summary>
@@ -159,16 +160,6 @@ namespace Terminal.Gui {
 				}
 			}
 
-			ustring hrule = ustring.Empty;
-			ustring vrule = ustring.Empty;
-			if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
-
-				string h = "0123456789";
-				hrule = h.Repeat ((int)Math.Ceiling ((double)(rect.Width) / (double)h.Length)) [0..(rect.Width)];
-				string v = "0123456789";
-				vrule = v.Repeat ((int)Math.Ceiling ((double)(rect.Height * 2) / (double)v.Length)) [0..(rect.Height * 2)];
-			};
-
 			// Draw the Top side
 			if (Top > 0) {
 				Application.Driver.FillRect (new Rect (rect.X, rect.Y, rect.Width, Math.Min (rect.Height, Top)), topChar);
@@ -192,20 +183,25 @@ namespace Terminal.Gui {
 			// TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
 			if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
 				// Top
-				Application.Driver.Move (rect.X, rect.Y);
-				Application.Driver.AddStr (hrule);
+				var hruler = new Ruler () { Length = rect.Width, Orientation = Orientation.Horizontal };
+				if (Top > 0) {
+					hruler.Draw (new Point (rect.X, rect.Y));
+				}
+
 				//Left
-				for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
-					Application.Driver.Move (rect.X, r);
-					Application.Driver.AddRune (vrule [r - rect.Y]);
+				var vruler = new Ruler () { Length = rect.Height - 2, Orientation = Orientation.Vertical };
+				if (Left > 0) {
+					vruler.Draw (new Point (rect.X, rect.Y + 1), 1);
 				}
+
 				// Bottom
-				Application.Driver.Move (rect.X, rect.Y + rect.Height - Bottom + 1);
-				Application.Driver.AddStr (hrule);
+				if (Bottom > 0) {
+					hruler.Draw (new Point (rect.X, rect.Y + rect.Height - 1));
+				}
+
 				// Right
-				for (var r = rect.Y + 1; r < rect.Y + rect.Height; r++) {
-					Application.Driver.Move (rect.X + rect.Width - Right + 1, r);
-					Application.Driver.AddRune (vrule [r - rect.Y]);
+				if (Right > 0) {
+					vruler.Draw (new Point (rect.X + rect.Width - 1, rect.Y + 1), 1);
 				}
 			}
 

+ 11 - 8
Terminal.Gui/Drawing/Ruler.cs

@@ -32,30 +32,33 @@ namespace Terminal.Gui {
 		/// </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";
+		string _hTemplate { get; set; } = "|123456789";
+		string _vTemplate { get; set; } = "-123456789";
 
 
 		/// <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)
+		/// <param name="start">The start value of the ruler.</param>
+		public void Draw (Point location, int start = 0)
 		{
+			if (start < 0) {
+				throw new ArgumentException ("start must be greater than or equal to 0");
+			}
+			
 			if (Length < 1) {
 				return;
 			}
-
+			
 			if (Orientation == Orientation.Horizontal) {
-				var hrule = Template.Repeat ((int)Math.Ceiling ((double)Length / (double)Template.Length)) [0..Length];
+				var hrule = _hTemplate.Repeat ((int)Math.Ceiling ((double)Length / (double)_hTemplate.Length)) [start..(Length + start)];
 				// 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)];
+				var vrule = _vTemplate.Repeat ((int)Math.Ceiling ((double)(Length) / (double)_vTemplate.Length)) [start..(Length + start)];
 				for (var r = location.Y; r < location.Y + Length; r++) {
 					Application.Driver.Move (location.X, r);
 					Application.Driver.AddRune (vrule [r - location.Y]);

+ 131 - 0
UnitTests/Core/ThicknessTests.cs

@@ -485,6 +485,137 @@ namespace Terminal.Gui.CoreTests {
 
 		}
 
+		[Fact (), AutoInitShutdown]
+		public void DrawTests_Ruler ()
+		{
+			// 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);
+			
+			((FakeDriver)Application.Driver).SetBufferSize (45, 20);
+			var t = new Thickness (0, 0, 0, 0);
+			var r = new Rect (2, 2, 40, 15);
+			Application.Refresh ();
+			ConsoleDriver.Diagnostics |= ConsoleDriver.DiagnosticFlags.FrameRuler;
+			t.Draw (r, "Test");
+			ConsoleDriver.Diagnostics = ConsoleDriver.DiagnosticFlags.Off;
+			TestHelpers.AssertDriverContentsAre (@"
+┌───────────────────────────────────────────┐
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+└───────────────────────────────────────────┘", output);
+
+
+			t = new Thickness (1, 1, 1, 1);
+			r = new Rect (1, 1, 40, 15);
+			Application.Refresh ();
+			ConsoleDriver.Diagnostics |= ConsoleDriver.DiagnosticFlags.FrameRuler;
+			t.Draw (r, "Test");
+			ConsoleDriver.Diagnostics = ConsoleDriver.DiagnosticFlags.Off;
+			TestHelpers.AssertDriverContentsAre (@"
+┌───────────────────────────────────────────┐
+│|123456789|123456789|123456789|123456789   │
+│1                                      1   │
+│2                                      2   │
+│3                                      3   │
+│4                                      4   │
+│5                                      5   │
+│6                                      6   │
+│7                                      7   │
+│8                                      8   │
+│9                                      9   │
+│-                                      -   │
+│1                                      1   │
+│2                                      2   │
+│3                                      3   │
+│|123456789|123456789|123456789|123456789   │
+│                                           │
+│                                           │
+│                                           │
+└───────────────────────────────────────────┘", output);
+
+			t = new Thickness (1, 2, 3, 4);
+			r = new Rect (2, 2, 40, 15);
+			Application.Refresh ();
+			ConsoleDriver.Diagnostics |= ConsoleDriver.DiagnosticFlags.FrameRuler;
+			t.Draw (r, "Test");
+			ConsoleDriver.Diagnostics = ConsoleDriver.DiagnosticFlags.Off;
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+┌───────────────────────────────────────────┐
+│                                           │
+│ |123456789|123456789|123456789|123456789  │
+│ 1                                      1  │
+│ 2                                      2  │
+│ 3                                      3  │
+│ 4                                      4  │
+│ 5                                      5  │
+│ 6                                      6  │
+│ 7                                      7  │
+│ 8                                      8  │
+│ 9                                      9  │
+│ -                                      -  │
+│ 1                                      1  │
+│ 2                                      2  │
+│ 3                                      3  │
+│ |123456789|123456789|123456789|123456789  │
+│                                           │
+│                                           │
+└───────────────────────────────────────────┘", output);
+
+
+			t = new Thickness (-1, 1, 1, 1);
+			r = new Rect (5, 5, 40, 15);
+			Application.Refresh ();
+			ConsoleDriver.Diagnostics |= ConsoleDriver.DiagnosticFlags.FrameRuler;
+			t.Draw (r, "Test");
+			ConsoleDriver.Diagnostics = ConsoleDriver.DiagnosticFlags.Off;
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+┌───────────────────────────────────────────┐
+│                                           │
+│                                           │
+│                                           │
+│                                           │
+│    |123456789|123456789|123456789|123456789
+│                                           1
+│                                           2
+│                                           3
+│                                           4
+│                                           5
+│                                           6
+│                                           7
+│                                           8
+│                                           9
+│                                           -
+│                                           1
+│                                           2
+│                                           3
+└────|123456789|123456789|123456789|123456789", output);
+
+		}
 		[Fact ()]
 		public void EqualsTest ()
 		{

+ 71 - 55
UnitTests/Drawing/RulerTests.cs

@@ -26,7 +26,6 @@ namespace Terminal.Gui.DrawingTests {
 		{
 			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);
 		}
@@ -40,7 +39,7 @@ namespace Terminal.Gui.DrawingTests {
 			r.Orientation = Orientation.Vertical;
 			Assert.Equal (Orientation.Vertical, r.Orientation);
 		}
-		
+
 		[Fact ()]
 		public void Length_set ()
 		{
@@ -50,17 +49,6 @@ namespace Terminal.Gui.DrawingTests {
 			Assert.Equal (42, r.Length);
 		}
 
-		[Fact ()]
-		public void Template_set ()
-		{
-			var newTemplate = "|123456789";
-
-			var r = new Ruler ();
-			Assert.Equal ("0123456789", r.Template);
-			r.Template = newTemplate;
-			Assert.Equal (newTemplate, r.Template);
-		}
-
 		[Fact ()]
 		public void Attribute_set ()
 		{
@@ -98,14 +86,14 @@ namespace Terminal.Gui.DrawingTests {
 			Application.Begin (Application.Top);
 			((FakeDriver)Application.Driver).SetBufferSize (len + 5, 5);
 			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));
+			r.Draw (new Point (0, 0));
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
-012345678901234────┐
+|123456789|1234────┐
 │                  │
 │                  │
 │                  │
@@ -116,7 +104,7 @@ namespace Terminal.Gui.DrawingTests {
 			r.Draw (new Point (1, 1));
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
 ┌──────────────────┐
-│012345678901234   │
+│|123456789|1234   │
 │                  │
 │                  │
 └──────────────────┘", output);
@@ -126,7 +114,7 @@ namespace Terminal.Gui.DrawingTests {
 			r.Draw (new Point (-1, 1));
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
 ┌──────────────────┐
-12345678901234     │
+123456789|1234     │
 │                  │
 │                  │
 └──────────────────┘", output);
@@ -136,14 +124,14 @@ namespace Terminal.Gui.DrawingTests {
 			r.Draw (new Point (10, 1));
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
 ┌──────────────────┐
-│         0123456789
+│         |123456789
 │                  │
 │                  │
 └──────────────────┘", output);
 		}
 
 		[Fact (), AutoInitShutdown]
-		public void Draw_Horizontal_Template ()
+		public void Draw_Horizontal_Start ()
 		{
 			var len = 15;
 
@@ -154,22 +142,28 @@ namespace Terminal.Gui.DrawingTests {
 				Width = Dim.Fill (),
 				Height = Dim.Fill (),
 			};
-
-		
 			Application.Top.Add (f);
 			Application.Begin (Application.Top);
 			((FakeDriver)Application.Driver).SetBufferSize (len + 5, 5);
 			Assert.Equal (new Rect (0, 0, len + 5, 5), f.Frame);
 
 			var r = new Ruler ();
-			r.Length = len;
+			Assert.Equal (Orientation.Horizontal, r.Orientation);
 
-			var newTemplate = "|123456789";
-			r.Template = newTemplate;
+			r.Length = len;
+			r.Draw (new Point (0, 0), 1);
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+123456789|12345────┐
+│                  │
+│                  │
+│                  │
+└──────────────────┘", output);
 
-			r.Draw (new Point (0, 0));
+			Application.Refresh ();
+			r.Length = len;
+			r.Draw (new Point (1, 0), 1);
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
-|123456789|1234────┐
+┌123456789|12345───┐
 │                  │
 │                  │
 │                  │
@@ -200,7 +194,7 @@ namespace Terminal.Gui.DrawingTests {
 			r.Length = len;
 			r.Draw (new Point (0, 0));
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
-0───┐
+-───┐
 1   │
 2   │
 3   │
@@ -210,7 +204,7 @@ namespace Terminal.Gui.DrawingTests {
 7   │
 8   │
 9   │
-0
+-
 1   │
 2   │
 3   │
@@ -226,7 +220,7 @@ namespace Terminal.Gui.DrawingTests {
 			r.Draw (new Point (1, 1));
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
 ┌───┐
-│0
+│-
 │1  │
 │2  │
 │3  │
@@ -236,7 +230,7 @@ namespace Terminal.Gui.DrawingTests {
 │7  │
 │8  │
 │9  │
-│0
+│-
 │1  │
 │2  │
 │3  │
@@ -259,7 +253,7 @@ namespace Terminal.Gui.DrawingTests {
 │7  │
 │8  │
 │9  │
-│0
+│-
 │1  │
 │2  │
 │3  │
@@ -285,7 +279,7 @@ namespace Terminal.Gui.DrawingTests {
 │   │
 │   │
 │   │
-│0
+│-
 │1  │
 │2  │
 │3  │
@@ -298,7 +292,7 @@ namespace Terminal.Gui.DrawingTests {
 		}
 
 		[Fact (), AutoInitShutdown]
-		public void Draw_Vertical_Template ()
+		public void Draw_Vertical_Start ()
 		{
 			var len = 15;
 
@@ -319,33 +313,55 @@ namespace Terminal.Gui.DrawingTests {
 			var r = new Ruler ();
 			r.Orientation = Orientation.Vertical;
 			r.Length = len;
-
-			var newTemplate = ")!@#$$%^&*(";
-			r.Template = newTemplate;
-
-			r.Draw (new Point (0, 0));
+			r.Draw (new Point (0, 0), 1);
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
-)───┐
-!   │
-@   │
-#   │
-$   │
-$   │
-%   │
-^   │
-&   │
-*   │
-(   │
-)   │
-!   │
-@   │
-#   │
+1───┐
+2   │
+3   │
+4   │
+5   │
+6   │
+7   │
+8   │
+9   │
+-   │
+1   │
+2   │
+3   │
+4   │
+5   │
+│   │
 │   │
 │   │
 │   │
+└───┘", output);
+
+			Application.Refresh ();
+			r.Length = len;
+			r.Draw (new Point (0, 1), 1);
+			TestHelpers.AssertDriverContentsWithFrameAre (@"
+┌───┐
+1   │
+2   │
+3   │
+4   │
+5   │
+6   │
+7   │
+8   │
+9   │
+-   │
+1   │
+2   │
+3   │
+4   │
+5   │
+│   │
+│   │
 │   │
 └───┘", output);
-		}		
+
+		}
 	}
 }