|
| 1 | +#nullable enable |
| 2 | +using Terminal.Gui; |
| 3 | + |
| 4 | +namespace UICatalog.Scenarios; |
| 5 | + |
| 6 | +[ScenarioMetadata ("Text Styles", "Shows Attribute.TextStyles including bold, italic, etc...")] |
| 7 | +[ScenarioCategory ("Text and Formatting")] |
| 8 | +[ScenarioCategory ("Colors")] |
| 9 | +public sealed class TestStyles : Scenario |
| 10 | +{ |
| 11 | + public override void Main () |
| 12 | + { |
| 13 | + // Init |
| 14 | + Application.Init (); |
| 15 | + |
| 16 | + // Setup - Create a top-level application window and configure it. |
| 17 | + Window appWindow = new () |
| 18 | + { |
| 19 | + Title = GetQuitKeyAndName (), |
| 20 | + BorderStyle = LineStyle.None |
| 21 | + }; |
| 22 | + |
| 23 | + appWindow.DrawingContent += OnAppWindowOnDrawingContent; |
| 24 | + |
| 25 | + // Run - Start the application. |
| 26 | + Application.Run (appWindow); |
| 27 | + appWindow.Dispose (); |
| 28 | + |
| 29 | + // Shutdown - Calling Application.Shutdown is required. |
| 30 | + Application.Shutdown (); |
| 31 | + } |
| 32 | + |
| 33 | + private void OnAppWindowOnDrawingContent (object? sender, DrawEventArgs args) |
| 34 | + { |
| 35 | + if (sender is View { } sendingView) |
| 36 | + { |
| 37 | + var y = 0; |
| 38 | + var x = 0; |
| 39 | + int maxWidth = sendingView.Viewport.Width; // Get the available width of the view |
| 40 | + |
| 41 | + TextStyle [] allStyles = Enum.GetValues (typeof (TextStyle)) |
| 42 | + .Cast<TextStyle> () |
| 43 | + .Where (style => style != TextStyle.None) |
| 44 | + .ToArray (); |
| 45 | + |
| 46 | + // Draw individual flags on the first line |
| 47 | + foreach (TextStyle style in allStyles) |
| 48 | + { |
| 49 | + string text = Enum.GetName (typeof (TextStyle), style)!; |
| 50 | + int textWidth = text.Length; |
| 51 | + |
| 52 | + // Check if the text fits in the current line |
| 53 | + if (x + textWidth >= maxWidth) |
| 54 | + { |
| 55 | + x = 0; // Move to the next line |
| 56 | + y++; |
| 57 | + } |
| 58 | + |
| 59 | + sendingView.Move (x, y); |
| 60 | + |
| 61 | + var attr = new Attribute (sendingView.GetNormalColor ()) |
| 62 | + { |
| 63 | + TextStyle = style |
| 64 | + }; |
| 65 | + sendingView.SetAttribute (attr); |
| 66 | + sendingView.AddStr (text); |
| 67 | + |
| 68 | + x += textWidth + 2; // Add spacing between entries |
| 69 | + } |
| 70 | + |
| 71 | + // Add a blank line |
| 72 | + y += 2; |
| 73 | + x = 0; |
| 74 | + |
| 75 | + // Generate all combinations of TextStyle (excluding individual flags) |
| 76 | + int totalCombinations = 1 << allStyles.Length; // 2^n combinations |
| 77 | + |
| 78 | + for (var i = 1; i < totalCombinations; i++) // Start from 1 to skip "None" |
| 79 | + { |
| 80 | + var combination = (TextStyle)0; |
| 81 | + List<string> styleNames = new (); |
| 82 | + |
| 83 | + for (var bit = 0; bit < allStyles.Length; bit++) |
| 84 | + { |
| 85 | + if ((i & (1 << bit)) != 0) |
| 86 | + { |
| 87 | + combination |= allStyles [bit]; |
| 88 | + styleNames.Add (Enum.GetName (typeof (TextStyle), allStyles [bit])!); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Skip individual flags |
| 93 | + if (styleNames.Count == 1) |
| 94 | + { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + string text = $"[{string.Join (" | ", styleNames)}]"; |
| 99 | + int textWidth = text.Length; |
| 100 | + |
| 101 | + // Check if the text fits in the current line |
| 102 | + if (x + textWidth >= maxWidth) |
| 103 | + { |
| 104 | + x = 0; // Move to the next line |
| 105 | + y++; |
| 106 | + } |
| 107 | + |
| 108 | + sendingView.Move (x, y); |
| 109 | + |
| 110 | + var attr = new Attribute (sendingView.GetNormalColor ()) |
| 111 | + { |
| 112 | + TextStyle = combination |
| 113 | + }; |
| 114 | + sendingView.SetAttribute (attr); |
| 115 | + sendingView.AddStr (text); |
| 116 | + |
| 117 | + x += textWidth + 2; // Add spacing between entries |
| 118 | + } |
| 119 | + |
| 120 | + args.Cancel = true; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments