Skip to content

A fast function for calculating the visual width of a string once printed to the terminal.

License

Notifications You must be signed in to change notification settings

fabiospampinato/fast-string-width

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fast String Width

A fast function for calculating the visual width of a string once printed to the terminal.

See fast-string-truncated-width for a lower-level version of this.

Install

npm install fast-string-width

Usage

import fastStringWidth from 'fast-string-width';

// The width of various classes of characters is configurable

const options = {
  ansiWidth: 0,
  controlWidth: 0,
  tabWidth: 8,
  ambiguousWidth: 1,
  emojiWidth: 2,
  fullWidthWidth: 2,
  regularWidth: 1,
  wideWidth: 2
};

// Calculating the visual width of some strings

fastStringWidth ( 'hello', options ); // => 5
fastStringWidth ( '\x1b[31mhello', options ); // => 5
fastStringWidth ( '👨‍👩‍👧‍👦', options ); // => 2
fastStringWidth ( 'hello👨‍👩‍👧‍👦', options ); // => 7

// Calculating the visual width while tweaking the width of emojis

fastStringWidth ( '👶👶🏽', { ...options, emojiWidth: 1.5 } ); // => 3

License

MIT © Fabio Spampinato

About

A fast function for calculating the visual width of a string once printed to the terminal.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 52.1%
  • JavaScript 47.9%