• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1wide-align
2----------
3
4A wide-character aware text alignment function for use in terminals / on the
5console.
6
7### Usage
8
9```
10var align = require('wide-align')
11
12// Note that if you view this on a unicode console, all of the slashes are
13// aligned. This is because on a console, all narrow characters are
14// an en wide and all wide characters are an em. In browsers, this isn't
15// held to and wide characters like "古" can be less than two narrow
16// characters even with a fixed width font.
17
18console.log(align.center('abc', 10))     // '   abc    '
19console.log(align.center('古古古', 10))  // '  古古古  '
20console.log(align.left('abc', 10))       // 'abc       '
21console.log(align.left('古古古', 10))    // '古古古    '
22console.log(align.right('abc', 10))      // '       abc'
23console.log(align.right('古古古', 10))   // '    古古古'
24```
25
26### Functions
27
28#### `align.center(str, length)` → `str`
29
30Returns *str* with spaces added to both sides such that that it is *length*
31chars long and centered in the spaces.
32
33#### `align.left(str, length)` → `str`
34
35Returns *str* with spaces to the right such that it is *length* chars long.
36
37### `align.right(str, length)` → `str`
38
39Returns *str* with spaces to the left such that it is *length* chars long.
40
41### Origins
42
43These functions were originally taken from
44[cliui](https://npmjs.com/package/cliui). Changes include switching to the
45MUCH faster pad generation function from
46[lodash](https://npmjs.com/package/lodash), making center alignment pad
47both sides and adding left alignment.
48