npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@j-o-r/cli

v2.0.3

Published

This project provides a command line interface (CLI) for creating interactive dialogs

Downloads

11

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.

  1. 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();
  1. 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:

  1. There can/should be only ONE command per line
  2. 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

  1. Command Parsing: The CLI now includes a parseCommandLine function that can extract commands from user input.
  2. Key Mappings: You can now register custom key mappings using the registerKeyMappings method.
  3. Role Management: The setRole method allows you to set or overwrite roles, prompts, and colors.
  4. Paste Detection: The CLI now detects rapid key presses, likely indicating a paste event.
  5. 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.