fixed-width-string
Terminal ANSI-aware string fit to fixed width
Last updated 5 years ago by lahmatiy .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install fixed-width-string 
SYNC missed versions from official npm registry.

NPM version Build Status Coverage Status

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 = require('fixed-width-string');

console.log(fixedWidthString('test', 10));
// "test      "

console.log(fixedWidthString('very long string', 10));
// "very long…"

console.log(fixedWidthString('very long string', { align: 'right' }));
// "…ng string"

var chalk = require('chalk');

console.log(fixedWidthString(chalk.green('hello') + ' ' + chalk.red('world'), 8));
// "\u001b[32mhello\u001b[39m \u001b[31mw…\u001b[39m"
//
// but in terminal you'll see colored
// "hello w…"

API

fixedWidthString(str, width[, options])

options.padding

String to padding with.

Default: ' ' (one space)

console.log(fixedWidthString('test', 10));
// "test      "

console.log(fixedWidthString('test', 10, { padding: '.' }));
// "test......"

options.align

Side to align.

Values: 'left' or 'right'
Default: 'left'

console.log(fixedWidthString('test', 10));
// "test      "

console.log(fixedWidthString('test', 10, { align: 'right' }));
// "      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.log(fixedWidthString('hello world', 8));
// "hello w…"

console.log(fixedWidthString('hello world', 8, { ellipsis: '~' }));
// "hello w~"

console.log(fixedWidthString('hello world', 8, { ellipsis: '...', align: 'right' }));
// "...world"

console.log(fixedWidthString('hello world', 8, { ellipsis: false, align: 'right' }));
// "lo world"

options.truncate

Side should be truncated.

Values: 'left' or 'right'
Default: 'right' when align: 'left' or 'left' when align: 'right'

console.log(fixedWidthString('hello world', 8));
// "hello w…"

console.log(fixedWidthString('hello world', 8, { truncate: 'left' }));
// "…o world"

console.log(fixedWidthString('hello world', 8, { align: 'right' }));
// "hello w…"

console.log(fixedWidthString('hello world', 8, { align: 'right', truncate: 'left' }));
// "…o world"

License

MIT

Current Tags

  • 2.0.0                                ...           latest (5 years ago)

3 Versions

  • 2.0.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |