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

makitso-prompt

v3.0.2

Published

a terminal prompt

Downloads

3

Readme

Makitso Prompt

The terminal prompt used by Makitso.

yeh, yeh.. I started with Commander, it calls process.exit() for various reasons which made it difficult to use in a REPL. I then converted Makitso use Inquirer, but it doesn't like promises so much and I needed moar async. Enquirer was better in this respect, but then I got to implementing autocomplete and things got tricky again. I had varying success overriding parts of these modules, but it was harder to bend to them my will than I liked. The inbuilt Node readline module was much the same, so I started from scratch, pulling in some of the complex bits from readline and filling in the gaps.

The essential part of a commandline prompt is being able to act on key presses and modify the output in the terminal accordingly. I've attempted to make this as simple as possible with makitso-prompt by allowing pure functions to be used as key-press processors which modify a state object which is then rendered to the terminal. Much of the logic is also broken into easily replaceable functions for customisation.

Usage

Simple

This will simply echo entered commands

const { Prompt } = require("makitso-prompt");
const prompt = new Prompt();
const command = prompt.start().then(console.log);

Custom prompt

const prompt = new Prompt({ prompt: chalk`{blue aPrompt> }` });
const command = prompt.start().then(console.log);

Include a header above the prompt and a footer below it

const prompt = new Prompt();
const header = "A line above the prompt";
const footer = "A line below the prompt\nAnother line";
const command = prompt.start({ header, footer }).then(console.log);

Add a custom keypress handler

const { keyPressHistory } = require("makitso-prompt");
const prompt = new Prompt();
Object.assign(prompt, { keyPressers: [...prompt.keyPressers, keyPressHistory] });
const command = prompt.start().then(console.log);

Keypress handlers

A keypress handler is an object with a keyPress method.

Keypress handlers keyPress methods are called each time a key is pressed.

Keypress handlers are called in the order of the keypressers array.

The keyPress method is called with the app state object and a press object.

Keypress handlers use the press and/or state objects to decide what, if anything, needs to be changed in the state object. Changes are made using state methods or using the applyPatch function from makitso-prompt/immutably on state.pojo.

const _filter = require("lodash/filter");
const { applyPatch } = require("makitso-prompt");

// available as `keyPressAutocomplete` but you'll likely want to build your own
function AutoComplete(choices) {
  return {
    keyPress: async function(state, press) {
      if (state.mode === "command") {
        let command = state.command;

        const matches = _filter(choices, choice => choice.startsWith(command));

        if (press.key && press.key.name === "tab" && matches.length === 1) {
          state.command = matches[0] + " ";
          state.cursorCols = null;
        } else {
          // state.pojo = applyPatch(state.pojo, { footer: matches.join(" ") });
          state.footer = matches.join(" ");
        }
      }
      return state;
    }
  };
}

const complete = AutoComplete(["abc1", "ab12", "abcdefg", "a123"]);

const prompt = new Prompt();
Object.assign(prompt, { keyPressers: [...prompt.keyPressers, complete] });

const command = prompt.start().then(console.log);