TextFormatterDemo.cs 5.3 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. 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. List<Justification> alignments = Enum.GetValues (typeof (Justification)).Cast<Justification> ().ToList ();
  55. Label [] singleLines = new Label [alignments.Count];
  56. Label [] multipleLines = new Label [alignments.Count];
  57. var multiLineHeight = 5;
  58. foreach (Justification alignment in alignments)
  59. {
  60. singleLines [(int)alignment] = new()
  61. {
  62. Justification = alignment,
  63. X = 0,
  64. Width = Dim.Fill (),
  65. Height = 1,
  66. ColorScheme = Colors.ColorSchemes ["Dialog"],
  67. Text = text
  68. };
  69. multipleLines [(int)alignment] = new()
  70. {
  71. Justification = alignment,
  72. X = 0,
  73. Width = Dim.Fill (),
  74. Height = multiLineHeight,
  75. ColorScheme = Colors.ColorSchemes ["Dialog"],
  76. Text = text
  77. };
  78. }
  79. var label = new Label
  80. {
  81. Y = Pos.Bottom (unicodeCheckBox) + 1, Text = "Demonstrating multi-line and word wrap:"
  82. };
  83. app.Add (label);
  84. foreach (Justification alignment in alignments)
  85. {
  86. label = new() { Y = Pos.Bottom (label), Text = $"{alignment}:" };
  87. app.Add (label);
  88. singleLines [(int)alignment].Y = Pos.Bottom (label);
  89. app.Add (singleLines [(int)alignment]);
  90. label = singleLines [(int)alignment];
  91. }
  92. label = new() { Y = Pos.Bottom (label), Text = "Demonstrating multi-line and word wrap:" };
  93. app.Add (label);
  94. foreach (Justification alignment in alignments)
  95. {
  96. label = new() { Y = Pos.Bottom (label), Text = $"{alignment}:" };
  97. app.Add (label);
  98. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  99. app.Add (multipleLines [(int)alignment]);
  100. label = multipleLines [(int)alignment];
  101. }
  102. unicodeCheckBox.Toggled += (s, e) =>
  103. {
  104. foreach (Justification alignment in alignments)
  105. {
  106. singleLines [(int)alignment].Text = e.OldValue == true ? text : unicode;
  107. multipleLines [(int)alignment].Text = e.OldValue == true ? text : unicode;
  108. }
  109. };
  110. Application.Run (app);
  111. app.Dispose ();
  112. }
  113. }