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-badges

v1.5.2

Published

Quirky little node-js library for generating badges for your cli apps.

Downloads

16

Readme

Quirky little node-js library for generating badges for your cli apps.

GitHub file size in bytes npm


Table Of Contents


Getting Started

Installing

As usual, you need to install from npm/yarn:

$ npm install cli-badges

Usage

This is a simple example, using badges to display test results:

const { badge } = require('cli-badges');

const failedBadge  = badge('failed', '2', { theme: 'red' });
const skippedBadge = badge.yellow('skipped', '2');
const successBadge = badge.green('success', '2');

console.log(failedBadge, successBadge, skippedBadge);

The above would output something similar to the terminal:

You could also create a donate badge with a link (if supported):

const donateBadge = badge.blue('❤️ donate', 'ko-fi', {
  link: 'https://ko-fi.com/logginjs',
});

console.log(donateBadge);

You can also only show the label:

const onlyLabel = badge('❤️ donate', '', { labelColor: 169 });

console.log(onlyLabel);
Example output is a mock, console output will vary slightly from terminal to terminal.

Badge Structure

A badge is conformed of a label and a message <label>:<message>. Each segment can be customized, by changing bg color, text color and style.

API

cli-badges exports a method called badge.

export function badge(
  label?: string,
  message?: string,
  options?: {
    labelBg?: string | number;
    messageBg?: string | number;
    labelColor?: string | number;
    messageColor?: string | number;
    labelStyle?: string;
    messageStyle?: string;
    labelWidth?: number;
    messageWidth?: number;
    link?: string;
    forceLink?: boolean;
    theme?: string;
    swapTheme?: boolean;
  }
): string;

Available Options

| Option | Description | Type | Default | | :------------- | :---------------------------------------------------------------------------------------- | :--------------- | :--------------- | | messageBg | Background color for the label | string or number | blue | | labelBg | Background color for the message | string or number | blackBright | | messageColor | Text color for the message | string or number | white | | labelColor | Text color for the label | string or number | white | | labelWidth | Width of the label | number | label length + 2 | | messageWidth | Width of the message | number | label length + 2 | | labelStyle | Style for the label text | string | null | | messageStyle | Style for the label text | string | null | | link | Add a link when a badge is clicked (only works in some terminals, see this) | URL | null | | forceLink | Force adding link even if not supported | boolean | false | | theme | Theme to be used, see all themes | string | blue | | swapTheme | Swap the theme, this means properties from label will be aplied to message and vice versa | boolean | false |

Colors

cli-badges uses cli-color internally for managing colors, you can check the list of available colors there. Take into account that when setting a color you don't need to pass the prefix bg, it's handled for you. ie: blackBright instead of bgBlackBright

Xterm colors

There are more colors available using xterm colors, see cli-color xterm colors for the complete list of available colors.

Not supported on Windows and some terminals. However if used in not supported environment, the closest color from basic (16 colors) palette is chosen.

Styles

cli-badges uses cli-color internally for managing styles, you can check the list of available styles there.

Styles will display correctly if font used in your console supports them.

Links

You can output badges with a link attached to it, that can be clicked in some terminals.

⚠︎ cli-badges will only output link if its supported by your terminal.

See this for information on supported terminals

badge('with', 'link', { link: 'https://link.com' });

Themes

Themes are a way to store badge configuration for repeated use. All the options (except for the theme option, obviously) that are needed by the badge can be stored by making a theme.

The library comes with a set of inbuilt themes:

Inbuilt Themes

  • red : Red Message Background
  • green : Green Message Background
  • blue : Blue Message Background
  • yellow : Black Colored Message on Yellow Background
  • cyan : Black Colored Message on Cyan Background
  • magenta : Black Colored Message on Magenta Background
  • success : ('Success') Message on Green Background
  • failed : ('Failed') Message on Red Background

Using Themes

You can use the themes in various ways, passing an option theme to badge:

badge('label', 'green', { theme: 'green' });
badge('label', 'magenta', { theme: 'magenta', swapTheme: true });

Or there are helper methods for ease of use:

badge.green('label', 'green');
badge.failed('theme', 'red');

Options present in the theme will override options passed. Missing options will have default values.

Adding a theme

You can also add you own themes:

badge.addTheme('donate', {
  label: '❤️ donate',
});

badge('', 'ko-fi', { theme: 'donate' });
badge.donate('', 'ko-fi');

You can also send in a PR and suggest a new inbuilt theme :)

Swap Properties

You can also swap all themes, this means properties from label will be aplied to message and vice versa.

badge.failed('theme', 'red');
badge.failed.swap('theme', 'red');

You can check the complete list of themes here.

Other Libraries?

cli-badges is also available in other languages:

Test Coverage

| Statements | Branches | Functions | Lines | | --------------------------------------------------------------------------- | ------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------- | | Statements | Branches | Functions | Lines |

Support the project

I tend to open source anything I can, and love to help people that need help with the project.

However, if you are using this project and are happy with it or just want to encourage me to continue creating stuff, there are few ways you can do so:

  • Starring and sharing the project 🚀
  • Reporting bugs 🐛
  • Sending feedback
  • Or even coding :P

Thanks! ❤️


Contributions are very welcomed 🥰