From 86fc997abc244e5c29c89ebdcd48636250886294 Mon Sep 17 00:00:00 2001 From: Tigger Kindel Date: Tue, 4 Apr 2023 12:26:14 -0600 Subject: [PATCH] initial commit --- Terminal.Gui/Core/Thickness.cs | 17 --- Terminal.Gui/Drawing/Ruler.cs | 83 +++++++++++ UnitTests/Application/ApplicationTests.cs | 10 ++ UnitTests/Drawing/RulerTests.cs | 160 ++++++++++++++++++++++ UnitTests/TopLevels/ToplevelTests.cs | 1 - UnitTests/Views/FrameViewTests.cs | 65 ++++++++- 6 files changed, 317 insertions(+), 19 deletions(-) create mode 100644 Terminal.Gui/Drawing/Ruler.cs create mode 100644 UnitTests/Drawing/RulerTests.cs diff --git a/Terminal.Gui/Core/Thickness.cs b/Terminal.Gui/Core/Thickness.cs index 81f8a010a..f37e22e18 100644 --- a/Terminal.Gui/Core/Thickness.cs +++ b/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 (); - } - } } \ No newline at end of file diff --git a/Terminal.Gui/Drawing/Ruler.cs b/Terminal.Gui/Drawing/Ruler.cs new file mode 100644 index 000000000..aa0c82422 --- /dev/null +++ b/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 { + /// + /// Draws a ruler on the screen. + /// + /// + /// + /// + /// + public class Ruler { + + /// + /// Gets or sets whether the ruler is drawn horizontally or vertically. The default is horizontally. + /// + public Orientation Orientation { get; set; } + + /// + /// Gets or sets the lenght of the ruler. The default is 0. + /// + public int Length { get; set; } + + /// + /// Gets or sets the foreground and backgrond color to use. + /// + public Attribute Attribute { get; set; } + + /// + /// Gets or sets the ruler template. This will be repeated in the ruler. The default is "0123456789". + /// + public string Template { get; set; } = "0123456789"; + + + /// + /// Draws the . + /// + /// The location to start drawing the ruler, in screen-relative coordinates. + 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 (); + } + } +} \ No newline at end of file diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index 621750fae..607e4f2ef 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/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 () diff --git a/UnitTests/Drawing/RulerTests.cs b/UnitTests/Drawing/RulerTests.cs new file mode 100644 index 000000000..744a381b9 --- /dev/null +++ b/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); + } + + } +} + + diff --git a/UnitTests/TopLevels/ToplevelTests.cs b/UnitTests/TopLevels/ToplevelTests.cs index df9cec066..55fc97847 100644 --- a/UnitTests/TopLevels/ToplevelTests.cs +++ b/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 () diff --git a/UnitTests/Views/FrameViewTests.cs b/UnitTests/Views/FrameViewTests.cs index 42e484db3..514abf79f 100644 --- a/UnitTests/Views/FrameViewTests.cs +++ b/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); + } } }