using NStack; using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Text.Json.Serialization; 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; } string _hTemplate { get; set; } = "|123456789"; string _vTemplate { get; set; } = "-123456789"; /// /// Draws the . /// /// The location to start drawing the ruler, in screen-relative coordinates. /// The start value of the ruler. 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 = _hTemplate.Repeat ((int)Math.Ceiling ((double)Length + 2 / (double)_hTemplate.Length)) [start..(Length + start)]; // Top Application.Driver.Move (location.X, location.Y); Application.Driver.AddStr (hrule); } else { var vrule = _vTemplate.Repeat ((int)Math.Ceiling ((double)(Length + 2) / (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]); } } } } 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 (); } } }