Ruler.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #nullable enable
  2. namespace Terminal.Gui.Drawing;
  3. /// <summary>Draws a ruler on the screen.</summary>
  4. /// <remarks>
  5. /// <para></para>
  6. /// </remarks>
  7. internal class Ruler
  8. {
  9. /// <summary>Gets or sets the foreground and background color to use.</summary>
  10. public Attribute Attribute { get; set; } = new ();
  11. /// <summary>Gets or sets the length of the ruler. The default is 0.</summary>
  12. public int Length { get; set; }
  13. /// <summary>Gets or sets whether the ruler is drawn horizontally or vertically. The default is horizontally.</summary>
  14. public Orientation Orientation { get; set; }
  15. private string _hTemplate { get; } = "|123456789";
  16. private string _vTemplate { get; } = "-123456789";
  17. /// <summary>Draws the <see cref="Ruler"/>.</summary>
  18. /// <param name="location">The location to start drawing the ruler, in screen-relative coordinates.</param>
  19. /// <param name="start">The start value of the ruler.</param>
  20. /// <param name="driver">Optional Driver. If not provided, driver will be used.</param>
  21. public void Draw (Point location, int start = 0, IDriver? driver = null)
  22. {
  23. if (start < 0)
  24. {
  25. throw new ArgumentException ("start must be greater than or equal to 0");
  26. }
  27. if (Length < 1)
  28. {
  29. return;
  30. }
  31. driver ??= driver;
  32. if (Orientation == Orientation.Horizontal)
  33. {
  34. string hrule =
  35. _hTemplate.Repeat ((int)Math.Ceiling (Length + 2 / (double)_hTemplate.Length))! [start..(Length + start)];
  36. // Top
  37. driver?.Move (location.X, location.Y);
  38. driver?.AddStr (hrule);
  39. }
  40. else
  41. {
  42. string vrule =
  43. _vTemplate.Repeat ((int)Math.Ceiling ((Length + 2) / (double)_vTemplate.Length))!
  44. [start..(Length + start)];
  45. for (int r = location.Y; r < location.Y + Length; r++)
  46. {
  47. driver?.Move (location.X, r);
  48. driver?.AddRune ((Rune)vrule [r - location.Y]);
  49. }
  50. }
  51. }
  52. }