2.0.2 • Published 1 year ago

colorix v2.0.2

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
1 year ago

Colorix

Colorix provides a simple way to define and layer color presets, and helps you recognize and track ANSI escape sequences in your code. Each module uses template literals (and a bit of magic 🪄) to construct a type representation of the color sequences applied to your strings.

import cx from 'colorix'

const goblinInk = cx('bgGreen', 'black', 'bold')
console.log(goblinInk('hello goblin', '!'))

goblin-example

With TypeScript, always know when a string has hidden characters

declare const goblinMessage: Colorix<['bgGreen', 'black', 'bold'], ['hello goblin', '!']>
// => `\u001B[42;30;1mhello goblin!\u001B[0m`

This is useful with primitive strings as well

declare const goblinMessage: Colorix<['bgGreen', 'black', 'bold'], [string, ...string[]]>
// => `\u001B[42;30;1m${string}\u001B[0m`

How it works

// create a theme by passing colors to the first function.
declare const cx: <Colors extends Color[]>(
  ...colors: Colors
) => // then pass stringifiable values to the second function to colorize them.
<Strings extends Stringifiable[]>(
  ...strings: Strings
) => // the returned value is an ansified string.
Colorix<Colors, Strings>

Installation

Add colorix to your project using your favorite package manager.

NPM

npm install colorix

PNPM

pnpm add colorix

Yarn

yarn add colorix

How to support terminals without color

safe, colorixSafe, cxs

You can use safe to check if the terminal supports color before applying a preset.

import cx, { safe } from 'colorix'
const errorInk = cx('bold', 'red')

console.log(errorInk('That tasted purple...'))
// "\u001B[mThat tasted purple...\u001B[0m"

console.log(safe(errorInk)('That tasted purple...'))
// "That tasted purple..." | "\u001B[mThat tasted purple...\u001B[0m"

Alternatively, use colorixSafe / cxs for an Ink preset that only applies colors if the terminal supports it.

import { cxs, colorixSafe } from 'colorix'

const safeErrorInk = cxs('bold', 'red') // or colorixSafe('bold', 'red')
console.log(safeErrorInk('That tasted purple...'))

Bonus Features

ColorixError

Fun way to colorize error messages without worrying about the terminal supporting color, or importing colorix / cx / safe into many files.

import { ColorixError } from 'colorix'

const BasicError = new ColorixError(
  'simple message that is always safe to display'
)
const PrettyError = new ColorixError((cx) =>
  cx(
    'white',
    'bgBlue',
    'dim',
    'bold'
  )(
    'Pretty Message',
    cx('reset')(' '),
    cx('underline', 'green')(
      'npmjs.com/package/colorix',
      cx('reset', 'italic')(' '),
      '(this message will always be stripped of color if supportsColor is false)'
    )
  )
)

throw PrettyError

PrettyError

For an "out-of-the-box" solution, use PrettyError. It provides the same naming and fallback message behavior as Colorix without an api for customizing the rest of the error.

import { PrettyError } from 'colorix'

const IOError = PrettyError('IOError', 'An unknown IO error occurred.')

try {
  throw new IOError()
} catch (err) {
  console.log(err)
}

try {
  throw new IOError('Illegal write operation.', 'more info...')
} catch (err) {
  console.log(err)
}

pretty-error-example

colorix-error-example

Exports

default, colorix, cx

ColorixDescription
default / cx / colorixCreate presets for colorizing Stringifiable values.
cxs / colorixSafeCreate presets for colorizing when the terminal supports it Stringifiable.
safeCall a colorix preset safely safe(cx( ...colors )).
ColorixErrorCreate child Error classes with colorized messages.

Constants

NameDescription
CSIcontrol sequence introducer ("\x1b[")
SGRTselect graphic rendition terminator ("m")
FOREGROUNDreadonly foreground lookup object
BACKGROUNDreadonly background lookup object
MODIFIERreadonly modifier lookup object
COLORSreadonly color lookup object (foreground, background, and modifiers)
hasBasicColorsboolean indicating if the terminal supports basic colors
has256Colorsboolean indicating if the terminal supports 256 colors
has16mColorsboolean indicating if the terminal supports 16 million colors
supportsColorboolean indicating if the terminal supports any color

Types

GenericDescription
Colorixutility for creating literals wrapped in ANSI sequences
ColorSequenceutility for creating literal ANSI sequences
AliasDescription
ResetSequenceliteral reset sequence that is always appended to the end of a color sequence
ColorTablereadonly record of color aliases and color codes
Colora foreground, background, or modifier color (keyof ColorTable)
ColorCodean SGR color code
Foregrounda foreground color
Backgrounda background color
Modifiera modifier color

Color Tables

ForegroundCodeForeground BrightCode
"black"30"gray"90
"red"31"redBright"91
"green"32"greenBright"92
"yellow"33"yellowBright"93
"blue"34"blueBright"94
"magenta"35"magentaBright"95
"cyan"36"cyanBright"96
"white"37"whiteBright"97
BackgroundCodeBackground BrightCode
"bgBlack"40"bgGray"100
"bgRed"41"bgRedBright"101
"bgGreen"42"bgGreenBright"102
"bgYellow"43"bgYellowBright"103
"bgBlue"44"bgBlueBright"104
"bgMagenta"45"bgMagentaBright"105
"bgCyan"46"bgCyanBright"106
"bgWhite"47"bgWhiteBright"107
ModifierCode
"reset"0
"bold"1
"dim"2
"italic"3
"underline"4
"inverse"7
"hidden"8
"strikethrough"9
2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago