Have ideas to improve npm?Join in the discussion! »

    tty-truncate

    1.0.5 • Public • Published

    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

    Install

    npm i tty-truncate

    DownloadsWeekly Downloads

    925

    Version

    1.0.5

    License

    ISC

    Unpacked Size

    4.51 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar