|
1 |
| -# Modular CLI Menu System |
| 1 | +# Modular CLI Menu |
2 | 2 |
|
3 |
| -A flexible and extensible command-line interface menu system built with TypeScript, designed to be easily extendable through plugins. |
| 3 | +A TypeScript library for creating modular and interactive CLI menus with plugin support and internationalization. |
4 | 4 |
|
5 |
| -## Overview |
| 5 | +## Installation |
6 | 6 |
|
7 |
| -This project provides a modular framework for creating interactive CLI menus with support for: |
| 7 | +```bash |
| 8 | +npm install modular-cli-menu |
| 9 | +``` |
8 | 10 |
|
9 |
| -- Multi-language support (currently English and Italian) |
10 |
| -- Plugin-based architecture for easy extension |
11 |
| -- Hierarchical menu navigation |
12 |
| -- Environment configuration |
| 11 | +## Key Features |
13 | 12 |
|
14 |
| -## Features |
| 13 | +- 🔌 Modular plugin system |
| 14 | +- 🌍 Multi-language support (i18n) |
| 15 | +- 🎨 Customizable menu colors |
| 16 | +- 🔄 Hierarchical menu navigation |
| 17 | +- ⌨️ Interactive input with different question types |
| 18 | +- 📦 Easily extensible |
15 | 19 |
|
16 |
| -- **Plugin System**: Extend functionality by adding new plugins with their own menus and actions |
17 |
| -- **Internationalization**: Full multi-language support with automatic translation loading |
18 |
| -- **Configurable Navigation**: Define how users navigate through menus after actions complete |
19 |
| -- **Dynamic Menu Loading**: Menus and actions are loaded dynamically at runtime |
| 20 | +## Basic Usage |
20 | 21 |
|
21 |
| -## Project Structure |
| 22 | +```typescript |
| 23 | +import { print, write } from 'modular-cli-menu'; |
22 | 24 |
|
| 25 | +// Start the main menu |
| 26 | +print(); |
| 27 | + |
| 28 | +// Use write for custom messages |
| 29 | +write('Hello World', 'green'); |
23 | 30 | ```
|
24 |
| -. |
25 |
| -├── languages/ # Global translations |
26 |
| -│ ├── en.json # English translations |
27 |
| -│ └── it.json # Italian translations |
28 |
| -├── plugins/ # Plugin modules |
29 |
| -│ ├── settings/ # Settings plugin |
30 |
| -│ └── show-info/ # Information display plugin |
31 |
| -├── src/ |
32 |
| -│ ├── actions/ # Core actions |
33 |
| -│ ├── classes/ # Core classes (Menu, MenuItem, etc.) |
34 |
| -│ ├── interfaces/ # TypeScript interfaces |
35 |
| -│ ├── utils/ # Utility functions |
36 |
| -│ └── index.ts # Application entry point |
37 |
| -├── .env # Default environment configuration |
38 |
| -└── .env.local # Local environment overrides |
| 31 | + |
| 32 | +### Using print and write Methods |
| 33 | + |
| 34 | +The library provides two main methods for output: |
| 35 | + |
| 36 | +- `print()`: Initializes and displays the menu interface |
| 37 | +- `write(message: string, color?: ColorName)`: Prints custom messages with optional styling |
| 38 | + ```typescript |
| 39 | + write('Success!', 'green'); |
| 40 | + write('Error!', 'red'); |
| 41 | + ``` |
| 42 | + |
| 43 | +## Creating and Registering Plugins |
| 44 | + |
| 45 | +To create a new plugin, define an object implementing the `PluginType` interface: |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { addPlugin } from 'modular-cli-menu'; |
| 49 | +import type { PluginType, ActionType } from 'modular-cli-menu'; |
| 50 | + |
| 51 | +const myPlugin: PluginType = { |
| 52 | + menu: { |
| 53 | + name: 'myplugin', // Unique menu name |
| 54 | + parent: 'main', // Parent menu (optional) |
| 55 | + color: 'blue', // Menu color (optional) |
| 56 | + choices: [ // Array of available actions |
| 57 | + 'menu.myplugin.action1', |
| 58 | + 'menu.myplugin.action2', |
| 59 | + 'menu.action.back' |
| 60 | + ] |
| 61 | + }, |
| 62 | + actions: { |
| 63 | + 'menu.myplugin.action1': { |
| 64 | + type: 'function', |
| 65 | + name: 'menu.myplugin.action1', |
| 66 | + color: 'green', |
| 67 | + options: { |
| 68 | + message: { |
| 69 | + text: 'Action 1 executed!', |
| 70 | + color: 'green' |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + languages: { |
| 76 | + en: { |
| 77 | + 'menu.myplugin.question': 'My Plugin', |
| 78 | + 'menu.myplugin.action1': 'Execute Action 1', |
| 79 | + 'menu.myplugin.action2': 'Execute Action 2' |
| 80 | + } |
| 81 | + } |
| 82 | +}; |
39 | 83 | ```
|
40 | 84 |
|
41 |
| -## How It Works |
| 85 | +In the main file remember to register the plugin |
| 86 | +```typescript |
| 87 | +import module1 from './plugins/myPlugin/index.ts'; |
| 88 | +import { addPlugin, print } from 'modular-cli-menu'; |
42 | 89 |
|
43 |
| -The application loads menus from configuration files (`menu.json`) found in both the core application and plugins. Each menu item can either lead to a submenu or execute an action. |
| 90 | +addPlugin(myPlugin); |
44 | 91 |
|
45 |
| -### Environment Variables |
| 92 | +print(); |
| 93 | +``` |
46 | 94 |
|
47 |
| -- `LANGUAGE`: Sets the default language (e.g., "en" or "it") |
48 |
| -- `DEBUG_LOG`: When set to "true", enables detailed logging |
| 95 | +## Navigation and Control Methods |
49 | 96 |
|
50 |
| -## Adding a New Plugin |
| 97 | +The library provides several methods for menu navigation and control: |
51 | 98 |
|
52 |
| -To add a new plugin: |
| 99 | +### actionExit |
53 | 100 |
|
54 |
| -1. Create a new directory under `/plugins/` |
55 |
| -2. Add a `menu.json` file defining menu structure |
56 |
| -3. Add language files under `/languages/` directory |
57 |
| -4. Implement actions as TypeScript files in an `actions/` subdirectory |
| 101 | +`menu.action.exit` can be used to terminate the application. |
| 102 | +Add it to your menu's choices: |
58 | 103 |
|
59 |
| -## Getting Started |
| 104 | +```typescript |
| 105 | +import { actionExit } from 'modular-cli-menu'; |
60 | 106 |
|
61 |
| -### Prerequisites |
| 107 | +const myPlugin: PluginType = { |
| 108 | + menu: { |
| 109 | + name: 'myplugin', |
| 110 | + choices: [ |
| 111 | + 'menu.myplugin.action1', |
| 112 | + 'menu.action.exit' // This will add an exit option to your menu |
| 113 | + ] |
| 114 | + }, |
| 115 | + // ... rest of the plugin configuration |
| 116 | +}; |
| 117 | +``` |
62 | 118 |
|
63 |
| -- Node.js (v14 or higher) |
64 |
| -- npm or yarn |
| 119 | +### actionGoBack |
65 | 120 |
|
66 |
| -### Installation |
| 121 | +`menu.action.back` can be used to navigate to the previous menu. |
| 122 | +Add it to your menu's choices: |
67 | 123 |
|
68 |
| -```bash |
69 |
| -# Clone the repository |
70 |
| -git clone https://github.com/BlorisL/modular-cli-menu.git |
| 124 | +```typescript |
| 125 | +import { actionGoBack } from 'modular-cli-menu'; |
71 | 126 |
|
72 |
| -# Navigate to project directory |
73 |
| -cd modular-cli-menu |
| 127 | +const myPlugin: PluginType = { |
| 128 | + menu: { |
| 129 | + name: 'myplugin', |
| 130 | + choices: [ |
| 131 | + 'menu.myplugin.action1', |
| 132 | + 'menu.action.back' // This will add a "back" option to your menu |
| 133 | + ] |
| 134 | + }, |
| 135 | + // ... rest of the plugin configuration |
| 136 | +}; |
| 137 | +``` |
74 | 138 |
|
75 |
| -# Install dependencies |
76 |
| -npm install |
| 139 | +### waitForKeyPress |
77 | 140 |
|
78 |
| -# Start the application |
79 |
| -npm start |
| 141 | +Use `waitForKeyPress()` to pause execution until the user presses a key: |
| 142 | + |
| 143 | +```typescript |
| 144 | +import { waitForKeyPress } from 'modular-cli-menu'; |
| 145 | + |
| 146 | +const myAction: ActionType = { |
| 147 | + type: 'function', |
| 148 | + options: { |
| 149 | + callback: async (parent?:) => { |
| 150 | + await waitForKeyPress(); |
| 151 | + // Continue with the next actions |
| 152 | + } |
| 153 | + } |
| 154 | +}; |
80 | 155 | ```
|
81 | 156 |
|
82 |
| -## Configuration |
| 157 | +## Action Types |
| 158 | + |
| 159 | +The library supports different types of actions: |
| 160 | + |
| 161 | +1. **function**: Executes a callback function |
| 162 | +2. **input**: Requests user input |
| 163 | +3. **checkbox**: Allows multiple selections |
| 164 | +4. **goto**: Navigates to another menu |
| 165 | + |
| 166 | +### Function Action Example |
| 167 | + |
| 168 | +```typescript |
| 169 | +{ |
| 170 | + type: 'function', |
| 171 | + name: 'myAction', |
| 172 | + color: 'green', |
| 173 | + options: { |
| 174 | + message: { |
| 175 | + text: 'Action executed!', |
| 176 | + color: 'green' |
| 177 | + }, |
| 178 | + callback: async () => { |
| 179 | + // Your logic here |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | +``` |
83 | 184 |
|
84 |
| -You can customize the application by creating a `.env.local` file based on the `.env.example` template. |
| 185 | +### Input Action Example |
| 186 | + |
| 187 | +```typescript |
| 188 | +{ |
| 189 | + type: 'input', |
| 190 | + name: 'inputAction', |
| 191 | + color: 'yellow', |
| 192 | + options: { |
| 193 | + message: { |
| 194 | + text: 'Enter a value:', |
| 195 | + color: 'yellow' |
| 196 | + }, |
| 197 | + callback: async (value, parent) => { |
| 198 | + console.log(`Entered value: ${value}`); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | +``` |
85 | 203 |
|
86 |
| -## License |
| 204 | +### Checkbox Action Example |
| 205 | + |
| 206 | +```typescript |
| 207 | +{ |
| 208 | + type: 'checkbox', |
| 209 | + name: 'checkboxAction', |
| 210 | + options: { |
| 211 | + choices: ['Option 1', 'Option 2', 'Option 3'], |
| 212 | + callback: async (selected) => { |
| 213 | + console.log('Selected options:', selected); |
| 214 | + } |
| 215 | + } |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +### Goto Action Example |
87 | 220 |
|
88 |
| -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 221 | +```typescript |
| 222 | +{ |
| 223 | + type: 'goto', |
| 224 | + name: 'gotoAction', |
| 225 | + options: { |
| 226 | + menu: 'targetMenuName' |
| 227 | + } |
| 228 | +} |
| 229 | +``` |
89 | 230 |
|
90 | 231 | ## Support me
|
91 | 232 |
|
|
0 commit comments