npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

terminal-font

v0.9.3

Published

Use color, font styles and more in your Node.JS console. RGB color is supported!

Downloads

108

Readme

TerminalFont.JS

[TOC]

Terminal font library for Node.JS, providing support for colors, font styles and more in Node.JS console.

Overview

  • :rabbit: Support colors, font weight, font styles and more
  • :rabbit: Support 24-bit true colors (by RGB or hex color codes)
  • :rabbit: Support font styles combination
  • :rabbit: Support CSS-like settings
  • :rabbit: Easy to use and extend
  • :rabbit: Full typescript support

Installation

npm install terminal-font

If you are using Yarn, you can use

yarn add terminal-font

Usage

Quick Overview

Multiple invocation ways are supported, just depending on what you like:

const { font } = require('terminal-font');

console.log(font().green().apply('Green text'));
console.log(font().bold().red().bgCyan().italic().apply('Complex styles!'));
console.log(
  font().set('bold', 'italic', 'magenta').apply('Yet another way to use')
);
console.log(
  font()
    .set({
      color: font.rgb(245, 169, 184),
      backgroundColor: font.hexColor('#47a9fa'),
    })
    .apply('Fallen angel, distant heaven.')
);

The result is:

Screenshots

Colors

To set the color or the background color of the text, simply use the format like

font().red().apply('Red text');
font().bgBlue().apply('Text with blue background');

which uses the pre-defined colors by the terminal (see pre-defined colors) or

font()
  .set({
    color: font.rgb(245, 169, 184),
    backgroundColor: font.hexColor('#47a9fa'),
  })
  .apply('Fallen angel, distant heaven.');

to use the 24-bit true colors. Specially, font().default() or font().bgDefault() set the color or background color of the text to the default color defined by the terminal.

NOTICE: True color mode is supported by most of the terminal emulators recently, however, not all terminal emulator supports 24-bit true color mode. A classical counterexample is the Terminal.App in MacOS, which may map the color into the closet color in 256-colors table, or pre-defined basic colors.

Pre-defined Colors

Recently, Terminal.JS supports the following pre-defined colors:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

If you want to specify a color beyond the pre-defined colors, use the RGB or hex color code form instead. Moreover, more pre-defined colors will be supported in the future, just like what CSS acts.

Styles

To set the styles of the text, use one of the following forms:

font().bold().italic().apply('Italic bold text');
font()
  .set({
    fontStyle: ['faint', 'underline', 'strike'],
  })
  .apply('Another way');

Supported styles include:

  • bold
  • italic
  • faint
  • underline
  • blink
  • strike

Specially, set the font style to regular will clear all styles (colors won't be affected).

Combining Colors and Styles

Chain invocation is supported in TerminalFont.JS, thus you can easily combine the colors and styles with the following form:

font()
  .set({
    color: 'red',
  })
  .bgWhite()
  .set({
    fontStyle: ['italic', 'faint'],
  })
  .blink()
  .strike()
  .underline()
  .apply('Very complex styles');

API

~~TL;DR;~~

Function: font

font()

Syntax

  font(): Font;

Creates a new Font object. This is the alias for calling new Font().

font.rgb

Syntax

font.rgb(red: number, green: number, blue: number): RgbObject;

Returns a RgbObject with the given parameters defining the RGB values. Please use this with Font.prototype.set.

font.hexColor

Syntax

font.hexColor(hexCode: string): RgbObject;

Returns a RgbObject with the given hex code, similarily to font.rgb.

Class: Font

An instance of class Font stores and manipulate certian settings of font, and is able to apply these settings to a string.

Font.prototype.set()

Syntax

Font.prototype.set(...FontSettings[]);

Configure the font settings. The arguments can be passed via various ways:

Via string

Sample:

font().set('red', 'italic', 'strike');

Via object

In this way, you need to pass a object with the type FontSettingsObject.

Sample:

font().set({
  color: 'red', // text color
  backgroundColor: 'cyan', // background color
  fontStyle: ['bold', 'italic'], // font style
  options: {
    // options
  },
});

See Options for supported options list.

Font.prototype.apply()

Syntax

Font.prototype.apply(originalString: string): string;

Apply the font settings to a string and returns the new string with font. This operation doesn't change the original string.

Options

Additional options can be set with the form

font().set({
  options: {
    // ...
  },
});

Supported options include:

reset

Type: boolean Defualt: true

This configures whether to reset the colors at the ending of the string. Set this option to false will allow you to use TerminalFont.JS in another way:

const fontSetter = () =>
  font().set({
    options: {
      reset: false,
    },
  });
console.log(
  'Are %snested %sstyles %sawesome %sor %snot?',
  fontSetter().red().apply(''),
  fontSetter().bold().apply(''),
  fontSetter().bgCyan().apply(''),
  fontSetter().strike().apply(''),
  fontSetter().underline().apply('')
);

NOTICE: This is not a recommended way to use TerminalFont.JS, as it may lead to an unexpected result. We will provide more elegant ways to use nested styles in the future.

To-do List

  • :rabbit: Fallback strategy if terminal doesn't support true color
  • :rabbit: More pre-defined color
  • :rabbit: Gradient color support
  • :rabbit: Render HTML template

License

TerminalFont.JS is licensed under GNU Lesser General Public License 2.1.