You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.4 KiB

4 years ago
  1. # text-table
  2. generate borderless text table strings suitable for printing to stdout
  3. [![build status](https://secure.travis-ci.org/substack/text-table.png)](http://travis-ci.org/substack/text-table)
  4. [![browser support](https://ci.testling.com/substack/text-table.png)](http://ci.testling.com/substack/text-table)
  5. # example
  6. ## default align
  7. ``` js
  8. var table = require('text-table');
  9. var t = table([
  10. [ 'master', '0123456789abcdef' ],
  11. [ 'staging', 'fedcba9876543210' ]
  12. ]);
  13. console.log(t);
  14. ```
  15. ```
  16. master 0123456789abcdef
  17. staging fedcba9876543210
  18. ```
  19. ## left-right align
  20. ``` js
  21. var table = require('text-table');
  22. var t = table([
  23. [ 'beep', '1024' ],
  24. [ 'boop', '33450' ],
  25. [ 'foo', '1006' ],
  26. [ 'bar', '45' ]
  27. ], { align: [ 'l', 'r' ] });
  28. console.log(t);
  29. ```
  30. ```
  31. beep 1024
  32. boop 33450
  33. foo 1006
  34. bar 45
  35. ```
  36. ## dotted align
  37. ``` js
  38. var table = require('text-table');
  39. var t = table([
  40. [ 'beep', '1024' ],
  41. [ 'boop', '334.212' ],
  42. [ 'foo', '1006' ],
  43. [ 'bar', '45.6' ],
  44. [ 'baz', '123.' ]
  45. ], { align: [ 'l', '.' ] });
  46. console.log(t);
  47. ```
  48. ```
  49. beep 1024
  50. boop 334.212
  51. foo 1006
  52. bar 45.6
  53. baz 123.
  54. ```
  55. ## centered
  56. ``` js
  57. var table = require('text-table');
  58. var t = table([
  59. [ 'beep', '1024', 'xyz' ],
  60. [ 'boop', '3388450', 'tuv' ],
  61. [ 'foo', '10106', 'qrstuv' ],
  62. [ 'bar', '45', 'lmno' ]
  63. ], { align: [ 'l', 'c', 'l' ] });
  64. console.log(t);
  65. ```
  66. ```
  67. beep 1024 xyz
  68. boop 3388450 tuv
  69. foo 10106 qrstuv
  70. bar 45 lmno
  71. ```
  72. # methods
  73. ``` js
  74. var table = require('text-table')
  75. ```
  76. ## var s = table(rows, opts={})
  77. Return a formatted table string `s` from an array of `rows` and some options
  78. `opts`.
  79. `rows` should be an array of arrays containing strings, numbers, or other
  80. printable values.
  81. options can be:
  82. * `opts.hsep` - separator to use between columns, default `' '`
  83. * `opts.align` - array of alignment types for each column, default `['l','l',...]`
  84. * `opts.stringLength` - callback function to use when calculating the string length
  85. alignment types are:
  86. * `'l'` - left
  87. * `'r'` - right
  88. * `'c'` - center
  89. * `'.'` - decimal
  90. # install
  91. With [npm](https://npmjs.org) do:
  92. ```
  93. npm install text-table
  94. ```
  95. # Use with ANSI-colors
  96. Since the string length of ANSI color schemes does not equal the length
  97. JavaScript sees internally it is necessary to pass the a custom string length
  98. calculator during the main function call.
  99. See the `test/ansi-colors.js` file for an example.
  100. # license
  101. MIT