TextFormatterDemo.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios;
  7. [ScenarioMetadata ("TextFormatter Demo", "Demos and tests the TextFormatter class.")]
  8. [ScenarioCategory ("Text and Formatting")]
  9. public class TextFormatterDemo : Scenario
  10. {
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. var app = new Window
  15. {
  16. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
  17. };
  18. // Make Win smaller so sizing the window horizontally will make the
  19. // labels shrink to zero-width
  20. app.X = 10;
  21. app.Width = Dim.Fill (10);
  22. var text = "Hello world, how are you today? Pretty neat!\nSecond line\n\nFourth Line.";
  23. var unicode =
  24. "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ\nτὸ σπίτι φτωχικὸ στὶς ἀμμουδιὲς τοῦ Ὁμήρου.\nΜονάχη ἔγνοια ἡ γλῶσσα μου στὶς ἀμμουδιὲς τοῦ Ὁμήρου.";
  25. var blockText = new Label
  26. {
  27. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  28. X = 0,
  29. Y = 0,
  30. Height = 10,
  31. Width = Dim.Fill ()
  32. };
  33. var block = new StringBuilder ();
  34. block.AppendLine (" ▄████ █ ██ ██▓ ▄████▄ ██████ ");
  35. block.AppendLine (" ██▒ ▀█▒ ██ ▓██▒▓██▒ ▒██▀ ▀█ ▒██ ▒ ");
  36. block.AppendLine ("▒██░▄▄▄░▓██ ▒██░▒██▒ ▒▓█ ▄ ░ ▓██▄ ");
  37. block.AppendLine ("░▓█ ██▓▓▓█ ░██░░██░ ▒▓▓▄ ▄██▒ ▒ ██▒");
  38. block.AppendLine ("░▒▓███▀▒▒▒█████▓ ░██░ ██▓ ▒ ▓███▀ ░▒██████▒▒");
  39. block.AppendLine (" ░▒ ▒ ░▒▓▒ ▒ ▒ ░▓ ▒▓▒ ░ ░▒ ▒ ░▒ ▒▓▒ ▒ ░");
  40. block.AppendLine (" ░ ░ ░░▒░ ░ ░ ▒ ░ ░▒ ░ ▒ ░ ░▒ ░ ░");
  41. block.AppendLine ("░ ░ ░ ░░░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ");
  42. block.AppendLine (" ░ ░ ░ ░ ░ ░ ░ ");
  43. block.AppendLine (" ░ ░ ");
  44. blockText.Text = block.ToString (); // .Replace(" ", "\u00A0"); // \u00A0 is 'non-breaking space
  45. app.Add (blockText);
  46. var unicodeCheckBox = new CheckBox
  47. {
  48. X = 0,
  49. Y = Pos.Bottom (blockText) + 1,
  50. Text = "Unicode",
  51. Checked = app.HotKeySpecifier == (Rune)' '
  52. };
  53. app.Add (unicodeCheckBox);
  54. static IEnumerable<T> GetUniqueEnumValues<T> () where T : Enum
  55. {
  56. var values = new HashSet<T> ();
  57. foreach (T v in Enum.GetValues (typeof (T)))
  58. {
  59. if (values.Add (v))
  60. {
  61. yield return v;
  62. }
  63. }
  64. }
  65. List<Alignment> alignments = new () { Alignment.Start, Alignment.End, Alignment.Center, Alignment.Fill };
  66. Label [] singleLines = new Label [alignments.Count];
  67. Label [] multipleLines = new Label [alignments.Count];
  68. var multiLineHeight = 5;
  69. for (int i = 0; i < alignments.Count; i++)
  70. {
  71. singleLines [i] = new ()
  72. {
  73. TextAlignment = alignments [i],
  74. X = 0,
  75. Width = Dim.Fill (),
  76. Height = 1,
  77. ColorScheme = Colors.ColorSchemes ["Dialog"],
  78. Text = text
  79. };
  80. multipleLines [i] = new ()
  81. {
  82. TextAlignment = alignments [i],
  83. X = 0,
  84. Width = Dim.Fill (),
  85. Height = multiLineHeight,
  86. ColorScheme = Colors.ColorSchemes ["Dialog"],
  87. Text = text
  88. };
  89. }
  90. var label = new Label
  91. {
  92. Y = Pos.Bottom (unicodeCheckBox) + 1, Text = "Demonstrating multi-line and word wrap:"
  93. };
  94. app.Add (label);
  95. for (int i = 0; i < alignments.Count; i++)
  96. {
  97. label = new () { Y = Pos.Bottom (label), Text = $"{alignments [i]}:" };
  98. app.Add (label);
  99. singleLines [i].Y = Pos.Bottom (label);
  100. app.Add (singleLines [i]);
  101. label = singleLines [i];
  102. }
  103. label = new () { Y = Pos.Bottom (label), Text = "Demonstrating multi-line and word wrap:" };
  104. app.Add (label);
  105. for (int i = 0; i < alignments.Count; i++)
  106. {
  107. label = new () { Y = Pos.Bottom (label), Text = $"{alignments [i]}:" };
  108. app.Add (label);
  109. multipleLines [i].Y = Pos.Bottom (label);
  110. app.Add (multipleLines [i]);
  111. label = multipleLines [i];
  112. }
  113. unicodeCheckBox.Toggled += (s, e) =>
  114. {
  115. for (int i = 0; i < alignments.Count; i++)
  116. {
  117. singleLines [i].Text = e.OldValue == true ? text : unicode;
  118. multipleLines [i].Text = e.OldValue == true ? text : unicode;
  119. }
  120. };
  121. Application.Run (app);
  122. app.Dispose ();
  123. }
  124. }