TextFormatterDemo.cs 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 and Formatting")]
  11. public class TextFormatterDemo : Scenario {
  12. public override void Setup ()
  13. {
  14. // TODO: Move this to another Scenario that specifically tests `Views` that have no subviews.
  15. //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" +
  16. // "<-- 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.";
  17. //Top.TextAlignment = TextAlignment.Centered;
  18. //Top.ColorScheme = Colors.Base;
  19. // Make Win smaller so sizing the window horizontally will make the
  20. // labels shrink to zero-width
  21. Win.X = 10;
  22. Win.Width = Dim.Fill (10);
  23. string text = "Hello world, how are you today? Pretty neat!\nSecond line\n\nFourth Line.";
  24. string unicode = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ\nτὸ σπίτι φτωχικὸ στὶς ἀμμουδιὲς τοῦ Ὁμήρου.\nΜονάχη ἔγνοια ἡ γλῶσσα μου στὶς ἀμμουδιὲς τοῦ Ὁμήρου.";
  25. Label blockText = new Label () { ColorScheme = Colors.TopLevel, X = 0, Y = 0, Height = 10, Width = Dim.Fill (0), AutoSize = false };
  26. var block = new StringBuilder ();
  27. block.AppendLine (" ▄████ █ ██ ██▓ ▄████▄ ██████ ");
  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. blockText.Text = ustring.Make (block.ToString ()); // .Replace(" ", "\u00A0"); // \u00A0 is 'non-breaking space
  38. Win.Add (blockText);
  39. var unicodeCheckBox = new CheckBox ("Unicode", Application.Top.HotKeySpecifier == (Rune)' ') {
  40. X = 0,
  41. Y = Pos.Bottom (blockText) + 1,
  42. };
  43. Win.Add (unicodeCheckBox);
  44. var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
  45. var singleLines = new Label [alignments.Count];
  46. var multipleLines = new Label [alignments.Count];
  47. var multiLineHeight = 5;
  48. foreach (var alignment in alignments) {
  49. singleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = 1, ColorScheme = Colors.Dialog, AutoSize = false };
  50. multipleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = multiLineHeight, ColorScheme = Colors.Dialog, AutoSize = false };
  51. }
  52. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (unicodeCheckBox) + 1 };
  53. Win.Add (label);
  54. foreach (var alignment in alignments) {
  55. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  56. Win.Add (label);
  57. singleLines [(int)alignment].Y = Pos.Bottom (label);
  58. Win.Add (singleLines [(int)alignment]);
  59. label = singleLines [(int)alignment];
  60. }
  61. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) };
  62. Win.Add (label);
  63. foreach (var alignment in alignments) {
  64. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  65. Win.Add (label);
  66. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  67. Win.Add (multipleLines [(int)alignment]);
  68. label = multipleLines [(int)alignment];
  69. }
  70. unicodeCheckBox.Toggled += (previous) => {
  71. foreach (var alignment in alignments) {
  72. singleLines [(int)alignment].Text = previous ? text : unicode;
  73. multipleLines [(int)alignment].Text = previous ? text : unicode;
  74. }
  75. };
  76. }
  77. }
  78. }