Skip to content

Commit bdcb2f1

Browse files
committed
feat: add retry/language plugins, update menu logic and documentation
1 parent 5d7c833 commit bdcb2f1

File tree

5 files changed

+249
-94
lines changed

5 files changed

+249
-94
lines changed

README.md

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,37 @@ The library provides two main methods for output:
3535

3636
- `print()`: Initializes and displays the menu interface
3737
- `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+
```
4243

4344
## Creating and Registering Plugins
4445

4546
To create a new plugin, define an object implementing the `PluginType` interface:
4647

4748
```typescript
48-
import { print, addPlugin } from 'modular-cli-menu';
49+
import { addPlugin, print } from 'modular-cli-menu';
4950

5051
addPlugin({
5152
menu: {
5253
name: 'test',
53-
parent: "main",
54+
parent: 'main',
5455
choices: [
5556
'menu.test.action1',
5657
'menu.action.back'
5758
]
58-
},
59+
},
5960
actions: {
6061
'menu.test.action1': {
6162
type: 'function',
6263
name: 'menu.test.action1',
6364
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+
}
6569
}
6670
}
6771
},
@@ -77,18 +81,18 @@ addPlugin({
7781
'menu.test.action1.message': 'Qualcosa',
7882
}
7983
}
80-
})
84+
});
8185

8286
print();
8387
```
8488

85-
In the main file remember to register the plugin
89+
In your main file, remember to register the plugin:
90+
8691
```typescript
87-
import module1 from './plugins/myPlugin/index.ts';
92+
import myPlugin from './plugins/myPlugin';
8893
import { addPlugin, print } from 'modular-cli-menu';
8994

9095
addPlugin(myPlugin);
91-
9296
print();
9397
```
9498

@@ -98,11 +102,10 @@ The library provides several methods for menu navigation and control:
98102

99103
### actionExit
100104

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:
103106

104107
```typescript
105-
import { actionExit } from 'modular-cli-menu';
108+
import { actionExit, PluginType } from 'modular-cli-menu';
106109

107110
const myPlugin: PluginType = {
108111
menu: {
@@ -118,11 +121,10 @@ const myPlugin: PluginType = {
118121

119122
### actionGoBack
120123

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:
123125

124126
```typescript
125-
import { actionGoBack } from 'modular-cli-menu';
127+
import { actionGoBack, PluginType } from 'modular-cli-menu';
126128

127129
const myPlugin: PluginType = {
128130
menu: {
@@ -141,16 +143,17 @@ const myPlugin: PluginType = {
141143
Use `waitForKeyPress()` to pause execution until the user presses a key:
142144

143145
```typescript
144-
import { waitForKeyPress } from 'modular-cli-menu';
146+
import { waitForKeyPress, PluginType } from 'modular-cli-menu';
145147

146-
const myAction: ActionType = {
148+
const myAction: PluginType = {
147149
type: 'function',
150+
name: 'myAction',
148151
options: {
149-
callback: async (parent?:) => {
152+
callback: async (parent) => {
150153
await waitForKeyPress();
151154
// Continue with the next actions
152-
}
153-
}
155+
},
156+
},
154157
};
155158
```
156159

@@ -208,6 +211,10 @@ The library supports different types of actions:
208211
type: 'checkbox',
209212
name: 'checkboxAction',
210213
options: {
214+
message: {
215+
text: 'Select options:',
216+
color: 'cyan',
217+
},
211218
choices: ['Option 1', 'Option 2', 'Option 3'],
212219
callback: async (selected) => {
213220
console.log('Selected options:', selected);
@@ -222,12 +229,61 @@ The library supports different types of actions:
222229
{
223230
type: 'goto',
224231
name: 'gotoAction',
232+
color: 'blue',
225233
options: {
226-
menu: 'targetMenuName'
227-
}
234+
callback: async (to) => {
235+
// Custom logic before navigating
236+
},
237+
},
228238
}
229239
```
230240

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+
231287
## Support me
232288
233289
[Share with me a cup of tea](https://www.buymeacoffee.com/bloris) ☕

src/languages/en.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,5 @@
77
"menu.action.back.label": "Back",
88
"menu.action.exit.label": "Exit",
99
"menu.action.exit.message": "Exiting the application",
10-
"menu.main.action.waiting.label": "Press any key to continue...",
11-
"menu.language.question": "Change language",
12-
"menu.language.action.it.message": "Lingua italiana selezionata con successo",
13-
"menu.language.action.en.message": "English language selected successfully",
14-
"menu.language.action.it.label": "Italian",
15-
"menu.language.action.en.label": "English"
10+
"menu.main.action.waiting.label": "Press any key to continue..."
1611
}

src/languages/it.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,5 @@
77
"menu.action.back.label": "Torna indietro",
88
"menu.action.exit.label": "Esci",
99
"menu.action.exit.message": "Uscita dall'applicazione",
10-
"menu.main.action.waiting.label": "Premi un tasto per continuare...",
11-
"menu.language.question": "Cambio lingua",
12-
"menu.language.action.it.message": "Lingua italiana selezionata con successo",
13-
"menu.language.action.en.message": "English language selected successfully",
14-
"menu.language.action.it.label": "Italiano",
15-
"menu.language.action.en.label": "Inglese"
10+
"menu.main.action.waiting.label": "Premi un tasto per continuare..."
1611
}

0 commit comments

Comments
 (0)