TextFormatterDemo.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. AutoSize = false,
  31. Height = 10,
  32. Width = Dim.Fill (),
  33. };
  34. var block = new StringBuilder ();
  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. block.AppendLine (" ░ ░ ");
  45. blockText.Text = block.ToString (); // .Replace(" ", "\u00A0"); // \u00A0 is 'non-breaking space
  46. app.Add (blockText);
  47. var unicodeCheckBox = new CheckBox
  48. {
  49. X = 0,
  50. Y = Pos.Bottom (blockText) + 1,
  51. Text = "Unicode",
  52. Checked = app.HotKeySpecifier == (Rune)' '
  53. };
  54. app.Add (unicodeCheckBox);
  55. List<TextAlignment> alignments = Enum.GetValues (typeof (TextAlignment)).Cast<TextAlignment> ().ToList ();
  56. Label [] singleLines = new Label [alignments.Count];
  57. Label [] multipleLines = new Label [alignments.Count];
  58. var multiLineHeight = 5;
  59. foreach (TextAlignment alignment in alignments)
  60. {
  61. singleLines [(int)alignment] = new Label
  62. {
  63. TextAlignment = alignment,
  64. X = 0,
  65. AutoSize = false,
  66. Width = Dim.Fill (),
  67. Height = 1,
  68. ColorScheme = Colors.ColorSchemes ["Dialog"],
  69. Text = text
  70. };
  71. multipleLines [(int)alignment] = new Label
  72. {
  73. TextAlignment = alignment,
  74. X = 0,
  75. AutoSize = false,
  76. Width = Dim.Fill (),
  77. Height = multiLineHeight,
  78. ColorScheme = Colors.ColorSchemes ["Dialog"],
  79. Text = text
  80. };
  81. }
  82. var label = new Label
  83. {
  84. Y = Pos.Bottom (unicodeCheckBox) + 1, Text = "Demonstrating multi-line and word wrap:"
  85. };
  86. app.Add (label);
  87. foreach (TextAlignment alignment in alignments)
  88. {
  89. label = new Label { Y = Pos.Bottom (label), Text = $"{alignment}:" };
  90. app.Add (label);
  91. singleLines [(int)alignment].Y = Pos.Bottom (label);
  92. app.Add (singleLines [(int)alignment]);
  93. label = singleLines [(int)alignment];
  94. }
  95. label = new Label { Y = Pos.Bottom (label), Text = "Demonstrating multi-line and word wrap:" };
  96. app.Add (label);
  97. foreach (TextAlignment alignment in alignments)
  98. {
  99. label = new Label { Y = Pos.Bottom (label), Text = $"{alignment}:" };
  100. app.Add (label);
  101. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  102. app.Add (multipleLines [(int)alignment]);
  103. label = multipleLines [(int)alignment];
  104. }
  105. unicodeCheckBox.Toggled += (s, e) =>
  106. {
  107. foreach (TextAlignment alignment in alignments)
  108. {
  109. singleLines [(int)alignment].Text = e.OldValue == true ? text : unicode;
  110. multipleLines [(int)alignment].Text = e.OldValue == true ? text : unicode;
  111. }
  112. };
  113. Application.Run (app);
  114. app.Dispose ();
  115. }
  116. }