TextFormatterDemo.cs 4.6 KB

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