CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-terminal-kit

Comprehensive Node.js terminal library with 256 colors, interactive components, and advanced graphics capabilities

Pending
Overview
Eval results
Files

colors-styling.mddocs/

Colors and Styling

Terminal Kit provides comprehensive color and text styling capabilities, supporting everything from basic ANSI colors to 24-bit RGB colors, with full background color support and text formatting options.

Basic ANSI Colors

Foreground Colors

terminal.black(text?: string): Terminal;
terminal.red(text?: string): Terminal;
terminal.green(text?: string): Terminal;
terminal.yellow(text?: string): Terminal;
terminal.blue(text?: string): Terminal;
terminal.magenta(text?: string): Terminal;
terminal.cyan(text?: string): Terminal;
terminal.white(text?: string): Terminal;
terminal.gray(text?: string): Terminal;
terminal.grey(text?: string): Terminal;

Basic 8-color ANSI support. Methods are chainable and can include text.

const term = require('terminal-kit').terminal;

term.red('Error: ').white('Something went wrong!\n');
term.green().bold('Success!\n');

Bright Colors

terminal.brightBlack(text?: string): Terminal;
terminal.brightRed(text?: string): Terminal;
terminal.brightGreen(text?: string): Terminal;
terminal.brightYellow(text?: string): Terminal;
terminal.brightBlue(text?: string): Terminal;
terminal.brightMagenta(text?: string): Terminal;
terminal.brightCyan(text?: string): Terminal;
terminal.brightWhite(text?: string): Terminal;

Bright variants of the basic colors.

term.brightRed('Urgent: ').brightWhite('Important message\n');

Background Colors

terminal.bgBlack(text?: string): Terminal;
terminal.bgRed(text?: string): Terminal;
terminal.bgGreen(text?: string): Terminal;
terminal.bgYellow(text?: string): Terminal;
terminal.bgBlue(text?: string): Terminal;
terminal.bgMagenta(text?: string): Terminal;
terminal.bgCyan(text?: string): Terminal;
terminal.bgWhite(text?: string): Terminal;
terminal.bgGray(text?: string): Terminal;
terminal.bgGrey(text?: string): Terminal;

Background color variants.

term.bgRed().white(' ERROR ').styleReset(' Something failed\n');

Bright Background Colors

terminal.bgBrightBlack(text?: string): Terminal;
terminal.bgBrightRed(text?: string): Terminal;
terminal.bgBrightGreen(text?: string): Terminal;
terminal.bgBrightYellow(text?: string): Terminal;
terminal.bgBrightBlue(text?: string): Terminal;
terminal.bgBrightMagenta(text?: string): Terminal;
terminal.bgBrightCyan(text?: string): Terminal;
terminal.bgBrightWhite(text?: string): Terminal;

Bright background colors.

Advanced Color Support

256-Color Palette

terminal.color256(colorIndex: number, text?: string): Terminal;
terminal.bgColor256(colorIndex: number, text?: string): Terminal;

Use the full 256-color terminal palette (0-255).

// Use color index 196 (bright red)
term.color256(196, 'Bright red text\n');

// Background color
term.bgColor256(21).white(' Blue background ').styleReset('\n');

RGB Colors (24-bit True Color)

terminal.colorRgb(r: number, g: number, b: number, text?: string): Terminal;
terminal.bgColorRgb(r: number, g: number, b: number, text?: string): Terminal;

Use RGB values (0-255 each) for true color support.

// Custom RGB colors
term.colorRgb(255, 100, 50, 'Orange text\n');
term.bgColorRgb(30, 144, 255).white(' Dodger blue background ').styleReset('\n');

Grayscale Colors

terminal.colorGrayscale(level: number, text?: string): Terminal;
terminal.bgColorGrayscale(level: number, text?: string): Terminal;

Use grayscale values (0-23 for terminal-kit's grayscale palette).

term.colorGrayscale(12, 'Medium gray text\n');
term.bgColorGrayscale(20).black(' Light gray background ').styleReset('\n');

Generic Color Methods

terminal.color(color: string | number, text?: string): Terminal;
terminal.bgColor(color: string | number, text?: string): Terminal;

Set colors using color names or numbers.

term.color('red', 'Red text using name\n');
term.color(9, 'Bright red using index\n');
term.bgColor('blue').white(' Blue background ').styleReset('\n');

Color Reset

terminal.defaultColor(text?: string): Terminal;

Reset to default terminal colors.

term.red('Red text ').defaultColor('back to default\n');

Text Styling

Basic Styling

terminal.styleReset(text?: string): Terminal;
terminal.bold(state?: boolean, text?: string): Terminal;
terminal.dim(state?: boolean, text?: string): Terminal;
terminal.italic(state?: boolean, text?: string): Terminal;
terminal.underline(state?: boolean, text?: string): Terminal;
terminal.blink(state?: boolean, text?: string): Terminal;
terminal.inverse(state?: boolean, text?: string): Terminal;
terminal.hidden(state?: boolean, text?: string): Terminal;
terminal.strike(state?: boolean, text?: string): Terminal;

Text formatting and styling options. The state parameter toggles the style on/off.

term.bold('Bold text ');
term.italic(true, 'Italic text ');
term.underline().red('Underlined red text').styleReset('\n');

// Toggle styles
term.bold(true).red('Bold red ').bold(false).green('normal green\n');

Style Combinations

// Combine multiple styles
term.bold().italic().underline().red('Multiple styles\n').styleReset();

// Chain styles with colors
term.bgYellow().black().bold(' WARNING ').styleReset().yellow(' Check this out\n');

Color Utility Functions

Color Conversion

// Color name utilities
colorNameToIndex(colorName: string): number;
indexToColorName(colorIndex: number): string;
colorNameToRgb(colorName: string): { r: number; g: number; b: number };
rgbToColorName(r: number, g: number, b: number): string;

// Color format conversion  
color256ToRgb(colorIndex: number): { r: number; g: number; b: number };
rgbToColor256(r: number, g: number, b: number): number;
hexToRgba(hex: string): { r: number; g: number; b: number; a: number };
rgbToHex(r: number, g: number, b: number): string;

Utility functions for color format conversion.

const termkit = require('terminal-kit');

// Convert color name to index
const redIndex = termkit.colorNameToIndex('red'); // Returns 1

// Convert RGB to 256-color index
const colorIndex = termkit.rgbToColor256(255, 100, 50);
term.color256(colorIndex, 'Orange-ish text\n');

// Hex to RGBA conversion
const rgba = termkit.hexToRgba('#FF6432');
term.colorRgb(rgba.r, rgba.g, rgba.b, 'Hex color text\n');

Color Palettes

Get and Set Colors

terminal.getColor(register: number, callback: (error: Error | null, r: number, g: number, b: number) => void): void;
terminal.setColor(register: number, r: number, g: number, b: number, names?: string[], callback?: (error: Error | null) => void): void;

Get or set individual color register values.

// Get current color value
term.getColor(1, (error, r, g, b) => {
  if (!error) {
    console.log(`Red register: RGB(${r}, ${g}, ${b})`);
  }
});

// Set custom color
term.setColor(1, 255, 100, 50, ['custom-orange'], (error) => {
  if (!error) {
    term.red('Now using custom orange for red\n');
  }
});

Palette Operations

terminal.getPalette(register: number, callback: (error: Error | null, palette: ColorPalette) => void): void;
terminal.setPalette(palette: ColorPalette, callback?: (error: Error | null) => void): void;

Get or set entire color palettes.

interface ColorPalette {
  [colorIndex: number]: {
    r: number;
    g: number;
    b: number;
    names?: string[];
  };
}

Chroma.js Integration

Terminal Kit includes the Chroma.js library for advanced color manipulation.

terminal.chroma: typeof import('chroma-js');
const term = require('terminal-kit').terminal;
const chroma = term.chroma;

// Create color scale
const scale = chroma.scale(['red', 'yellow', 'green']).mode('lab');

// Use colors from scale
for (let i = 0; i <= 10; i++) {
  const color = scale(i / 10).rgb();
  term.colorRgb(color[0], color[1], color[2], '█');
}
term('\n');

Usage Examples

Rainbow Text

const term = require('terminal-kit').terminal;

const text = 'Rainbow Text!';
const colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'magenta'];

for (let i = 0; i < text.length; i++) {
  const color = colors[i % colors.length];
  term[color](text[i]);
}
term('\n');

Progress Bar with Colors

function drawProgressBar(percent) {
  const width = 40;
  const filled = Math.floor(width * percent / 100);
  const empty = width - filled;
  
  term.moveTo(1, 10);
  term.cyan('[');
  
  // Green for progress
  term.bgGreen(' '.repeat(filled));
  
  // Red for remaining
  term.bgRed(' '.repeat(empty));
  
  term.cyan('] ');
  
  // Color-coded percentage
  if (percent < 30) {
    term.red(`${percent}%`);
  } else if (percent < 70) {
    term.yellow(`${percent}%`);
  } else {
    term.green(`${percent}%`);
  }
}

Styled Log Messages

function logMessage(level, message) {
  const timestamp = new Date().toISOString();
  
  term.gray(`[${timestamp}] `);
  
  switch (level) {
    case 'error':
      term.bgRed().white(' ERROR ').styleReset().red(` ${message}\n`);
      break;
    case 'warn':
      term.bgYellow().black(' WARN ').styleReset().yellow(` ${message}\n`);
      break;
    case 'info':
      term.bgBlue().white(' INFO ').styleReset().blue(` ${message}\n`);
      break;
    case 'success':
      term.bgGreen().white(' SUCCESS ').styleReset().green(` ${message}\n`);
      break;
    default:
      term(` ${message}\n`);
  }
}

// Usage
logMessage('error', 'Something went wrong');
logMessage('success', 'Operation completed');

Install with Tessl CLI

npx tessl i tessl/npm-terminal-kit

docs

buffers-graphics.md

colors-styling.md

document-model.md

index.md

interactive-input.md

terminal-control.md

tile.json