Fit a string to the fixed width (length) with aware of ANSI escape codes.
Features:
- Works well with strings with ANSI escape codes, i.e strings styled with a lib like chalk
- Pad or truncate string if needed
- Align string
Install
$ npm install fixed-width-string
Usage
var fixedWidthString = ; console;// "test " console;// "very long…" console;// "…ng string" var chalk = ; console;// "\u001b[32mhello\u001b[39m \u001b[31mw…\u001b[39m"//// but in terminal you'll see colored// "hello w…"
API
options.padding
String to padding with.
Default: ' '
(one space)
console;// "test " console;// "test......"
options.align
Side to align.
Values: 'left'
or 'right'
Default: 'left'
console;// "test " console;// " test"
options.ellipsis
String to be placed when string is truncating.
Default: '…'
Note: To omit ellipsis, pass an empty string or falsy value for the option.
console;// "hello w…" console;// "hello w~" console;// "...world" console;// "lo world"
options.truncate
Side should be truncated.
Values: 'left'
or 'right'
Default: 'right'
when align: 'left'
or 'left'
when align: 'right'
console;// "hello w…" console;// "…o world" console;// "hello w…" console;// "…o world"
License
MIT