1.0.2 • Published 4 years ago

ansi-code v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

ansi-code

Catalog of ANSI standard codes for ASCII terminals

Install

npm install ansi-code

Usage

import {font, cursor, screen, beep} from 'ansi-code'

const write = (...txt: string[]) => process.stdout.write(txt.join(''))

// Erase current screen, and move cursor position to top-left
write(
  screen.erase,
  cursor.to(0, 0)
)

// Text style
write(
  font.style.bold,
  'This is a bold text',
  font.reset,
  cursor.nextLine()
)

// Text color
write(
  font.color.red,
  'This is a red text',
  font.reset,
  cursor.nextLine()
)

// Text bgColor
write(
  font.bgColor.blue,
  'This is a text with blue background',
  font.reset,
  cursor.nextLine()
)

// Text style, color, and bgColor
write(
  font.style.underline,
  font.color.cyan,
  font.bgColor.red,
  'This is a underlined cyan text with red background',
  font.reset,
  cursor.nextLine()
)

API

Exported variables


font

Const object

Properties:

NameTypeValue
resetstring\u001B[0m
familyobjectsee below
styleobjectsee below
colorobjectsee below
bgColorobjectsee below

font.family

NameTypeValue
defaultstring\u001B[10m
font1string\u001B[11m
font2string\u001B[12m
font3string\u001B[13m
font4string\u001B[14m
font5string\u001B[15m

font.style

NameTypeValue
boldstring\u001B[1m
italicstring\u001B[3m
underlinestring\u001B[4m

font.color

NameTypeValue
blackstring\u001B[30m
redstring\u001B[31m
greenstring\u001B[32m
yellowstring\u001B[33m
bluestring\u001B[34m
magentastring\u001B[35m
cyanstring\u001B[36m
whitestring\u001B[37m
graystring\u001B[90m
brightRedstring\u001B[91m
brightGreenstring\u001B[92m
brightYellowstring\u001B[93m
brightBluestring\u001B[94m
brightMagentastring\u001B[95m
brightCyanstring\u001B[96m
brightWhitestring\u001B[97m
rgbfunction(r: number, g: number, b: number) => \u001B[38;2;${r};${g}${b}m

font.bgColor

NameTypeValue
blackstring\u001B[40m
redstring\u001B[41m
greenstring\u001B[42m
yellowstring\u001B[43m
bluestring\u001B[44m
magentastring\u001B[45m
cyanstring\u001B[46m
whitestring\u001B[47m
graystring\u001B[100m
brightRedstring\u001B[101m
brightGreenstring\u001B[102m
brightYellowstring\u001B[103m
brightBluestring\u001B[104m
brightMagentastring\u001B[105m
brightCyanstring\u001B[106m
brightWhitestring\u001B[107m
rgbfunction(r: number, g: number, b: number) => \u001B[48;2;${r};${g}${b}m

cursor

Const object

Properties:

NameTypeValueDescription
showstring\u001B[?25hShow the cursor
hidestring\u001B[?25lHide the cursor
savestringisAppleTerminal ? '\u001B7' : \u001BsSave current cursor position
restorestringisAppleTerminal ? \u001B8 : \u001BuRestore cursor position if saved before
eraseForwardstring\u001B[0KErase from current cursor position to end of the line
eraseBackwardstring\u001B[1KErase from beginning of the line to current cursor position
eraseLinestring\u001B[2KErase entire line
eraseDownstring\u001B[0JErase from current cursor position to end of screen
eraseUpstring\u001B[1JErase from beginning of the screen to current cursor position
forwardfunction(n: number) => stringMove the cursor n columns forward
backwardfunction(n: number) => stringMove the cursor n columns backward
upfunction(n: number) => stringMove the cursor n rows up
downfunction(n: number) => stringMove the cursor n rows down
nextLinefunction(n: number) => stringMove the cursor to beginning of the line n rows down
previsousLinefunction(n: number) => stringMove the cursor to beginning of the line n rows up
movefunction(x: number,y: number) => stringMove the cursor to column x, row y relative to the current cursor position
tofunction(x: number,y?: number) => stringMove the cursor to column x, row y in screen (current view-port). The position starts x: 0, y: 0 from top-left of the screen.

screen

Const object

Properties:

NameTypeValueDescription
clearstring\u001BcClear current screen
erasestring\u001B[2JErase entire current screen (the cursor position stays as-is)
eraseWholestring\u001B[3JErase whole screen including scroll-back buffer (the cursor position stays as-is)
scrollUpfunction(n: number) => \u001B[${n}SMove entire screen up for n rows, insert new line at bottom
scrollDownfunction(n: number) => \u001B[${n}TInsert n new lines at the top of screen
setModefunction(mode: string) => \u001B[?${mode}hSet mode
resetModefunction(mode: string) => \u001B[?${mode}lReset mode
normalBufferstring\u001B[?47lSwitch to normal screen buffer
alternateBufferstring\u001B[?47hSwitch to alternate screen buffer
rmcupstring\u001B[?1049lSwitch to normal screen buffer, clear memory before switching
smcupstring\u001B[?1049hSwitch to alternate screen buffer, clear memory before switching

beep

Const : \u0007