|
| 1 | +# Use Toolbar to guide the user |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +With the toolbar you have the possibility to design your command with the use of labels in the right down corner. |
| 6 | + |
| 7 | + |
| 8 | +# Use case 1 - Automatically show the toolbar when the command name is typed by the user |
| 9 | + |
| 10 | +A specific requirement is also that the labels with the values shall have certain colors, not just any default colors. This is how the PowerCommandAttribute look like, to help the user consume this command. |
| 11 | +The look is that one showing in the image above with the fruits, notice that the command name is ```toolbar```. |
| 12 | + |
| 13 | +For this case we will use the attribute ```PowerCommandsToolbar``` attribute like this: |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +You can also see the ```PowerCommandDesign``` attribute, where the suggestions also are added with valid values, if the ```PowerCommandsToolbar``` is omitted the suggestions will be uses as labels instead. |
| 18 | +But in this case I want to show the values surrounded by ```[]``` and with a describing label ```[Pick a valid fruit]``` to really guide the user right. |
| 19 | +If I had added that as suggestions, the user could not us tab to cycle trough valid values. |
| 20 | + |
| 21 | +## The Command class |
| 22 | +For this to work we need to inherit the ```CommandWithToolbarBase``` base class instead of the regular base class ```CommandBase``` class. |
| 23 | +But other than that is is just the ```PowerCommandsToolbar``` attribute that you need to add and the rest will just work. |
| 24 | + |
| 25 | +Notice that the Run function starts with calling the base.Run() function, that is to automatically clear the toolbar. |
| 26 | +If the user input is wrong the toolbar is programmatically called to show the toolbar again. |
| 27 | +``` |
| 28 | +[PowerCommandsToolbar( labels: "[ Pick a valid fruit ->]|[Apple]|[Orange]|[Banana]", |
| 29 | + colors: new [] { ConsoleColor.DarkBlue,ConsoleColor.DarkGreen ,ConsoleColor.Red,ConsoleColor.DarkYellow})] |
| 30 | +[PowerCommandDesign( description: "Demonstration of the usage of a command with a toolbar", |
| 31 | + suggestions: "Apple|Orange|Banana", |
| 32 | + example: "toolbar")] |
| 33 | +public class ToolbarCommand : CommandWithToolbarBase<PowerCommandsConfiguration> |
| 34 | +{ |
| 35 | + public ToolbarCommand(string identifier, PowerCommandsConfiguration configuration) : base(identifier, configuration) { } |
| 36 | +
|
| 37 | + public override RunResult Run() |
| 38 | + { |
| 39 | + base.Run(); |
| 40 | + var fruits = new[] { "Apple", "Orange", "Banana" }; |
| 41 | + var fruit = Input.SingleArgument; |
| 42 | + if(fruits.Any(f => f == fruit))WriteSuccessLine($"You picked {fruit}\nWell done"); |
| 43 | + else |
| 44 | + { |
| 45 | + WriteFailureLine($"{fruit} is not a valid fruit!"); |
| 46 | + DrawToolbar(); |
| 47 | + } |
| 48 | + return Ok(); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +# Use case 2 - Show toolbar with dynamically content |
| 54 | +This use case is a bit more tricky, the command ```openapi``` has this requirements: |
| 55 | +- Needs to first set the working directory to a specific folder that contains a couple of *.yaml files. |
| 56 | +- If the ```openapi``` is typed a check will performed to see if the current working directory is a valid one. |
| 57 | + - if not a label will be displayed to the user with instructions. |
| 58 | + - If it is a valid directory a couple of labels will be shown what to do next. |
| 59 | + |
| 60 | +We inherit the same base class ```CommandWithToolbarBase``` base we are also been using functionality from the ```CdCommand``` so that we could use the current WorkingDirectory. |
| 61 | +We will not use the ```PowerCommandsToolbar``` attribute because the labels are not static, we will instead listen on the user input from the command line and act on that if the typed in command is ```openapi```. |
| 62 | +The base class already listen on this event but we override the behavior and implement a custom one. |
| 63 | + |
| 64 | +Below is the command code, where implementation code skipped, the sample command exist in the source code if you want to use it. It´s actually is implementation code to generate WebApi using OpenAPI specification with their Docker image. |
| 65 | +``` |
| 66 | +[PowerCommandDesign( description: "Generate API with OpenApi Code generator using a docker image, you create two files.\n First one with the API specification, the second one for the config, the filename should contain config so the command knows what is what...\n Navigate to the directory with the files using the cd command.", |
| 67 | + example: "openapi --generate")] |
| 68 | +public class OpenApiCommand : CommandWithToolbarBase<PowerCommandsConfiguration> |
| 69 | +{ |
| 70 | + public OpenApiCommand(string identifier, PowerCommandsConfiguration configuration) : base(identifier, configuration, autoShowToolbar: false) { } |
| 71 | + protected override void ReadLineService_CmdLineTextChanged(object? sender, CmdLineTextChangedArgs e) |
| 72 | + { |
| 73 | + if (e.CmdText.StartsWith(Identifier)) |
| 74 | + { |
| 75 | + if (e.CmdText == Identifier) |
| 76 | + { |
| 77 | + Labels.Clear(); |
| 78 | + if (ValidFilesExists()) |
| 79 | + { |
| 80 | + Labels.Add("Valid files found! ->"); |
| 81 | + Labels.Add("--generate"); |
| 82 | + DrawToolbar(); |
| 83 | + return; |
| 84 | + } |
| 85 | + Labels.Add("Navigate to a directory with the yaml files, using cd command"); |
| 86 | + DrawToolbar(); |
| 87 | + } |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + ClearToolbar(); |
| 92 | + Labels.Clear(); |
| 93 | + } |
| 94 | + } |
| 95 | + public override RunResult Run() |
| 96 | + { |
| 97 | + if(HasOption("generate")) GenerateCode(); |
| 98 | + return Ok(); |
| 99 | + } |
| 100 | + public void GenerateCode() |
| 101 | + { |
| 102 | + //Implementation code excluded |
| 103 | + } |
| 104 | + public bool ValidFilesExists() |
| 105 | + { |
| 106 | + //Implementation code excluded |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | +If the user navigates with ```CdCommand``` to a directory with two *.yaml files this toolbar will be shown. |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +# Use case 3 - Draw toolbar using DialogService |
| 115 | +You could also use ```DialogService``` direct in your command without inherit functionality from the ```CommandWithToolbarBase``` base class. |
| 116 | +``` |
| 117 | +DialogService.DrawToolbar(labels, colors) |
| 118 | +``` |
| 119 | +Read more about: |
| 120 | + |
| 121 | +[Design your Command](Design_command.md) |
| 122 | + |
| 123 | +[Input](Input.md) |
| 124 | + |
| 125 | +[options](options.md) |
| 126 | + |
| 127 | +[Back to start](https://github.com/PowerCommands/PowerCommands2022/blob/main/Docs/README.md) |
0 commit comments