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

ansi-fragments

v0.2.1

Published

A tiny library with builders to help making logs/CLI pretty with a nice DX.

Downloads

6,814,309

Readme

ansi-fragments

Version

PRs Welcome MIT License Chat Code of Conduct

A tiny library with builders to help making logs/CLI pretty with a nice DX.

Installation

yarn add ansi-fragments

Usage

import { color, modifier, pad, container } from 'ansi-fragments';

const prettyLog = (level, message) => container(
  color('green', modifier('italic', level)),
  pad(1),
  message
).build();

console.log(prettyLog('success', 'Yay!'));

API

Each fragment implements IFragment interface:

interface IFragment {
  build(): string;
}

The build method is responsible for traversing the tree of fragments and create a string representation with ANSI escape codes.

color

color(
  ansiColor: AnsiColor,
  ...children: Array<string | IFragment>
): IFragment

Creates fragment for standard ANSI colors.

color('red', 'Oh no');
color('bgBlue', color('brightBlue', 'Hey'));
color('green', modifier('bold', 'Sup!'));

modifier

modifier(
  ansiModifier: AnsiModifier,
  ...children: Array<string | IFragment>
): IFragment

Creates fragment for standard ANSI modifiers: dim, bold, hidden, italic, underline, strikethrough.

modifier('underline', 'Hello', 'World');
modifier('italic', modifier('bold', 'Hey'));
modifier('bold', color('green', 'Sup!'));

container

container(...children: Array<string | IFragment>): IFragment

Creates fragment, which sole purpose is to hold and build nested fragments.

container(
  color('gray', '[08/01/18 12:00]'),
  pad(1),
  color('green', 'success'),
  pad(1),
  'Some message'
)

pad

pad(count: number, separator?: string): IFragment

Creates fragment, which repeats given separator (default: ) given number of times.

pad(1);
pad(2, '#')
pad(1, '\n')

fixed

fixed(
  value: number,
  bias: 'start' | 'end',
  ...children: Array<string | IFragment>
): IFragment

Creates fragment, which makes sure the children will always build to given number of non-ANSI characters. It will either trim the results or add necessary amount of spaces. The bias control if trimming/padding should be done at the start of the string representing built children or at the end.

fixed(5, 'start', 'ERR'); // => '  ERR'
fixed(8, 'end', color('green', 'success')); // equals to color('green', 'success') + ' '
fixed(10, 'end', 'Hello', pad(2), 'World') // => 'Hello  Wor'

ifElse

ifElse(
  condition: Condition,
  ifTrueFragment: string | IFragment,
  elseFragment: string | IFragment
): IFragment

type ConditionValue = boolean | string | number | null | undefined;
type Condition = ConditionValue | (() => ConditionValue);

Change the output based on condition. Condition can ba a primitive value, which can be casted to boolean or a function. If conation or return value of condition is evaluated to true, the first argument - ifTrueFragment will be used, otherwise elseFragment.

let condition = getConditionValue()
ifElse(
  () => condition,
  color('red', 'ERROR'),
  color('yellow', 'WARNING')
)

provide

provide<T>(
  value: T,
  builder: (value: T) => string | IFragment
): IFragment

Provides given value to a builder function, which should return string or fragment. Useful in situations when the output is connected with some calculated value - using provide you only need to calculate final value once and forward it to custom styling logic.

provide(getMessageFromSomewhere(), value => {
  switch (value.level) {
    case 'error':
      return container(
        color('red', modifier('bold', value.level.toUpperCase())),
        pad(2),
        value.log
      );
    case 'info':
      return container(
        color('blue', value.level.toUpperCase()),
        pad(2),
        value.log
      );
    default:
      return container(value.level.toUpperCase(), pad(2), value.log);
  }
})