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

cli-menu

v1.0.0

Published

Organize your CLI app around menu selections

Downloads

7

Readme

Organize your CLI app around menu selections.

This function displays a menu on the screen, waits for the user to type a key to make a selection, and then calls the function for the selected menu item.

Example

const menu = require(`./index.js`)

menu({
	menu_items: [{
		key: `l`,
		name: `lol`,
		action: () => console.log(`Yeah, laugh it up!`),
	}, {
		key: `t`,
		name: `Tell me the time`,
		action: async() => {
			console.log(`The time is`, new Date().toLocaleTimeString())
			await new Promise(resolve => setTimeout(resolve, 1500))
			console.log(`...and now it's`, new Date().toLocaleTimeString())
		},
	}, {
		key: `m`,
		name: `Check out a different menu`,
		action: () => menu({
			title: `BEHOLD THE SUBMENU`,
			menu_items: [{
				key: `j`,
				name: `Tell me a joke`,
				action: () => console.log(`This library has no eunuch testes`),
			}, {
				key: `b`,
				name: `Go back`,
				action: ({ back }) => back(),
			}],
		}),
	}, {
		key: `q`,
		name: `quit`,
		action: ({ back }) => back(),
	}],
}).then(() => {
	console.log(`👋`)
})

example screenshot

API

The menu function takes an object with three properties.

menu({ title = `Menu`, menu_items, display_menu = default_display_menu })
  • title: A label to display above the menu items
  • menu_items: an array of objects representing the menu items
    • key: a single-character string representing the key the user will press to select the item
    • name: the string to display for the menu item
    • action: the function to call when the user selects the menu item
  • display_menu: optional function that will be called when the menu needs to be displayed

The menu function returns a promise that will resolve once the user backs out of the menu.

action({ back })

The action function may return a promise.

Once the promise resolves, the menu will be displayed again.

back is a function. If you call it, the menu promise will resolve after the action function finishes.

Whatever value the action function returns will be used as the resolve value for the menu's promise.

display_menu({ title, menu_items })

Defaults to:

const menu_width = 80
const default_display_menu = ({ title, menu_items }) => {
	if (title.length <= menu_width - 4) {
		const title_with_spaces = ` ` + title + ` `
		const dashes_on_left = Math.floor((menu_width - title_with_spaces.length) / 2)
		const title_with_dashes_on_left = title_with_spaces.padStart(title_with_spaces.length + dashes_on_left, `-`)
		const display_title = title_with_dashes_on_left.padEnd(menu_width, `-`)
		console.log(display_title)
	} else {
		console.log(title)
	}
	menu_items.forEach(
		({ key, name }) => console.log(key, `:`, name),
	)
}

License

WTFPL