Ruler.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 
  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="driver">Optional Driver. If not provided, driver will be used.</param>
  19. /// <param name="location">The location to start drawing the ruler, in screen-relative coordinates.</param>
  20. /// <param name="start">The start value of the ruler.</param>
  21. public void Draw (IDriver? driver, Point location, int start = 0)
  22. {
  23. ArgumentNullException.ThrowIfNull (driver);
  24. if (start < 0)
  25. {
  26. throw new ArgumentException ("start must be greater than or equal to 0");
  27. }
  28. if (Length < 1)
  29. {
  30. return;
  31. }
  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. }