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

menu-string

v1.3.0

Published

Generate a menu with selectable menu items as a string

Downloads

145

Readme

menu-string

Generate a menu with selectable menu items as a string.

npm Build status js-standard-style

Installation

npm install menu-string --save

Usage

const Menu = require('menu-string')

const menu = new Menu({
  items: [
    {text: 'Item 1'},
    {text: 'Item 2'},
    {text: '------', separator: true},
    {text: 'Item 3'}
  ],
  selected: 0
})

console.log(menu.toString())
// > Item 1
//   Item 2
//   ------
//   Item 3

// Move cursor one item down
menu.down()

console.log(menu.toString())
//   Item 1
// > Item 2
//   ------
//   Item 3

API

menu = new Menu(options)

Initialize a new menu object.

Options:

  • items - An array of item objects
  • render - An optional render function that will be called for each menu item that is rendered. Will be called with the item object that should be rendered and a boolean indicating if the item is currectly selected. Should return a rendered string representing the menu item. The item object will have an index property indicating its position in the menu
  • selected - An optional integer specifying which menu item should be selected by default (defaults to 0)
  • height - Set menu max height (in rows). If set, the menu will maintain a viewport, only ever rendering that amout of menu items. When moving the cursor up or down, the viewport will scroll to accommodate.

An item object can have the following properties:

  • text - The text to show when rendering the item
  • separator - A boolean indicating if the item can be selected
  • marked - A boolean indicating if the item should me "marked". This property can be toggled using item.toggleMark(). It's up the the implementer to render marked items differently.

An item can also just be a string, which is treated as {text: item}.

If no custom render function is provided, the following default render function is used:

function (item, selected) {
  return (selected ? '> ' : '  ') + item.text
}

menu = new Menu(items)

An alias for:

new Menu({
  items: items
})

Event: update

An update event is emitted when ever the menu is updated - i.e. the cursor is moved.

menu.items

Array of menu items.

success = menu.up()

Move the cursor one item up. Separator items will be skipped.

Returns false if top of menu have been reached. Returns true otherwise.

success = menu.down()

Move the cursor one item down. Separator items will be skipped.

Returns false if bottom of menu have been reached. Returns true otherwise.

menu.select(index)

Select a specific menu item.

Returns false if the index was invalid. Returns true otherwise.

item = menu.selected()

Returns the selected item object. The returned item object will have an index property indicating its position in the menu. Returns null if no selectable menu item exists.

menu.toggleMark()

Toggle the marked boolean of the currently selected item.

str = menu.toString()

Returns the entire menu as a single string. One line for each menu item rendered by the render function.

License

MIT