https://github.com/joutvhu/control-terminal
Control terminal cursor, screen and create string styling
https://github.com/joutvhu/control-terminal
color console cursor logging styling terminal
Last synced: 6 months ago
JSON representation
Control terminal cursor, screen and create string styling
- Host: GitHub
- URL: https://github.com/joutvhu/control-terminal
- Owner: joutvhu
- License: mit
- Created: 2021-04-19T11:12:16.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-28T07:03:24.000Z (about 2 years ago)
- Last Synced: 2025-04-18T03:57:17.883Z (6 months ago)
- Topics: color, console, cursor, logging, styling, terminal
- Language: TypeScript
- Homepage:
- Size: 36.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Control Terminal
- Control terminal cursor
- Terminal string styling## Installation
Install using `pnpm`:
```shell
pnpm add control-terminal
```Or `yarn`:
```shell
yarn add control-terminal
```Or `npm`:
```shell
npm i control-terminal
```## Usage
```typescript
import * as terminal from 'control-terminal';console.log(terminal.style.bold.blue('Hello world!'));
console.log(terminal.style.bgBlue.hex('#ff7b72')('Blue backgroud'));terminal.cursor.erasePreviousLine();
console.log(terminal.style.bgRgb(255, 123, 114).underline('Underline'));
```Output:

## 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 1Clear 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
```js
// Two these are equivalent.
terminal.cursor.eraseCurrentLine();
terminal.cursor.carriageReturn().eraseToEndLine();
```
- `erasePreviousLine(line: number = 1)`: Erase the previous line from the cursor
```js
// Two these are equivalent.
terminal.cursor.erasePreviousLine(line);
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:
```js
console.log(terminal.style.bold.bgCyan.green('Hello'));console.log(terminal.style.italic.bgBlack.white.underline('Hello'));
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)`
```js
terminal.style.ansi256(12)('Blue text');
```- `bgAnsi256(code: number)`
```js
terminal.style.bgAnsi256(12)('Blue background');
```RGB:
- `rgb(red: number, green: number, blue: number)`
```js
terminal.style.rgb(88, 166, 255)('Blue text');
```- `bgRgb(red: number, green: number, blue: number)`
```js
terminal.style.bgRgb(88, 166, 255)('Blue background');
```Hex:
- `hex(hex: string)`
```js
terminal.style.hex('#58a6ff')('Blue text');
```- `bgHex(hex: string)`
```js
terminal.style.bgHex('#58a6ff')('Blue background');
```