Socket
Socket
Sign inDemoInstall

ansi-fragments

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ansi-fragments

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


Version published
Weekly downloads
1.9M
increased by3.37%
Maintainers
1
Weekly downloads
 
Created
Source

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(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(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(...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(count: number, separator?: string): IFragment

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

pad(1);
pad(2, '#')
pad(1, '\n')
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'

Keywords

FAQs

Package last updated on 08 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc