TextFormatterDemo.cs 5.2 KB

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