Ruler.cs 1.9 KB

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