Vangware's ANSI logo

Build Status Coverage License NPM Version Open Issues

📟 Functional CLI ANSI formatter.

Usage

This package can be installed as a dependency or used directly.

Usage as ECMAScript module

🦕 In JS or deno:

import { foregroundRed } from "https://cdn.skypack.dev/@vangware/ansi";

foregroundRed("Hello world!");

🌎 Or in the browser:

<script type="module">
    import { foregroundRed } from "https://cdn.skypack.dev/@vangware/ansi";

    foregroundRed("Hello world!");
</script>

Usage with local installation

First:

npm i @vangware/ansi

And then:

import { whiteText, redBackground, bold, mix } from "@vangware/ansi";

// You can just use the styles
console.log(redBackground("Red background text"));

// Or you can mix them!
const warningText = mix([whiteText, redBackground, bold]);

console.log(warningText("Warning!"));

Documentation

Documentation can be found HERE. It is auto-generated with typedoc based on the JSDocs and the types in the source. Shouldn't be necessary to read this, code editors like VSCode integrate the documentation in the UI.

Changelog

Changelog can be found HERE.

Test coverage

Test coverage can be found HERE.

Index

Background Functions

Const backgroundBlack

  • backgroundBlack<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for black background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with black background.

Const backgroundBlue

  • backgroundBlue<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for blue background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with blue background.

Const backgroundBrightBlue

  • backgroundBrightBlue<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright blue background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright blue background.

Const backgroundBrightCyan

  • backgroundBrightCyan<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright cyan background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright cyan background.

Const backgroundBrightGreen

  • backgroundBrightGreen<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright green background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright green background.

Const backgroundBrightMagenta

  • backgroundBrightMagenta<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright magenta background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright magenta background.

Const backgroundBrightRed

  • backgroundBrightRed<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright red background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright red background.

Const backgroundBrightWhite

  • backgroundBrightWhite<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright white background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright white background.

Const backgroundBrightYellow

  • backgroundBrightYellow<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright yellow background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright yellow background.

Const backgroundColor

  • backgroundColor<Open>(...arg0: number & Open): (<Input>(input: Input) => Input) | (<Input>(input: Input) => `[${Open}m${Input}`)
  • Takes an open ANSI code and an input string and adds the open and close ANSI codes to that string (for background color).

    Type parameters

    • Open: number

    Parameters

    • Rest ...arg0: number & Open

    Returns (<Input>(input: Input) => Input) | (<Input>(input: Input) => `[${Open}m${Input}`)

    Curried function with close ANSI code for background in context.

Const backgroundCyan

  • backgroundCyan<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for cyan background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with cyan background.

Const backgroundGray

  • backgroundGray<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for gray background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with gray background.

Const backgroundGreen

  • backgroundGreen<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for green background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with green background.

Const backgroundMagenta

  • backgroundMagenta<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for magenta background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with magenta background.

Const backgroundRed

  • backgroundRed<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for red background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with red background.

Const backgroundWhite

  • backgroundWhite<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for white background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with white background.

Const backgroundYellow

  • backgroundYellow<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for yellow background.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with yellow background.

Common Functions

Const ansi

  • ansi<Input>(input: Input): `[${Input}m`
  • Takes an input number and returns the escape ANSI sequence.

    Type parameters

    • Input: number

    Parameters

    • input: Input

      Input to escape.

    Returns `[${Input}m`

    Escaped ANSI sequence.

Const format

  • format<Close>(close: Close): <Open>(open: Open) => <Input>(input: Input) => `[${Open}m${Input}[${Close}m`
  • Given an open, a close value and an input string, return formatted input (surrounded by open and close).

    Type parameters

    • Close: number

    Parameters

    • close: Close

      Close value.

    Returns <Open>(open: Open) => <Input>(input: Input) => `[${Open}m${Input}[${Close}m`

    Curried function with close in context.

      • <Open>(open: Open): <Input>(input: Input) => `[${Open}m${Input}[${Close}m`
      • Type parameters

        • Open: number

        Parameters

        • open: Open

        Returns <Input>(input: Input) => `[${Open}m${Input}[${Close}m`

          • <Input>(input: Input): `[${Open}m${Input}[${Close}m`
          • Type parameters

            • Input: string

            Parameters

            • input: Input

            Returns `[${Open}m${Input}[${Close}m`

            Curried function with open and close in context.

Const formattingEnabled

  • formattingEnabled(__namedParameters?: { env?: ProcessEnv; stdout?: Pick<WriteStream & { fd: 1 }, "isTTY"> }): boolean
  • Given a NodeJS process, return true if colors are enabled.

    Parameters

    • __namedParameters: { env?: ProcessEnv; stdout?: Pick<WriteStream & { fd: 1 }, "isTTY"> } = {}
      • Optional Readonly env?: ProcessEnv
      • Optional Readonly stdout?: Pick<WriteStream & { fd: 1 }, "isTTY">

    Returns boolean

Const mix

  • mix(formatters: readonly ((input: string) => string)[]): (input: string) => string
  • Composes formatters.

    Parameters

    • formatters: readonly ((input: string) => string)[]

      Array of formatters to be composed.

    Returns (input: string) => string

      • (input: string): string
      • Parameters

        • input: string

        Returns string

Const optionalFormat

  • optionalFormat<Close>(...arg0: Close & number): (<Open>(open: Open) => <Input>(input: Input) => `[${Open}m${Input}[${Close}m`) | ((_open: number) => <Input>(input: Input) => Input)
  • Impure version of format which changes depending on the current NodeJS process environment.

    Type parameters

    • Close: number

    Parameters

    • Rest ...arg0: Close & number

    Returns (<Open>(open: Open) => <Input>(input: Input) => `[${Open}m${Input}[${Close}m`) | ((_open: number) => <Input>(input: Input) => Input)

Foreground Functions

Const foregroundBlack

  • foregroundBlack<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for black foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with black foreground.

Const foregroundBlue

  • foregroundBlue<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for blue foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with blue foreground.

Const foregroundBrightBlue

  • foregroundBrightBlue<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright blue foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright blue foreground.

Const foregroundBrightCyan

  • foregroundBrightCyan<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright cyan foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright cyan foreground.

Const foregroundBrightGreen

  • foregroundBrightGreen<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright green foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright green foreground.

Const foregroundBrightMagenta

  • foregroundBrightMagenta<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright magenta foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright magenta foreground.

Const foregroundBrightRed

  • foregroundBrightRed<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright red foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright red foreground.

Const foregroundBrightWhite

  • foregroundBrightWhite<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright white foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright white foreground.

Const foregroundBrightYellow

  • foregroundBrightYellow<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bright yellow foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bright yellow foreground.

Const foregroundColor

  • foregroundColor<Open>(...arg0: number & Open): (<Input>(input: Input) => Input) | (<Input>(input: Input) => `[${Open}m${Input}`)
  • Takes an open ANSI code and an input string and adds the open and close ANSI codes to that string (for text color).

    Type parameters

    • Open: number

    Parameters

    • Rest ...arg0: number & Open

    Returns (<Input>(input: Input) => Input) | (<Input>(input: Input) => `[${Open}m${Input}`)

    Curried function with close ANSI code for text color in context.

Const foregroundCyan

  • foregroundCyan<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for cyan foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with cyan foreground.

Const foregroundGray

  • foregroundGray<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for gray foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with gray foreground.

Const foregroundGreen

  • foregroundGreen<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for green foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with green foreground.

Const foregroundMagenta

  • foregroundMagenta<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for magenta foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with magenta foreground.

Const foregroundRed

  • foregroundRed<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for red foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with red foreground.

Const foregroundWhite

  • foregroundWhite<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for white foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with white foreground.

Const foregroundYellow

  • foregroundYellow<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for yellow foreground.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with yellow foreground.

Text Functions

Const bold

  • bold<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for bold text.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with bold format.

Const dimmed

  • dimmed<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for dimmed text.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with dimmed format.

Const inverse

  • inverse<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for inverse text.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with inverse format.

Const italic

  • italic<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for italic text.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with italic format.

Const strikethrough

  • strikethrough<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for strikethrough text.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with strikethrough format.

Const underlined

  • underlined<Input>(...input: Input): Input | `${Input}`
  • Takes an input string and adds the ANSI codes for underlined text.

    Type parameters

    • Input: string

    Parameters

    • Rest ...input: Input

    Returns Input | `${Input}`

    input string with underlined format.