fude

Node.js library for terminal text style formatting

Usage no npm install needed!

<script type="module">
  import fude from 'https://cdn.skypack.dev/fude';
</script>

README

fude">

nodejs library for terminal text style formatting.

npm npm_dt npm_license npm_types bundle_tree size coveralls dependabot Node.js CI CodeQL

Features

  • Fast loading, fast performing
  • ANSI Codes complete
  • 100% tree-shakeable by design
  • Template literals friendly
  • Font modifiers

ornaments

  • Foreground colors (normal & bright)

fg_colors

  • Background colors (normal & bright)

bg_colors

Table of contents

Install

$ npm i fude

Usage

import { fude, white, black, bgRed, bgWhite } from 'fude'

console.log(fude('筆', bgRed, white) + fude(' fude ', bgWhite, black))

// alternatively...

console.log(bgRed(white`筆`) + bgWhite(black` fude `))

// ...template literals

console.log(bgRed`${white`筆`}` + bgWhite`${black` fude `}`)

// nesting (same result, slightly different intentions with the backgrounds)...

console.log(bgRed(white('筆') + bgWhite(` ${black('fude')} `)))

// template literals (also nested)...

console.log(bgRed`${white`筆`}${bgWhite` ${black`fude`} `}`)

TTY capabilities

To check what your TTY is capable of, call this handy function:

import { ttyCapability } from 'fude'

console.log(ttyCapability())

API

fude(string, ...<ornament>)

Example:

import { fude, bgWhite, red } from 'fude'

let output = fude('red text on white background', red, bgWhite)

fude.<ornament>(string|<ornament>())

Example:

import { bgWhite, red } from 'fude'

let output = bgWhite(red('red text on white background'))

Tagged template literals

Example:

import { bgWhite, red, blue } from 'fude'

let output1 = `${red`red text`} and ${bgWhite`${blue`blue text on white background`}`}`

let output2 =
  `${red`red text`}` +
  ' and ' +
  `${bgWhite(blue`blue text on white background`)}`

// output1 === output2

Ornaments

Character ornaments (styles) applicable to text.

  • Modifiers
  • Foreground Colors
  • Background Colors
Modifiers Fg colors (normal) Fg colors (bright) Bg colors (normal) Bg colors (bright)
bold black gray bgBlack bgGray
dim red brightRed bgRed bgBrightRed
italic green brightGreen bgGreen bgBrightGreen
underline yellow brightYellow bgYellow bgBrightYellow
doublyUnderline blue brightBlue bgBlue bgBrightBlue
blinkSlow magenta brightMagenta bgMagenta bgBrightMagenta
blinkFast cyan brightCyan bgCyan bgBrightCyan
inverse white brightWhite bgWhite bgBrightWhite
hide
strikethrough

Note on terminal capabilities

  1. Exact colors values are dependant on the terminal implementation.

  2. Not all modifiers are available on every terminal.

(check your terminal capabilities)

RGB & HEX

In terminals able to display 16 million colors (welcome to the future) you can have foreground or background colors defined by their RGB or HEX values. Also underlines with different colors than text!

HEX values are accepted starting with or without #, both short form FFF and long form FFFFFF.

fude.rgb(string|<ornament>, {r:number, g:number, b:number})

fude.hex(string|<ornament>, hex:number)

fude.rgbBg(string|<ornament>, {r:number, g:number, b:number})

fude.hexBg(string|<ornament>, hex:number)

fude.rgbUnderline(string|<ornament>, {r:number, g:number, b:number, double?:boolean})

fude.hexUnderline(string|<ornament>, hex:number, double?:boolean)

Examples:

import { rgb, hexBg, rgbUnderline, black, bold, red } from 'fude'

console.log(rgb('tomato text', { r: 255, g: 99, b: 71 }))

console.log(hexBg('mustard background', '#FFBF47'))

// this will underline the text and apply the given color to the underline only
console.log(
  `${rgbBg('You have misspelt:', { g: 128 })} ${rgbUnderline(
    black(bold('buisness')),
    255,
    50,
    50
  )}`
)

console.log(`
  Q: 1 + 1
  
  A: ${hexUnderline('3', 'F00', true)} ${bold(red('Bad Error!!!'))}`)

rgb_examples

ANSI SGR Parameters Codes

It is possible to call directly the ANSI SGR Parameters codes:

fude.ansi(string|<ornament>, ...number)

Example:

import * as fude from 'fude'

fude.ansi('This text is black on green background', 42, 30)

// or composing with ornaments

ansi(
  'white on green and ' +
    italic(bold('italic, bold and white on green background')),
  42,
  30
)

As a convenience, it is possible to use ornaments by appending Code to their name:

import { ansi, bgGreenCode, blackCode } from 'fude'

ansi('This text is black on green background', bgGreenCode, blackCode)

TypeScript

TypeScript types are included.

What's in a name?

Fude (筆 - Japanese pronunciation: [ɸɯ̟ᵝde̞] foo-de -- de as in dentist) is Japanese for a calligraphy brush. Since there isn't really a plural form in Japanese, in this case 'fude' can be interpreted as 'brushes'.

The idea is that you use a different brush for a different style of stroke (or color). Here you have different font styles and foreground/background colors.

fude('筆', bgRed, white)

This means, I want to use this set of brushes (bgRed and white) to compose the given text: .

Benchmarks

For an analysis of how well fude stacks against other libraries as well as which is the fastest way to render a string with fude, refer to benchmarks.

Spoiler alert fude is fast.

Changelog

The changelog can be found on the Releases page.

Contributing

Everyone is welcome to contribute. Please take a moment to review the contributing guidelines.

Authors and license

Mirco Sanguineti and contributors.

MIT License, see the included LICENCE file.