@@ -35,33 +35,37 @@ The library provides two main methods for output:
35
35
36
36
- ` print() ` : Initializes and displays the menu interface
37
37
- ` write(message: string, color?: ColorName) ` : Prints custom messages with optional styling
38
- ``` typescript
39
- write (' Success!' , ' green' );
40
- write (' Error!' , ' red' );
41
- ```
38
+
39
+ ``` typescript
40
+ write (' Success!' , ' green' );
41
+ write (' Error!' , ' red' );
42
+ ```
42
43
43
44
## Creating and Registering Plugins
44
45
45
46
To create a new plugin, define an object implementing the ` PluginType ` interface:
46
47
47
48
``` typescript
48
- import { print , addPlugin } from ' modular-cli-menu' ;
49
+ import { addPlugin , print } from ' modular-cli-menu' ;
49
50
50
51
addPlugin ({
51
52
menu: {
52
53
name: ' test' ,
53
- parent: " main" ,
54
+ parent: ' main' ,
54
55
choices: [
55
56
' menu.test.action1' ,
56
57
' menu.action.back'
57
58
]
58
- },
59
+ },
59
60
actions: {
60
61
' menu.test.action1' : {
61
62
type: ' function' ,
62
63
name: ' menu.test.action1' ,
63
64
options: {
64
- message: { text: ' menu.test.action1.message' }
65
+ message: { text: ' menu.test.action1.message' , color: ' green' },
66
+ callback : async () => {
67
+ // Your logic here
68
+ }
65
69
}
66
70
}
67
71
},
@@ -77,18 +81,18 @@ addPlugin({
77
81
' menu.test.action1.message' : ' Qualcosa' ,
78
82
}
79
83
}
80
- })
84
+ });
81
85
82
86
print ();
83
87
```
84
88
85
- In the main file remember to register the plugin
89
+ In your main file, remember to register the plugin:
90
+
86
91
``` typescript
87
- import module1 from ' ./plugins/myPlugin/index.ts ' ;
92
+ import myPlugin from ' ./plugins/myPlugin' ;
88
93
import { addPlugin , print } from ' modular-cli-menu' ;
89
94
90
95
addPlugin (myPlugin );
91
-
92
96
print ();
93
97
```
94
98
@@ -98,11 +102,10 @@ The library provides several methods for menu navigation and control:
98
102
99
103
### actionExit
100
104
101
- ` menu.action.exit ` can be used to terminate the application.
102
- Add it to your menu's choices:
105
+ ` menu.action.exit ` can be used to terminate the application. Add it to your menu's choices:
103
106
104
107
``` typescript
105
- import { actionExit } from ' modular-cli-menu' ;
108
+ import { actionExit , PluginType } from ' modular-cli-menu' ;
106
109
107
110
const myPlugin: PluginType = {
108
111
menu: {
@@ -118,11 +121,10 @@ const myPlugin: PluginType = {
118
121
119
122
### actionGoBack
120
123
121
- ` menu.action.back ` can be used to navigate to the previous menu.
122
- Add it to your menu's choices:
124
+ ` menu.action.back ` can be used to navigate to the previous menu. Add it to your menu's choices:
123
125
124
126
``` typescript
125
- import { actionGoBack } from ' modular-cli-menu' ;
127
+ import { actionGoBack , PluginType } from ' modular-cli-menu' ;
126
128
127
129
const myPlugin: PluginType = {
128
130
menu: {
@@ -141,16 +143,17 @@ const myPlugin: PluginType = {
141
143
Use ` waitForKeyPress() ` to pause execution until the user presses a key:
142
144
143
145
``` typescript
144
- import { waitForKeyPress } from ' modular-cli-menu' ;
146
+ import { waitForKeyPress , PluginType } from ' modular-cli-menu' ;
145
147
146
- const myAction: ActionType = {
148
+ const myAction: PluginType = {
147
149
type: ' function' ,
150
+ name: ' myAction' ,
148
151
options: {
149
- callback : async (parent ? : ) => {
152
+ callback : async (parent ) => {
150
153
await waitForKeyPress ();
151
154
// Continue with the next actions
152
- }
153
- }
155
+ },
156
+ },
154
157
};
155
158
```
156
159
@@ -208,6 +211,10 @@ The library supports different types of actions:
208
211
type : ' checkbox' ,
209
212
name : ' checkboxAction' ,
210
213
options : {
214
+ message : {
215
+ text : ' Select options:' ,
216
+ color : ' cyan' ,
217
+ },
211
218
choices : [' Option 1' , ' Option 2' , ' Option 3' ],
212
219
callback : async (selected ) => {
213
220
console .log (' Selected options:' , selected );
@@ -222,12 +229,61 @@ The library supports different types of actions:
222
229
{
223
230
type : ' goto' ,
224
231
name : ' gotoAction' ,
232
+ color : ' blue' ,
225
233
options : {
226
- menu : ' targetMenuName'
227
- }
234
+ callback : async (to ) => {
235
+ // Custom logic before navigating
236
+ },
237
+ },
228
238
}
229
239
```
230
240
241
+ ## Built-in Plugins: Retry and Language
242
+
243
+ The library includes some built-in plugins, such as ` retry ` and ` language ` , to enhance user experience and internationalization out of the box.
244
+
245
+ ### Retry Plugin
246
+
247
+ The ` retry ` plugin provides a menu for handling invalid input or failed actions, allowing users to easily retry an operation.
248
+
249
+ Example structure:
250
+
251
+ ``` typescript
252
+ addActionToMenu ({
253
+ type: ' input' ,
254
+ name: ' menu.foo.action' ,
255
+ options: {
256
+ message: { text: ' menu.foo.action.message' },
257
+ callback : async (value : string ) => {
258
+ if (value .length > 0 && fs .existsSync (value )) {
259
+ options .foo = value ;
260
+ await waitForKeyPress ({ message: ' menu.foo.action.callback.success' , color: ' green' });
261
+ await print (' main' );
262
+ } else {
263
+ await print (' retry' , {
264
+ action: ' menu.foo.action' ,
265
+ message: ' menu.foo.action.callback.error' ,
266
+ color: ' red'
267
+ });
268
+ }
269
+ }
270
+ }
271
+ });
272
+ ```
273
+
274
+ You can use this plugin to prompt the user to retry an action after an error or invalid input.
275
+
276
+ ### Language Plugin
277
+
278
+ The ` language ` plugin allows users to switch the interface language at runtime. It updates the ` MENU_LANGUAGE ` environment variable and reloads translations.
279
+ ```
280
+
281
+ These plugins are registered automatically, but you can customize or extend them as needed.
282
+
283
+ ## Internationalization (i18n)
284
+
285
+ You can provide translations for your plugin by adding a `languages` property to your plugin object. The library will automatically merge these translations and use the correct language based on the current environment.
286
+
231
287
## Support me
232
288
233
289
[Share with me a cup of tea](https://www.buymeacoffee.com/bloris) ☕
0 commit comments