ansi-fragments
A tiny library with builders to help making logs/CLI pretty with a nice DX.
Installation
yarn add ansi-fragments
Usage
; const prettyLog = ; console;
API
Each fragment implements IFragment
interface:
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.
;;;
modifier
modifier ansiModifier: AnsiModifier, ...children: Array<string | IFragment>: IFragment
Creates fragment for standard ANSI modifiers: dim
, bold
, hidden
, italic
, underline
, strikethrough
.
;;;
container
container...children: Array<string | IFragment>: IFragment
Creates fragment, which sole purpose is to hold and build nested fragments.
pad
padcount: number, separator?: string: IFragment
Creates fragment, which repeats given separator (default: ) given number of times.
;
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.
; // => ' ERR'; // equals to color('green', 'success') + ' ' // => 'Hello Wor'
ifElse
ifElse condition: Condition, ifTrueFragment: string | IFragment, elseFragment: string | IFragment: IFragment ;;
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 =
provide
provide value: T, builder: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.