Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
ansi-align
Advanced tools
Package description
The ansi-align npm package is used to align text within the terminal using ANSI escape codes. It allows for centering, right-aligning, or left-aligning strings within a given width, which is useful for creating visually appealing command-line interfaces.
Center alignment
This feature centers the text within a specified width. The code sample demonstrates how to center the text 'Centered Text' within a width of 20 characters.
const ansiAlign = require('ansi-align');
console.log(ansiAlign.center('Centered Text', {width: 20}));
Right alignment
This feature right-aligns the text within a specified width. The code sample demonstrates how to right-align the text 'Right Aligned' within a width of 20 characters.
const ansiAlign = require('ansi-align');
console.log(ansiAlign.right('Right Aligned', {width: 20}));
Left alignment
This feature left-aligns the text within a specified width. The code sample demonstrates how to left-align the text 'Left Aligned' within a width of 20 characters.
const ansiAlign = require('ansi-align');
console.log(ansiAlign.left('Left Aligned', {width: 20}));
The cli-align package provides similar text alignment functionalities for command-line interfaces. It allows for aligning text to the left, right, or center, and can handle multi-line strings. Compared to ansi-align, cli-align offers a more extensive API for customizing alignments and padding.
The text-table package is used to create a text-based table with aligned columns. It can align text within each column of the table. While ansi-align focuses on aligning individual lines of text, text-table is more suited for aligning text in a tabular format.
The string-width package is designed to calculate and return the visual width of a string in the terminal, considering ANSI escape codes. It does not provide direct alignment features but can be used in conjunction with other packages to align text properly, taking into account the visual width of ANSI-colored strings.
Readme
align-text with ANSI support for CLIs
Easily center- or right- align a block of text, carefully ignoring ANSI escape codes.
E.g. turn this:
Into this:
npm install --save ansi-align
var ansiAlign = require('ansi-align')
ansiAlign(text, [opts])
Align the given text per the line with the greatest string-width
, returning a new string (or array).
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.
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.
center-align
: Very close to this package, except it doesn't support ANSI codes.left-pad
: Great for left-padding but does not support center alignment or ANSI codes.ISC © Contributors
FAQs
align-text with ANSI support for CLIs
The npm package ansi-align receives a total of 8,510,811 weekly downloads. As such, ansi-align popularity was classified as popular.
We found that ansi-align demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.