TextFormatter.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using NStack;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Text alignment enumeration, controls how text is displayed.
  9. /// </summary>
  10. public enum TextAlignment {
  11. /// <summary>
  12. /// Aligns the text to the left of the frame.
  13. /// </summary>
  14. Left,
  15. /// <summary>
  16. /// Aligns the text to the right side of the frame.
  17. /// </summary>
  18. Right,
  19. /// <summary>
  20. /// Centers the text in the frame.
  21. /// </summary>
  22. Centered,
  23. /// <summary>
  24. /// Shows the text as justified text in the frame.
  25. /// </summary>
  26. Justified
  27. }
  28. /// <summary>
  29. /// Provides text formatting capabilities for console apps. Supports, hotkeys, horizontal alignment, multiple lines, and word-based line wrap.
  30. /// </summary>
  31. public class TextFormatter {
  32. List<ustring> lines = new List<ustring> ();
  33. ustring text;
  34. TextAlignment textAlignment;
  35. Attribute textColor = -1;
  36. bool needsFormat;
  37. Key hotKey;
  38. Size size;
  39. /// <summary>
  40. /// The text to be displayed. This text is never modified.
  41. /// </summary>
  42. public virtual ustring Text {
  43. get => text;
  44. set {
  45. text = value;
  46. if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount)) {
  47. // Provide a default size (width = length of longest line, height = 1)
  48. // TODO: It might makes more sense for the default to be width = length of first line?
  49. Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1);
  50. }
  51. NeedsFormat = true;
  52. }
  53. }
  54. // TODO: Add Vertical Text Alignment
  55. /// <summary>
  56. /// Controls the horizontal text-alignment property.
  57. /// </summary>
  58. /// <value>The text alignment.</value>
  59. public TextAlignment Alignment {
  60. get => textAlignment;
  61. set {
  62. textAlignment = value;
  63. NeedsFormat = true;
  64. }
  65. }
  66. /// <summary>
  67. /// Gets or sets the size of the area the text will be constrained to when formatted.
  68. /// </summary>
  69. public Size Size {
  70. get => size;
  71. set {
  72. size = value;
  73. NeedsFormat = true;
  74. }
  75. }
  76. /// <summary>
  77. /// The specifier character for the hotkey (e.g. '_'). Set to '\xffff' to disable hotkey support for this View instance. The default is '\xffff'.
  78. /// </summary>
  79. public Rune HotKeySpecifier { get; set; } = (Rune)0xFFFF;
  80. /// <summary>
  81. /// The position in the text of the hotkey. The hotkey will be rendered using the hot color.
  82. /// </summary>
  83. public int HotKeyPos { get => hotKeyPos; set => hotKeyPos = value; }
  84. /// <summary>
  85. /// Gets the hotkey. Will be an upper case letter or digit.
  86. /// </summary>
  87. public Key HotKey { get => hotKey; internal set => hotKey = value; }
  88. /// <summary>
  89. /// Specifies the mask to apply to the hotkey to tag it as the hotkey. The default value of <c>0x100000</c> causes
  90. /// the underlying Rune to be identified as a "private use" Unicode character.
  91. /// </summary>HotKeyTagMask
  92. public uint HotKeyTagMask { get; set; } = 0x100000;
  93. /// <summary>
  94. /// Gets the cursor position from <see cref="HotKey"/>. If the <see cref="HotKey"/> is defined, the cursor will be positioned over it.
  95. /// </summary>
  96. public int CursorPosition { get; set; }
  97. /// <summary>
  98. /// Gets the formatted lines.
  99. /// </summary>
  100. /// <remarks>
  101. /// <para>
  102. /// Upon a 'get' of this property, if the text needs to be formatted (if <see cref="NeedsFormat"/> is <c>true</c>)
  103. /// <see cref="Format(ustring, int, TextAlignment, bool, bool)"/> will be called internally.
  104. /// </para>
  105. /// </remarks>
  106. public List<ustring> Lines {
  107. get {
  108. // With this check, we protect against subclasses with overrides of Text
  109. if (ustring.IsNullOrEmpty (Text)) {
  110. lines = new List<ustring> ();
  111. lines.Add (ustring.Empty);
  112. NeedsFormat = false;
  113. return lines;
  114. }
  115. if (NeedsFormat) {
  116. var shown_text = text;
  117. if (FindHotKey (text, HotKeySpecifier, true, out hotKeyPos, out hotKey)) {
  118. shown_text = RemoveHotKeySpecifier (Text, hotKeyPos, HotKeySpecifier);
  119. shown_text = ReplaceHotKeyWithTag (shown_text, hotKeyPos);
  120. }
  121. if (Size.IsEmpty) {
  122. throw new InvalidOperationException ("Size must be set before accessing Lines");
  123. }
  124. lines = Format (shown_text, Size.Width, textAlignment, Size.Height > 1);
  125. NeedsFormat = false;
  126. }
  127. return lines;
  128. }
  129. }
  130. /// <summary>
  131. /// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text when <see cref="Draw(Rect, Attribute, Attribute)"/> is called.
  132. /// If it is <c>false</c> when Draw is called, the Draw call will be faster.
  133. /// </summary>
  134. /// <remarks>
  135. /// <para>
  136. /// This is set to true when the properties of <see cref="TextFormatter"/> are set.
  137. /// </para>
  138. /// </remarks>
  139. public bool NeedsFormat { get => needsFormat; set => needsFormat = value; }
  140. static ustring StripCRLF (ustring str)
  141. {
  142. var runes = str.ToRuneList ();
  143. for (int i = 0; i < runes.Count; i++) {
  144. switch (runes [i]) {
  145. case '\n':
  146. runes.RemoveAt (i);
  147. break;
  148. case '\r':
  149. if ((i + 1) < runes.Count && runes [i + 1] == '\n') {
  150. runes.RemoveAt (i);
  151. runes.RemoveAt (i + 1);
  152. i++;
  153. } else {
  154. runes.RemoveAt (i);
  155. }
  156. break;
  157. }
  158. }
  159. return ustring.Make (runes);
  160. }
  161. static ustring ReplaceCRLFWithSpace (ustring str)
  162. {
  163. var runes = str.ToRuneList ();
  164. for (int i = 0; i < runes.Count; i++) {
  165. switch (runes [i]) {
  166. case '\n':
  167. runes [i] = (Rune)' ';
  168. break;
  169. case '\r':
  170. if ((i + 1) < runes.Count && runes [i + 1] == '\n') {
  171. runes [i] = (Rune)' ';
  172. runes.RemoveAt (i + 1);
  173. i++;
  174. } else {
  175. runes [i] = (Rune)' ';
  176. }
  177. break;
  178. }
  179. }
  180. return ustring.Make (runes);
  181. }
  182. /// <summary>
  183. /// Formats the provided text to fit within the width provided using word wrapping.
  184. /// </summary>
  185. /// <param name="text">The text to word wrap</param>
  186. /// <param name="width">The width to contain the text to</param>
  187. /// <param name="preserveTrailingSpaces">If <c>true</c>, the wrapped text will keep the trailing spaces.
  188. /// If <c>false</c>, the trailing spaces will be trimmed.</param>
  189. /// <returns>Returns a list of word wrapped lines.</returns>
  190. /// <remarks>
  191. /// <para>
  192. /// This method does not do any justification.
  193. /// </para>
  194. /// <para>
  195. /// This method strips Newline ('\n' and '\r\n') sequences before processing.
  196. /// </para>
  197. /// </remarks>
  198. public static List<ustring> WordWrap (ustring text, int width, bool preserveTrailingSpaces = false)
  199. {
  200. if (width < 0) {
  201. throw new ArgumentOutOfRangeException ("Width cannot be negative.");
  202. }
  203. int start = 0, end;
  204. var lines = new List<ustring> ();
  205. if (ustring.IsNullOrEmpty (text)) {
  206. return lines;
  207. }
  208. var runes = StripCRLF (text).ToRuneList ();
  209. while ((end = start + width) < runes.Count) {
  210. while (runes [end] != ' ' && end > start)
  211. end -= 1;
  212. if (end == start)
  213. end = start + width;
  214. lines.Add (ustring.Make (runes.GetRange (start, end - start)));
  215. start = end;
  216. if (runes [end] == ' ' && !preserveTrailingSpaces) {
  217. start++;
  218. }
  219. }
  220. if (start < text.RuneCount) {
  221. lines.Add (ustring.Make (runes.GetRange (start, runes.Count - start)));
  222. }
  223. return lines;
  224. }
  225. /// <summary>
  226. /// Justifies text within a specified width.
  227. /// </summary>
  228. /// <param name="text">The text to justify.</param>
  229. /// <param name="width">If the text length is greater that <c>width</c> it will be clipped.</param>
  230. /// <param name="talign">Alignment.</param>
  231. /// <returns>Justified and clipped text.</returns>
  232. public static ustring ClipAndJustify (ustring text, int width, TextAlignment talign)
  233. {
  234. if (width < 0) {
  235. throw new ArgumentOutOfRangeException ("Width cannot be negative.");
  236. }
  237. if (ustring.IsNullOrEmpty (text)) {
  238. return text;
  239. }
  240. var runes = text.ToRuneList ();
  241. int slen = runes.Count;
  242. if (slen > width) {
  243. return ustring.Make (runes.GetRange (0, width));
  244. } else {
  245. if (talign == TextAlignment.Justified) {
  246. return Justify (text, width);
  247. }
  248. return text;
  249. }
  250. }
  251. /// <summary>
  252. /// Justifies the text to fill the width provided. Space will be added between words (demarked by spaces and tabs) to
  253. /// make the text just fit <c>width</c>. Spaces will not be added to the ends.
  254. /// </summary>
  255. /// <param name="text"></param>
  256. /// <param name="width"></param>
  257. /// <param name="spaceChar">Character to replace whitespace and pad with. For debugging purposes.</param>
  258. /// <returns>The justified text.</returns>
  259. public static ustring Justify (ustring text, int width, char spaceChar = ' ')
  260. {
  261. if (width < 0) {
  262. throw new ArgumentOutOfRangeException ("Width cannot be negative.");
  263. }
  264. if (ustring.IsNullOrEmpty (text)) {
  265. return text;
  266. }
  267. var words = text.Split (ustring.Make (' '));
  268. int textCount = words.Sum (arg => arg.RuneCount);
  269. var spaces = words.Length > 1 ? (width - textCount) / (words.Length - 1) : 0;
  270. var extras = words.Length > 1 ? (width - textCount) % words.Length : 0;
  271. var s = new System.Text.StringBuilder ();
  272. for (int w = 0; w < words.Length; w++) {
  273. var x = words [w];
  274. s.Append (x);
  275. if (w + 1 < words.Length)
  276. for (int i = 0; i < spaces; i++)
  277. s.Append (spaceChar);
  278. if (extras > 0) {
  279. extras--;
  280. }
  281. }
  282. return ustring.Make (s.ToString ());
  283. }
  284. static char [] whitespace = new char [] { ' ', '\t' };
  285. private int hotKeyPos;
  286. /// <summary>
  287. /// Reformats text into lines, applying text alignment and optionally wrapping text to new lines on word boundaries.
  288. /// </summary>
  289. /// <param name="text"></param>
  290. /// <param name="width">The width to bound the text to for word wrapping and clipping.</param>
  291. /// <param name="talign">Specifies how the text will be aligned horizontally.</param>
  292. /// <param name="wordWrap">If <c>true</c>, the text will be wrapped to new lines as need. If <c>false</c>, forces text to fit a single line. Line breaks are converted to spaces. The text will be clipped to <c>width</c></param>
  293. /// <param name="preserveTrailingSpaces">If <c>true</c> and 'wordWrap' also true, the wrapped text will keep the trailing spaces. If <c>false</c>, the trailing spaces will be trimmed.</param>
  294. /// <returns>A list of word wrapped lines.</returns>
  295. /// <remarks>
  296. /// <para>
  297. /// An empty <c>text</c> string will result in one empty line.
  298. /// </para>
  299. /// <para>
  300. /// If <c>width</c> is 0, a single, empty line will be returned.
  301. /// </para>
  302. /// <para>
  303. /// If <c>width</c> is int.MaxValue, the text will be formatted to the maximum width possible.
  304. /// </para>
  305. /// </remarks>
  306. public static List<ustring> Format (ustring text, int width, TextAlignment talign, bool wordWrap, bool preserveTrailingSpaces = false)
  307. {
  308. if (width < 0) {
  309. throw new ArgumentOutOfRangeException ("width cannot be negative");
  310. }
  311. if (preserveTrailingSpaces && !wordWrap) {
  312. throw new ArgumentException ("if 'preserveTrailingSpaces' is true, then 'wordWrap' must be true either.");
  313. }
  314. List<ustring> lineResult = new List<ustring> ();
  315. if (ustring.IsNullOrEmpty (text) || width == 0) {
  316. lineResult.Add (ustring.Empty);
  317. return lineResult;
  318. }
  319. if (wordWrap == false) {
  320. text = ReplaceCRLFWithSpace (text);
  321. lineResult.Add (ClipAndJustify (text, width, talign));
  322. return lineResult;
  323. }
  324. var runes = text.ToRuneList ();
  325. int runeCount = runes.Count;
  326. int lp = 0;
  327. for (int i = 0; i < runeCount; i++) {
  328. Rune c = runes [i];
  329. if (c == '\n') {
  330. var wrappedLines = WordWrap (ustring.Make (runes.GetRange (lp, i - lp)), width, preserveTrailingSpaces);
  331. foreach (var line in wrappedLines) {
  332. lineResult.Add (ClipAndJustify (line, width, talign));
  333. }
  334. if (wrappedLines.Count == 0) {
  335. lineResult.Add (ustring.Empty);
  336. }
  337. lp = i + 1;
  338. }
  339. }
  340. foreach (var line in WordWrap (ustring.Make (runes.GetRange (lp, runeCount - lp)), width, preserveTrailingSpaces)) {
  341. lineResult.Add (ClipAndJustify (line, width, talign));
  342. }
  343. return lineResult;
  344. }
  345. /// <summary>
  346. /// Computes the number of lines needed to render the specified text given the width.
  347. /// </summary>
  348. /// <returns>Number of lines.</returns>
  349. /// <param name="text">Text, may contain newlines.</param>
  350. /// <param name="width">The minimum width for the text.</param>
  351. public static int MaxLines (ustring text, int width)
  352. {
  353. var result = TextFormatter.Format (text, width, TextAlignment.Left, true);
  354. return result.Count;
  355. }
  356. /// <summary>
  357. /// Computes the maximum width needed to render the text (single line or multiple lines) given a minimum width.
  358. /// </summary>
  359. /// <returns>Max width of lines.</returns>
  360. /// <param name="text">Text, may contain newlines.</param>
  361. /// <param name="width">The minimum width for the text.</param>
  362. public static int MaxWidth (ustring text, int width)
  363. {
  364. var result = TextFormatter.Format (text, width, TextAlignment.Left, true);
  365. var max = 0;
  366. result.ForEach (s => {
  367. var m = 0;
  368. s.ToRuneList ().ForEach (r => m += Rune.ColumnWidth (r));
  369. if (m > max) {
  370. max = m;
  371. }
  372. });
  373. return max;
  374. }
  375. /// <summary>
  376. /// Calculates the rectangle required to hold text, assuming no word wrapping.
  377. /// </summary>
  378. /// <param name="x">The x location of the rectangle</param>
  379. /// <param name="y">The y location of the rectangle</param>
  380. /// <param name="text">The text to measure</param>
  381. /// <returns></returns>
  382. public static Rect CalcRect (int x, int y, ustring text)
  383. {
  384. if (ustring.IsNullOrEmpty (text)) {
  385. return new Rect (new Point (x, y), Size.Empty);
  386. }
  387. int mw = 0;
  388. int ml = 1;
  389. int cols = 0;
  390. foreach (var rune in text) {
  391. if (rune == '\n') {
  392. ml++;
  393. if (cols > mw) {
  394. mw = cols;
  395. }
  396. cols = 0;
  397. } else {
  398. if (rune != '\r') {
  399. cols++;
  400. var rw = Rune.ColumnWidth (rune);
  401. if (rw > 0) {
  402. rw--;
  403. }
  404. cols += rw;
  405. }
  406. }
  407. }
  408. if (cols > mw) {
  409. mw = cols;
  410. }
  411. return new Rect (x, y, mw, ml);
  412. }
  413. /// <summary>
  414. /// Finds the hotkey and its location in text.
  415. /// </summary>
  416. /// <param name="text">The text to look in.</param>
  417. /// <param name="hotKeySpecifier">The hotkey specifier (e.g. '_') to look for.</param>
  418. /// <param name="firstUpperCase">If <c>true</c> the legacy behavior of identifying the first upper case character as the hotkey will be enabled.
  419. /// Regardless of the value of this parameter, <c>hotKeySpecifier</c> takes precedence.</param>
  420. /// <param name="hotPos">Outputs the Rune index into <c>text</c>.</param>
  421. /// <param name="hotKey">Outputs the hotKey.</param>
  422. /// <returns><c>true</c> if a hotkey was found; <c>false</c> otherwise.</returns>
  423. public static bool FindHotKey (ustring text, Rune hotKeySpecifier, bool firstUpperCase, out int hotPos, out Key hotKey)
  424. {
  425. if (ustring.IsNullOrEmpty (text) || hotKeySpecifier == (Rune)0xFFFF) {
  426. hotPos = -1;
  427. hotKey = Key.Unknown;
  428. return false;
  429. }
  430. Rune hot_key = (Rune)0;
  431. int hot_pos = -1;
  432. // Use first hot_key char passed into 'hotKey'.
  433. // TODO: Ignore hot_key of two are provided
  434. // TODO: Do not support non-alphanumeric chars that can't be typed
  435. int i = 0;
  436. foreach (Rune c in text) {
  437. if ((char)c != 0xFFFD) {
  438. if (c == hotKeySpecifier) {
  439. hot_pos = i;
  440. } else if (hot_pos > -1) {
  441. hot_key = c;
  442. break;
  443. }
  444. }
  445. i++;
  446. }
  447. // Legacy support - use first upper case char if the specifier was not found
  448. if (hot_pos == -1 && firstUpperCase) {
  449. i = 0;
  450. foreach (Rune c in text) {
  451. if ((char)c != 0xFFFD) {
  452. if (Rune.IsUpper (c)) {
  453. hot_key = c;
  454. hot_pos = i;
  455. break;
  456. }
  457. }
  458. i++;
  459. }
  460. }
  461. if (hot_key != (Rune)0 && hot_pos != -1) {
  462. hotPos = hot_pos;
  463. if (hot_key.IsValid && char.IsLetterOrDigit ((char)hot_key)) {
  464. hotKey = (Key)char.ToUpperInvariant ((char)hot_key);
  465. return true;
  466. }
  467. }
  468. hotPos = -1;
  469. hotKey = Key.Unknown;
  470. return false;
  471. }
  472. /// <summary>
  473. /// Replaces the Rune at the index specified by the <c>hotPos</c> parameter with a tag identifying
  474. /// it as the hotkey.
  475. /// </summary>
  476. /// <param name="text">The text to tag the hotkey in.</param>
  477. /// <param name="hotPos">The Rune index of the hotkey in <c>text</c>.</param>
  478. /// <returns>The text with the hotkey tagged.</returns>
  479. /// <remarks>
  480. /// The returned string will not render correctly without first un-doing the tag. To undo the tag, search for
  481. /// Runes with a bitmask of <c>otKeyTagMask</c> and remove that bitmask.
  482. /// </remarks>
  483. public ustring ReplaceHotKeyWithTag (ustring text, int hotPos)
  484. {
  485. // Set the high bit
  486. var runes = text.ToRuneList ();
  487. if (Rune.IsLetterOrNumber (runes [hotPos])) {
  488. runes [hotPos] = new Rune ((uint)runes [hotPos] | HotKeyTagMask);
  489. }
  490. return ustring.Make (runes);
  491. }
  492. /// <summary>
  493. /// Removes the hotkey specifier from text.
  494. /// </summary>
  495. /// <param name="text">The text to manipulate.</param>
  496. /// <param name="hotKeySpecifier">The hot-key specifier (e.g. '_') to look for.</param>
  497. /// <param name="hotPos">Returns the position of the hot-key in the text. -1 if not found.</param>
  498. /// <returns>The input text with the hotkey specifier ('_') removed.</returns>
  499. public static ustring RemoveHotKeySpecifier (ustring text, int hotPos, Rune hotKeySpecifier)
  500. {
  501. if (ustring.IsNullOrEmpty (text)) {
  502. return text;
  503. }
  504. // Scan
  505. ustring start = ustring.Empty;
  506. int i = 0;
  507. foreach (Rune c in text) {
  508. if (c == hotKeySpecifier && i == hotPos) {
  509. i++;
  510. continue;
  511. }
  512. start += ustring.Make (c);
  513. i++;
  514. }
  515. return start;
  516. }
  517. /// <summary>
  518. /// Draws the text held by <see cref="TextFormatter"/> to <see cref="Application.Driver"/> using the colors specified.
  519. /// </summary>
  520. /// <param name="bounds">Specifies the screen-relative location and maximum size for drawing the text.</param>
  521. /// <param name="normalColor">The color to use for all text except the hotkey</param>
  522. /// <param name="hotColor">The color to use to draw the hotkey</param>
  523. public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor)
  524. {
  525. // With this check, we protect against subclasses with overrides of Text (like Button)
  526. if (ustring.IsNullOrEmpty (text)) {
  527. return;
  528. }
  529. Application.Driver?.SetAttribute (normalColor);
  530. // Use "Lines" to ensure a Format (don't use "lines"))
  531. for (int line = 0; line < Lines.Count; line++) {
  532. if (line > bounds.Height)
  533. continue;
  534. var runes = lines [line].ToRunes ();
  535. int x;
  536. switch (textAlignment) {
  537. case TextAlignment.Left:
  538. case TextAlignment.Justified:
  539. x = bounds.Left;
  540. CursorPosition = hotKeyPos;
  541. break;
  542. case TextAlignment.Right:
  543. x = bounds.Right - runes.Length;
  544. CursorPosition = bounds.Width - runes.Length + hotKeyPos;
  545. break;
  546. case TextAlignment.Centered:
  547. x = bounds.Left + (bounds.Width - runes.Length) / 2;
  548. CursorPosition = (bounds.Width - runes.Length) / 2 + hotKeyPos;
  549. break;
  550. default:
  551. throw new ArgumentOutOfRangeException ();
  552. }
  553. var col = bounds.Left;
  554. for (var idx = bounds.Left; idx < bounds.Left + bounds.Width; idx++) {
  555. Application.Driver?.Move (col, bounds.Top + line);
  556. var rune = (Rune)' ';
  557. if (idx >= x && idx < (x + runes.Length)) {
  558. rune = runes [idx - x];
  559. }
  560. if ((rune & HotKeyTagMask) == HotKeyTagMask) {
  561. if (textAlignment == TextAlignment.Justified) {
  562. CursorPosition = idx - bounds.Left;
  563. }
  564. Application.Driver?.SetAttribute (hotColor);
  565. Application.Driver?.AddRune ((Rune)((uint)rune & ~HotKeyTagMask));
  566. Application.Driver?.SetAttribute (normalColor);
  567. } else {
  568. Application.Driver?.AddRune (rune);
  569. }
  570. col += Rune.ColumnWidth (rune);
  571. if (idx + 1 > - 1 && idx + 1 < runes.Length && col
  572. + Rune.ColumnWidth (runes [idx + 1]) > bounds.Width) {
  573. break;
  574. }
  575. }
  576. }
  577. }
  578. }
  579. }