Ruler.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #nullable enable
  2. namespace Terminal.Gui;
  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. public void Draw (Point location, int start = 0)
  21. {
  22. if (start < 0)
  23. {
  24. throw new ArgumentException ("start must be greater than or equal to 0");
  25. }
  26. if (Length < 1)
  27. {
  28. return;
  29. }
  30. if (Orientation == Orientation.Horizontal)
  31. {
  32. string hrule =
  33. _hTemplate.Repeat ((int)Math.Ceiling (Length + 2 / (double)_hTemplate.Length))! [start..(Length + start)];
  34. // Top
  35. Application.Driver?.Move (location.X, location.Y);
  36. Application.Driver?.AddStr (hrule);
  37. }
  38. else
  39. {
  40. string vrule =
  41. _vTemplate.Repeat ((int)Math.Ceiling ((Length + 2) / (double)_vTemplate.Length))!
  42. [start..(Length + start)];
  43. for (int r = location.Y; r < location.Y + Length; r++)
  44. {
  45. Application.Driver?.Move (location.X, r);
  46. Application.Driver?.AddRune ((Rune)vrule [r - location.Y]);
  47. }
  48. }
  49. }
  50. }