https://github.com/nexdrew/ansi-align
:abcd: align-text with ANSI support for CLIs
https://github.com/nexdrew/ansi-align
alignment ansi-align ansi-support center-alignment cli padding
Last synced: 8 months ago
JSON representation
:abcd: align-text with ANSI support for CLIs
- Host: GitHub
- URL: https://github.com/nexdrew/ansi-align
- Owner: nexdrew
- License: isc
- Created: 2016-04-30T17:08:04.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-09-27T11:53:09.000Z (over 4 years ago)
- Last Synced: 2025-05-09T00:07:26.365Z (8 months ago)
- Topics: alignment, ansi-align, ansi-support, center-alignment, cli, padding
- Language: JavaScript
- Homepage:
- Size: 46.9 KB
- Stars: 23
- Watchers: 1
- Forks: 5
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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