tty-truncate

Truncate a string to the current text terminal width


Keywords
tty, truncate, length, width, adjust, string, text, line, terminal, console, javascript, log, nodejs, string-manipulation
License
ISC
Install
npm install tty-truncate@1.0.5

Documentation

tty-truncate

npm version GitHub Actions Coverage Status

Truncate a string to the current text terminal width

const ttyTruncate = require('tty-truncate');

const str = '4724e053261747b278049de678b1ed';

process.stdout.columns; //=> 30
ttyTruncate(str); //=> '4724e053261747b278049de678b1ed'

process.stdout.columns; //=> 20
ttyTruncate(str); //=> '4724e053261747b2780…'

Installation

Use npm.

npm install tty-truncate

API

const ttyTruncate = require('tty-truncate');

ttyTruncate(input)

input: string (single-line string)
Return: string

It replaces overflowing text with a single .

Note that this module works only when process.stdout.isTTY is true. In a non-TTY environment it always throws an error.

const ttyTruncate = require('tty-truncate');

console.log(process.stdout.isTTY === true);

try {
  console.log(ttyTruncate('example'));
} catch ({message}) {
  console.log(message);
}
$ node example.js
> true
> example

$ node example.js | echo -n
> false
> tty-truncate doesn't support non-TTY environments.

License

ISC License © 2018 - 2019 Watanabe Shinnosuke