pax_global_header 0000666 0000000 0000000 00000000064 14124330142 0014504 g ustar 00root root 0000000 0000000 52 comment=f79021ac88b3c36c0ccd35c640fc0a82e20f5c0d
ansi-align-3.0.1/ 0000775 0000000 0000000 00000000000 14124330142 0013527 5 ustar 00root root 0000000 0000000 ansi-align-3.0.1/.gitignore 0000664 0000000 0000000 00000000055 14124330142 0015517 0 ustar 00root root 0000000 0000000 node_modules/
.nyc_output/
package-lock.json
ansi-align-3.0.1/.npmrc 0000664 0000000 0000000 00000000023 14124330142 0014642 0 ustar 00root root 0000000 0000000 package-lock=false
ansi-align-3.0.1/.travis.yml 0000664 0000000 0000000 00000000143 14124330142 0015636 0 ustar 00root root 0000000 0000000 sudo: false
language: node_js
node_js:
- '8'
- '10'
- 'node'
after_success: npm run coverage
ansi-align-3.0.1/CHANGELOG.md 0000664 0000000 0000000 00000003324 14124330142 0015342 0 ustar 00root root 0000000 0000000 # Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
### [3.0.1](https://github.com/nexdrew/ansi-align/compare/v3.0.0...v3.0.1) (2021-09-27)
### Bug Fixes
* **package:** update string-width to version 4.1.0 ([#52](https://github.com/nexdrew/ansi-align/issues/52)) ([ab5b733](https://github.com/nexdrew/ansi-align/commit/ab5b733b1c30eef87b75e15459f2216db28d7ed3))
# [3.0.0](https://github.com/nexdrew/ansi-align/compare/v2.0.0...v3.0.0) (2018-12-17)
### Bug Fixes
* **package:** update string-width to version 3.0.0 ([#50](https://github.com/nexdrew/ansi-align/issues/50)) ([67f0d8f](https://github.com/nexdrew/ansi-align/commit/67f0d8f))
### BREAKING CHANGES
* **package:** Node 4 no longer supported, please update to Node 6+ or use ansi-align@2.0.0
# [2.0.0](https://github.com/nexdrew/ansi-align/compare/v1.1.0...v2.0.0) (2017-05-01)
### Features
* ES2015ify, dropping support for Node <4 ([#30](https://github.com/nexdrew/ansi-align/issues/30)) ([7b43f48](https://github.com/nexdrew/ansi-align/commit/7b43f48))
### BREAKING CHANGES
* Node 0.10 or 0.12 no longer supported, please update to Node 4+ or use ansi-align@1.1.0
# [1.1.0](https://github.com/nexdrew/ansi-align/compare/v1.0.0...v1.1.0) (2016-06-06)
### Features
* support left-alignment as no-op ([#3](https://github.com/nexdrew/ansi-align/issues/3)) ([e581db6](https://github.com/nexdrew/ansi-align/commit/e581db6))
# 1.0.0 (2016-04-30)
### Features
* initial commit ([1914d90](https://github.com/nexdrew/ansi-align/commit/1914d90))
ansi-align-3.0.1/LICENSE 0000664 0000000 0000000 00000001333 14124330142 0014534 0 ustar 00root root 0000000 0000000 Copyright (c) 2016, Contributors
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
ansi-align-3.0.1/README.md 0000664 0000000 0000000 00000005601 14124330142 0015010 0 ustar 00root root 0000000 0000000 # ansi-align
> align-text with ANSI support for CLIs
[](https://travis-ci.org/nexdrew/ansi-align)
[](https://coveralls.io/github/nexdrew/ansi-align?branch=master)
[](https://github.com/conventional-changelog/standard-version)
[](https://greenkeeper.io/)
Easily center- or right- align a block of text, carefully ignoring ANSI escape codes.
E.g. turn this:
Into this:
## Install
```sh
npm install --save ansi-align
```
```js
var ansiAlign = require('ansi-align')
```
## API
### `ansiAlign(text, [opts])`
Align the given text per the line with the greatest [`string-width`](https://github.com/sindresorhus/string-width), returning a new string (or array).
#### Arguments
- `text`: required, string or array
The text to align. If a string is given, it will be split using either the `opts.split` value or `'\n'` by default. If an array is given, a different array of modified strings will be returned.
- `opts`: optional, object
Options to change behavior, see below.
#### Options
- `opts.align`: string, default `'center'`
The alignment mode. Use `'center'` for center-alignment, `'right'` for right-alignment, or `'left'` for left-alignment. Note that the given `text` is assumed to be left-aligned already, so specifying `align: 'left'` just returns the `text` as is (no-op).
- `opts.split`: string or RegExp, default `'\n'`
The separator to use when splitting the text. Only used if text is given as a string.
- `opts.pad`: string, default `' '`
The value used to left-pad (prepend to) lines of lesser width. Will be repeated as necessary to adjust alignment to the line with the greatest width.
### `ansiAlign.center(text)`
Alias for `ansiAlign(text, { align: 'center' })`.
### `ansiAlign.right(text)`
Alias for `ansiAlign(text, { align: 'right' })`.
### `ansiAlign.left(text)`
Alias for `ansiAlign(text, { align: 'left' })`, which is a no-op.
## Similar Packages
- [`center-align`](https://github.com/jonschlinkert/center-align): Very close to this package, except it doesn't support ANSI codes.
- [`left-pad`](https://github.com/camwest/left-pad): Great for left-padding but does not support center alignment or ANSI codes.
- Pretty much anything by the [chalk](https://github.com/chalk) team
## License
ISC © Contributors
ansi-align-3.0.1/index.js 0000664 0000000 0000000 00000002466 14124330142 0015204 0 ustar 00root root 0000000 0000000 'use strict'
const stringWidth = require('string-width')
function ansiAlign (text, opts) {
if (!text) return text
opts = opts || {}
const align = opts.align || 'center'
// short-circuit `align: 'left'` as no-op
if (align === 'left') return text
const split = opts.split || '\n'
const pad = opts.pad || ' '
const widthDiffFn = align !== 'right' ? halfDiff : fullDiff
let returnString = false
if (!Array.isArray(text)) {
returnString = true
text = String(text).split(split)
}
let width
let maxWidth = 0
text = text.map(function (str) {
str = String(str)
width = stringWidth(str)
maxWidth = Math.max(width, maxWidth)
return {
str,
width
}
}).map(function (obj) {
return new Array(widthDiffFn(maxWidth, obj.width) + 1).join(pad) + obj.str
})
return returnString ? text.join(split) : text
}
ansiAlign.left = function left (text) {
return ansiAlign(text, { align: 'left' })
}
ansiAlign.center = function center (text) {
return ansiAlign(text, { align: 'center' })
}
ansiAlign.right = function right (text) {
return ansiAlign(text, { align: 'right' })
}
module.exports = ansiAlign
function halfDiff (maxWidth, curWidth) {
return Math.floor((maxWidth - curWidth) / 2)
}
function fullDiff (maxWidth, curWidth) {
return maxWidth - curWidth
}
ansi-align-3.0.1/package.json 0000664 0000000 0000000 00000001636 14124330142 0016023 0 ustar 00root root 0000000 0000000 {
"name": "ansi-align",
"version": "3.0.1",
"description": "align-text with ANSI support for CLIs",
"main": "index.js",
"scripts": {
"pretest": "standard",
"test": "nyc ava",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"release": "standard-version"
},
"files": [
"index.js"
],
"repository": {
"type": "git",
"url": "git+https://github.com/nexdrew/ansi-align.git"
},
"keywords": [
"ansi",
"align",
"cli",
"center",
"pad"
],
"author": "nexdrew",
"license": "ISC",
"bugs": {
"url": "https://github.com/nexdrew/ansi-align/issues"
},
"homepage": "https://github.com/nexdrew/ansi-align#readme",
"dependencies": {
"string-width": "^4.1.0"
},
"devDependencies": {
"ava": "^2.0.0",
"chalk": "^2.4.2",
"coveralls": "^3.0.3",
"nyc": "^14.0.0",
"standard": "^14.0.0",
"standard-version": "^7.0.0"
}
}
ansi-align-3.0.1/test/ 0000775 0000000 0000000 00000000000 14124330142 0014506 5 ustar 00root root 0000000 0000000 ansi-align-3.0.1/test/ansi-align.js 0000664 0000000 0000000 00000004106 14124330142 0017067 0 ustar 00root root 0000000 0000000 import test from 'ava'
import chalk from 'chalk'
import ansiAlign from '../'
test('does not blow up on undefined args', (t) => {
t.is(ansiAlign(), undefined)
})
test('aligns center, splits line feed, and pads with space by default', (t) => {
// one two three
// four five
const out = ansiAlign('one two three\nfour five')
t.is(out, 'one two three\n four five')
})
test('supports ansi', (t) => {
// first line has four ansi escape sequences
// second line has two ansi escape sequences
const inp = chalk.red('one') + ' two ' + chalk.bold('three') + '\n' + chalk.cyan('four ') + 'five'
const out = ansiAlign(inp)
t.is(out, chalk.red('one') + ' two ' + chalk.bold('three') + '\n ' + chalk.cyan('four ') + 'five')
})
test('returns array if given array', (t) => {
// one two
// three four five
const inp = [chalk.green('one two'), 'three four five']
const out = ansiAlign(inp)
t.deepEqual(out, [' ' + chalk.green('one two'), 'three four five'])
})
test('accepts opts for split, pad, and align', (t) => {
// ........one two
// three four five
const inp = 'one two\tthree four five'
const out = ansiAlign(inp, { split: '\t', pad: '.', align: 'right' })
t.is(out, '........one two\tthree four five')
})
test('supports `align: \'left\'` as no-op', (t) => {
const inp = 'one two three\nfour five'
const out = ansiAlign(inp, { align: 'left' })
t.is(out, inp)
})
test('ansiAlign.left is alias for left align (no-op)', (t) => {
const inp = 'one two three\nfour five'
const out = ansiAlign.left(inp)
t.is(out, inp)
})
test('ansiAlign.center is alias for center align', (t) => {
// one
// two
// three four
// five
const inp = [' one ', ' two ', ' three four ', ' five ']
const out = ansiAlign.center(inp)
t.deepEqual(out, [' one ', ' two ', ' three four ', ' five '])
})
test('ansiAlign.right is alias for right align', (t) => {
// one
// two three
// four
// five
const inp = 'one\ntwo three\nfour\nfive'
const out = ansiAlign.right(inp)
t.is(out, ' one\ntwo three\n four\n five')
})