TextFormatterDemo.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. using Rune = System.Rune;
  8. namespace UICatalog.Scenarios {
  9. [ScenarioMetadata (Name: "TextFormatter Demo", Description: "Demos and tests the TextFormatter class.")]
  10. [ScenarioCategory ("Text")]
  11. [ScenarioCategory ("POC"), ScenarioCategory ("TextFormat")]
  12. public class TextFormatterDemo : Scenario {
  13. public override void Setup ()
  14. {
  15. // TODO: Move this to another Scenario that specifically tests `Views` that have no subviews.
  16. //Top.Text = "Press CTRL-Q to Quit. This is the Text for the TopLevel View. TextAlignment.Centered was specified. It is intentionally very long to illustrate word wrap.\n" +
  17. // "<-- There is a new line here to show a hard line break. You should see this text bleed underneath the subviews, which start at Y = 3.";
  18. //Top.TextAlignment = TextAlignment.Centered;
  19. //Top.ColorScheme = Colors.Base;
  20. // Make Win smaller so sizing the window horizontally will make the
  21. // labels shrink to zero-width
  22. Win.X = 10;
  23. Win.Width = Dim.Fill (10);
  24. string text = "Hello world, how are you today? Pretty neat!\nSecond line\n\nFourth Line.";
  25. string unicode = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ\nτὸ σπίτι φτωχικὸ στὶς ἀμμουδιὲς τοῦ Ὁμήρου.\nΜονάχη ἔγνοια ἡ γλῶσσα μου στὶς ἀμμουδιὲς τοῦ Ὁμήρου.";
  26. Label blockText = new Label () { ColorScheme = Colors.TopLevel, X = 0, Y = 0, Height = 10, Width = Dim.Fill (0) };
  27. var block = new StringBuilder ();
  28. block.AppendLine (" ▄████ █ ██ ██▓ ▄████▄ ██████ ");
  29. block.AppendLine (" ██▒ ▀█▒ ██ ▓██▒▓██▒ ▒██▀ ▀█ ▒██ ▒ ");
  30. block.AppendLine ("▒██░▄▄▄░▓██ ▒██░▒██▒ ▒▓█ ▄ ░ ▓██▄ ");
  31. block.AppendLine ("░▓█ ██▓▓▓█ ░██░░██░ ▒▓▓▄ ▄██▒ ▒ ██▒");
  32. block.AppendLine ("░▒▓███▀▒▒▒█████▓ ░██░ ██▓ ▒ ▓███▀ ░▒██████▒▒");
  33. block.AppendLine (" ░▒ ▒ ░▒▓▒ ▒ ▒ ░▓ ▒▓▒ ░ ░▒ ▒ ░▒ ▒▓▒ ▒ ░");
  34. block.AppendLine (" ░ ░ ░░▒░ ░ ░ ▒ ░ ░▒ ░ ▒ ░ ░▒ ░ ░");
  35. block.AppendLine ("░ ░ ░ ░░░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ");
  36. block.AppendLine (" ░ ░ ░ ░ ░ ░ ░ ");
  37. block.AppendLine (" ░ ░ ");
  38. blockText.Text = ustring.Make (block.ToString ()); // .Replace(" ", "\u00A0"); // \u00A0 is 'non-breaking space
  39. Win.Add (blockText);
  40. var unicodeCheckBox = new CheckBox ("Unicode", Top.HotKeySpecifier == (Rune)' ') {
  41. X = 0,
  42. Y = Pos.Bottom (blockText) + 1,
  43. };
  44. Win.Add (unicodeCheckBox);
  45. var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
  46. var singleLines = new Label [alignments.Count];
  47. var multipleLines = new Label [alignments.Count];
  48. var multiLineHeight = 5;
  49. foreach (var alignment in alignments) {
  50. singleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = 1, ColorScheme = Colors.Dialog };
  51. multipleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = multiLineHeight, ColorScheme = Colors.Dialog };
  52. }
  53. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (unicodeCheckBox) + 1 };
  54. Win.Add (label);
  55. foreach (var alignment in alignments) {
  56. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  57. Win.Add (label);
  58. singleLines [(int)alignment].Y = Pos.Bottom (label);
  59. Win.Add (singleLines [(int)alignment]);
  60. label = singleLines [(int)alignment];
  61. }
  62. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) };
  63. Win.Add (label);
  64. foreach (var alignment in alignments) {
  65. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  66. Win.Add (label);
  67. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  68. Win.Add (multipleLines [(int)alignment]);
  69. label = multipleLines [(int)alignment];
  70. }
  71. unicodeCheckBox.Toggled += (previous) => {
  72. foreach (var alignment in alignments) {
  73. singleLines [(int)alignment].Text = previous ? text : unicode;
  74. multipleLines [(int)alignment].Text = previous ? text : unicode;
  75. }
  76. };
  77. }
  78. }
  79. }