Justification.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using static Terminal.Gui.Pos;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Controls how the <see cref="Justifier"/> justifies items within a container.
  5. /// </summary>
  6. public enum Justification
  7. {
  8. /// <summary>
  9. /// The items will be aligned to the left.
  10. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  11. /// each item.
  12. /// </summary>
  13. /// <example>
  14. /// <c>
  15. /// 111 2222 33333
  16. /// </c>
  17. /// </example>
  18. Left,
  19. /// <summary>
  20. /// The items will be aligned to the top.
  21. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
  22. /// each item.
  23. /// </summary>
  24. Top = Left,
  25. /// <summary>
  26. /// The items will be aligned to the right.
  27. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  28. /// each item.
  29. /// </summary>
  30. /// <example>
  31. /// <c>
  32. /// 111 2222 33333
  33. /// </c>
  34. /// </example>
  35. Right,
  36. /// <summary>
  37. /// The items will be aligned to the bottom.
  38. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
  39. /// each item.
  40. /// </summary>
  41. Bottom = Right,
  42. /// <summary>
  43. /// The group will be centered in the container.
  44. /// If centering is not possible, the group will be left-justified.
  45. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  46. /// each item.
  47. /// </summary>
  48. /// <example>
  49. /// <c>
  50. /// 111 2222 33333
  51. /// </c>
  52. /// </example>
  53. Centered,
  54. /// <summary>
  55. /// The items will be justified. Space will be added between the items such that the first item
  56. /// is at the start and the right side of the last item against the end.
  57. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  58. /// each item.
  59. /// </summary>
  60. /// <example>
  61. /// <c>
  62. /// 111 2222 33333
  63. /// </c>
  64. /// </example>
  65. Justified,
  66. /// <summary>
  67. /// The first item will be aligned to the left and the remaining will aligned to the right.
  68. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  69. /// each item.
  70. /// </summary>
  71. /// <example>
  72. /// <c>
  73. /// 111 2222 33333
  74. /// </c>
  75. /// </example>
  76. FirstLeftRestRight,
  77. /// <summary>
  78. /// The first item will be aligned to the top and the remaining will aligned to the bottom.
  79. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
  80. /// each item.
  81. /// </summary>
  82. FirstTopRestBottom = FirstLeftRestRight,
  83. /// <summary>
  84. /// The last item will be aligned to the right and the remaining will aligned to the left.
  85. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  86. /// each item.
  87. /// </summary>
  88. /// <example>
  89. /// <c>
  90. /// 111 2222 33333
  91. /// </c>
  92. /// </example>
  93. LastRightRestLeft,
  94. /// <summary>
  95. /// The last item will be aligned to the bottom and the remaining will aligned to the left.
  96. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
  97. /// each item.
  98. /// </summary>
  99. LastBottomRestTop = LastRightRestLeft,
  100. }
  101. /// <summary>
  102. /// Justifies items within a container based on the specified <see cref="Justification"/>.
  103. /// </summary>
  104. public class Justifier
  105. {
  106. /// <summary>
  107. /// Gets or sets how the <see cref="Justifier"/> justifies items within a container.
  108. /// </summary>
  109. public Justification Justification { get; set; }
  110. /// <summary>
  111. /// The size of the container.
  112. /// </summary>
  113. public int ContainerSize { get; set; }
  114. /// <summary>
  115. /// Gets or sets whether <see cref="Justifier"/> puts a space is placed between items. Default is
  116. /// <see langword="false"/>. If <see langword="true"/>, a space will be
  117. /// placed between each item, which is useful for justifying text.
  118. /// </summary>
  119. public bool PutSpaceBetweenItems { get; set; }
  120. /// <summary>
  121. /// Takes a list of items and returns their positions when justified within a container <see name="ContainerSize"/>
  122. /// wide based on the specified
  123. /// <see cref="Justification"/>.
  124. /// </summary>
  125. /// <param name="sizes">The sizes of the items to justify.</param>
  126. /// <returns>The locations of the items, from left to right.</returns>
  127. public int [] Justify (int [] sizes) { return Justify (Justification, PutSpaceBetweenItems, ContainerSize, sizes); }
  128. /// <summary>
  129. /// Takes a list of items and returns their positions when justified within a container
  130. /// <paramref name="containerSize"/> wide based on the specified
  131. /// <see cref="Justification"/>.
  132. /// </summary>
  133. /// <param name="sizes">The sizes of the items to justify.</param>
  134. /// <param name="justification">The justification style.</param>
  135. /// <param name="putSpaceBetweenItems">Puts a space is placed between items.</param>
  136. /// <param name="containerSize">The size of the container.</param>
  137. /// <returns>The locations of the items, from left to right.</returns>
  138. public static int [] Justify (Justification justification, bool putSpaceBetweenItems, int containerSize, int [] sizes)
  139. {
  140. if (sizes.Length == 0)
  141. {
  142. return new int [] { };
  143. }
  144. int maxSpaceBetweenItems = putSpaceBetweenItems ? 1 : 0;
  145. var positions = new int [sizes.Length]; // positions of the items. the return value.
  146. int totalItemsSize = sizes.Sum ();
  147. int totalGaps = sizes.Length - 1; // total gaps between items
  148. int totalItemsAndSpaces = totalItemsSize + totalGaps * maxSpaceBetweenItems; // total size of items and spaces if we had enough room
  149. int spaces = totalGaps * maxSpaceBetweenItems; // We'll decrement this below to place one space between each item until we run out
  150. if (totalItemsSize >= containerSize)
  151. {
  152. spaces = 0;
  153. }
  154. else if (totalItemsAndSpaces > containerSize)
  155. {
  156. spaces = containerSize - totalItemsSize;
  157. }
  158. switch (justification)
  159. {
  160. case Justification.Left:
  161. var currentPosition = 0;
  162. for (var i = 0; i < sizes.Length; i++)
  163. {
  164. CheckSizeCannotBeNegative (i, sizes);
  165. if (i == 0)
  166. {
  167. positions [0] = 0; // first item position
  168. continue;
  169. }
  170. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  171. // subsequent items are placed one space after the previous item
  172. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  173. }
  174. break;
  175. case Justification.Right:
  176. currentPosition = Math.Max (0, containerSize - totalItemsSize - spaces);
  177. for (var i = 0; i < sizes.Length; i++)
  178. {
  179. CheckSizeCannotBeNegative (i, sizes);
  180. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  181. positions [i] = currentPosition;
  182. currentPosition += sizes [i] + spaceBefore;
  183. }
  184. break;
  185. case Justification.Centered:
  186. if (sizes.Length > 1)
  187. {
  188. // remaining space to be distributed before first and after the items
  189. int remainingSpace = Math.Max (0, containerSize - totalItemsSize - spaces);
  190. for (var i = 0; i < sizes.Length; i++)
  191. {
  192. CheckSizeCannotBeNegative (i, sizes);
  193. if (i == 0)
  194. {
  195. positions [i] = remainingSpace / 2; // first item position
  196. continue;
  197. }
  198. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  199. // subsequent items are placed one space after the previous item
  200. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  201. }
  202. }
  203. else if (sizes.Length == 1)
  204. {
  205. CheckSizeCannotBeNegative (0, sizes);
  206. positions [0] = (containerSize - sizes [0]) / 2; // single item is centered
  207. }
  208. break;
  209. case Justification.Justified:
  210. int spaceBetween = sizes.Length > 1 ? (containerSize - totalItemsSize) / (sizes.Length - 1) : 0;
  211. int remainder = sizes.Length > 1 ? (containerSize - totalItemsSize) % (sizes.Length - 1) : 0;
  212. currentPosition = 0;
  213. for (var i = 0; i < sizes.Length; i++)
  214. {
  215. CheckSizeCannotBeNegative (i, sizes);
  216. positions [i] = currentPosition;
  217. int extraSpace = i < remainder ? 1 : 0;
  218. currentPosition += sizes [i] + spaceBetween + extraSpace;
  219. }
  220. break;
  221. // 111 2222 33333
  222. case Justification.LastRightRestLeft:
  223. if (sizes.Length > 1)
  224. {
  225. currentPosition = 0;
  226. for (var i = 0; i < sizes.Length; i++)
  227. {
  228. CheckSizeCannotBeNegative (i, sizes);
  229. if (i < sizes.Length - 1)
  230. {
  231. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  232. positions [i] = currentPosition;
  233. currentPosition += sizes [i] + spaceBefore;
  234. }
  235. }
  236. positions [sizes.Length - 1] = containerSize - sizes [^1];
  237. }
  238. else if (sizes.Length == 1)
  239. {
  240. CheckSizeCannotBeNegative (0, sizes);
  241. positions [0] = containerSize - sizes [0]; // single item is flush right
  242. }
  243. break;
  244. // 111 2222 33333
  245. case Justification.FirstLeftRestRight:
  246. if (sizes.Length > 1)
  247. {
  248. currentPosition = 0;
  249. positions [0] = currentPosition; // first item is flush left
  250. for (int i = sizes.Length - 1; i >= 0; i--)
  251. {
  252. CheckSizeCannotBeNegative (i, sizes);
  253. if (i == sizes.Length - 1)
  254. {
  255. // start at right
  256. currentPosition = containerSize - sizes [i];
  257. positions [i] = currentPosition;
  258. }
  259. if (i < sizes.Length - 1 && i > 0)
  260. {
  261. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  262. positions [i] = currentPosition - sizes [i] - spaceBefore;
  263. currentPosition = positions [i];
  264. }
  265. }
  266. }
  267. else if (sizes.Length == 1)
  268. {
  269. CheckSizeCannotBeNegative (0, sizes);
  270. positions [0] = 0; // single item is flush left
  271. }
  272. break;
  273. default:
  274. throw new ArgumentOutOfRangeException (nameof (justification), justification, null);
  275. }
  276. return positions;
  277. }
  278. private static void CheckSizeCannotBeNegative (int i, int [] sizes)
  279. {
  280. if (sizes [i] < 0)
  281. {
  282. throw new ArgumentException ("The size of an item cannot be negative.");
  283. }
  284. }
  285. public override bool Equals (object other)
  286. {
  287. if (other is Justifier justifier)
  288. {
  289. return Justification == justifier.Justification &&
  290. ContainerSize == justifier.ContainerSize &&
  291. PutSpaceBetweenItems == justifier.PutSpaceBetweenItems;
  292. }
  293. return false;
  294. }
  295. }