项目作者: joutvhu

项目描述 :
Control terminal cursor, screen and create string styling
高级语言: TypeScript
项目地址: git://github.com/joutvhu/control-terminal.git
创建时间: 2021-04-19T11:12:16Z
项目社区:https://github.com/joutvhu/control-terminal

开源协议:MIT License

下载


Control Terminal

  • Control terminal cursor
  • Terminal string styling

Installation

Install using pnpm:

  1. pnpm add control-terminal

Or yarn:

  1. yarn add control-terminal

Or npm:

  1. npm i control-terminal

Usage

  1. import * as terminal from 'control-terminal';
  2. console.log(terminal.style.bold.blue('Hello world!'));
  3. console.log(terminal.style.bgBlue.hex('#ff7b72')('Blue backgroud'));
  4. terminal.cursor.erasePreviousLine();
  5. console.log(terminal.style.bgRgb(255, 123, 114).underline('Underline'));

Output:

example-log

Cursor

The methods in terminal.cursor namespace will help you control cursor and terminal.

Moving cursor methods:

  • goTo(line: number, column: number): Move the cursor to a specific position (line >= 1, column >= 1)
  • gotoStart(): Move the cursor to line 1, column 1
  • carriageReturn(): Move the cursor to start of line
  • up(line: number = 1): Move the cursor up line lines
  • down(line: number = 1): Move the cursor down line lines
  • forward(column: number = 1): Move the cursor forward column columns
  • backward(column: number = 1): Move the cursor backward column columns
  • previousLine(): Move the cursor up 1 line, column 1

Clear screen methods:

  • clearScreen(): Clear the screen
  • reset(): Reset device
  • eraseToEndLine(): Erase to the end of line that begins the cursor’s current position
  • eraseToEndScreen(): Erase to the end of screen that begins the cursor’s current position
  • eraseCurrentLine(): Erase the current line of the cursor
    1. // Two these are equivalent.
    2. terminal.cursor.eraseCurrentLine();
    3. terminal.cursor.carriageReturn().eraseToEndLine();
  • erasePreviousLine(line: number = 1): Erase the previous line from the cursor
    1. // Two these are equivalent.
    2. terminal.cursor.erasePreviousLine(line);
    3. terminal.cursor.up(line).eraseCurrentLine();

Other methods:

  • hide(): Hide cursor
  • save(): Save cursor position
  • restore(): Restore cursor position

Styles

The terminal.style namespace will help you string styling.

Modifiers:

  • reset: Resets the current color chain.
  • bold: Make text bold.
  • dim: Emitting only a small amount of light.
  • italic: Make text italic. (Not widely supported)
  • underline: Make text underline. (Not widely supported)
  • slowBlink: Make text slow blink. (Not widely supported)
  • rapidBlink: Make text rapid blink. (Not widely supported)
  • inverse: Inverse background and foreground colors.
  • hidden: Prints the text, but makes it invisible.
  • strikethrough: Puts a horizontal line through the center of the text. (Not widely supported)
  • fraktur: Make text fraktur. (Not widely supported)
  • framed: Make text framed. (Not widely supported)
  • encircled: Make text encircled. (Not widely supported)
  • overline: Make text overline. (Not widely supported)

Colors:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • blackBright
  • redBright
  • greenBright
  • yellowBright
  • blueBright
  • magentaBright
  • cyanBright
  • whiteBright

Background colors:

  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite
  • bgBlackBright
  • bgRedBright
  • bgGreenBright
  • bgYellowBright
  • bgBlueBright
  • bgMagentaBright
  • bgCyanBright
  • bgWhiteBright

Example:

  1. console.log(terminal.style.bold.bgCyan.green('Hello'));
  2. console.log(terminal.style.italic.bgBlack.white.underline('Hello'));
  3. console.log(terminal.style.white.inverse.bgMagentaBright('Hello'));

Support 256 color and True color

Control Terminal support 256 colors and True color (16 million colors) on terminal applications.

The following color models can be used:

256 colors:

  • ansi256(code: number)
  1. terminal.style.ansi256(12)('Blue text');
  • bgAnsi256(code: number)
  1. terminal.style.bgAnsi256(12)('Blue background');

RGB:

  • rgb(red: number, green: number, blue: number)
  1. terminal.style.rgb(88, 166, 255)('Blue text');
  • bgRgb(red: number, green: number, blue: number)
  1. terminal.style.bgRgb(88, 166, 255)('Blue background');

Hex:

  • hex(hex: string)
  1. terminal.style.hex('#58a6ff')('Blue text');
  • bgHex(hex: string)
  1. terminal.style.bgHex('#58a6ff')('Blue background');