123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using NStack;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text;
- using System.Text.Json.Serialization;
- 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; }
- 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>
- /// <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 = _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 ();
- }
- }
- }
|