Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fixed-width-string

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fixed-width-string

Terminal ANSI-aware string fit to fixed width

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

NPM version Build Status

Terminal ANSI-aware string fit to fixed width:

  • pad or truncate string if needed
  • align string
  • ANSI-aware (works right with colored strings)

Install

$ npm install --save 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])

Where:

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 trancate.

Default:

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"

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 © Roman Dvornov

Keywords

FAQs

Package last updated on 02 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc