Giter VIP home page Giter VIP logo

tty-truncate's Introduction

tty-truncate

npm version GitHub Actions Coverage Status

Truncate a string to the current text terminal width, considering its visual width

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

const string = '4724e053261747b278049de678b1ed';

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

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

Though the first impression of this module would be “string.slice(0, process.stdout.columns) suffices.”, it doesn't always work well because lots of non-ASCII characters occupy 2 columns in a terminal.

process.stdout.columns; //=> 80

const original = '字'.repeat(100);
//=> '字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字'
const sliced = original.slice(0, process.stdout.columns);
//=> '字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字'

original.length; //=> 100
sliced.length; //=> 80

// The output will overflow the current row,
// because the width of 字 in a text terminal is not 1 column but 2 columns.
console.log(sliced);

tty-truncate handles this case.

ttyTruncate(original);
//=> '字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字…'

Installation

Use npm.

npm install tty-truncate

API

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

ttyTruncate(input)

input: string with no \n
Return: string

It replaces overflowing text with a single .

process.stdout.columns; //=> 20

ttyTruncate('Halfwidth characters');
//=> 'Halfwidth characters'

ttyTruncate('Fullwidth characters');
//=> 'Fullwidth…'

This works only when process.stdout.isTTY is true. In a non-TTY environment it throws an Error instead.

License

ISC License © 2018 - 2019 Watanabe Shinnosuke

tty-truncate's People

Contributors

shinnn avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.