@j-o-r/cli
v2.0.3
Published
This project provides a command line interface (CLI) for creating interactive dialogs
Downloads
11
Maintainers
Readme
Command Line Interface (CLI) for Dialogs
This project provides a command line interface (CLI) for creating interactive dialogs. It includes functionalities for handling user input, asking questions, and processing commands. The CLI is designed to be flexible and easy to integrate into various applications.
Features
- Interactive Questions: Ask questions and get responses from the user.
- Role-Based Prompts: Different prompts and colors based on the role (e.g., user, system, assistant).
- Error Handling: Log and display errors.
- Spinner: Display a spinner for long-running operations.
- Command Parsing: Parse and handle custom commands within user input.
- Key Mappings: Register custom key mappings for special actions.
Installation
To use this CLI, you need to have Node.js installed:
npm install @j-o-r/cli
Usage
Basic Example
Here is a simple example demonstrating how to use the CLI to ask questions and handle commands.
- Create a CLI Handler: Create a file named
cli_questions.js
and set up the CLI handler.
#!/usr/bin/env node
import cli from '@j-o-r/cli'; // Adjust the path as necessary
cli.inputHandler = async (s) => {
if (!s) {
cli.focus();
return;
}
cli.focus('log');
cli.write(`user typed: ${s}`);
const name = await cli.question('What is your name?');
let res = await cli.yesNo(`${name}, are you okay?`);
cli.focus('assistant');
cli.write(res ? 'yes!!' : 'no!!!');
let selected = await cli.select('Why?', ['Nice weather', 'Eggs and bacon']);
if (selected) {
cli.write(`Aaah, Because of ${selected}`);
} else {
cli.write(`This option wasn't there`);
}
cli.focus();
};
cli.exitHandler = async (code = 0) => {
cli.log('CTRL - c :user wants to exit');
process.exit(code);
};
cli.focus('log');
cli.write('Just type something');
cli.focus();
- Run the CLI: Make the script executable and run it.
chmod +x cli_questions.js
./cli_questions.js
Commands
The syntax to distinguish 'user' input from commands is indicated by '>' and '<' characters.
Example: Add a folder listing to the user prompt
This is normal text >#!bash ls -Fla< this the rest of the sentence
Example: add the clipboard content to user prompt
What is this text about: >paste<
Edit the user prompt directly in ENV.editor (or vim)
Edit this >edit< text
Keep in mind:
- There can/should be only ONE command per line
- Multi-line input cannot contain commands; the command parsing for multi-line strings is switched off for now.
Implement the execution of commands via the input handler. (example to follow)
API
Cli
clearLine()
: Clear the current line and prompt.clearPreviousLine()
: Clear the previous line.focus(role)
: Set the focus to a specific role.write(message)
: Write a message to the console.error(err)
: Write an error message to the console.log(any)
: Log a message.clear()
: Clear the screen.startSpinner()
: Start the spinner.stopSpinner()
: Stop the spinner.question(question)
: Ask a question and resolve the answer.yesNo(question)
: Ask a yes/no question and resolve the answer.select(question, choices)
: Ask a selection question and resolve the answer.set inputHandler(handler)
: Set the input handler.set exitHandler(handler)
: Set the exit handler.set input(str)
: Call the input handler directly.exit(code)
: Call the exit handler directly with an optional exit code.registerKeyMappings(mappings)
: Register custom key mappings.setRole(role, prompt, color)
: Set or overwrite a role, prompt, and color.get roles()
: Get the available roles.
New Features and Changes
- Command Parsing: The CLI now includes a
parseCommandLine
function that can extract commands from user input. - Key Mappings: You can now register custom key mappings using the
registerKeyMappings
method. - Role Management: The
setRole
method allows you to set or overwrite roles, prompts, and colors. - Paste Detection: The CLI now detects rapid key presses, likely indicating a paste event.
- Error Handling: Improved error handling for uncaught exceptions and unhandled promise rejections.
License
This project is licensed under the APACHE 2.0 License. See the LICENSE file for details.