项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <a name="table"></a>
  2. # Table
  3. [![Travis build status](http://img.shields.io/travis/gajus/table/master.svg?style=flat)](https://travis-ci.org/gajus/table)
  4. [![NPM version](http://img.shields.io/npm/v/table.svg?style=flat)](https://www.npmjs.com/package/table)
  5. [![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-brightgreen.svg?style=flat)](https://github.com/gajus/canonical)
  6. * [Table](#table)
  7. * [Features](#table-features)
  8. * [Usage](#table-usage)
  9. * [Cell Content Alignment](#table-usage-cell-content-alignment)
  10. * [Column Width](#table-usage-column-width)
  11. * [Custom Border](#table-usage-custom-border)
  12. * [Draw Horizontal Line](#table-usage-draw-horizontal-line)
  13. * [Padding Cell Content](#table-usage-padding-cell-content)
  14. * [Predefined Border Templates](#table-usage-predefined-border-templates)
  15. * [Streaming](#table-usage-streaming)
  16. * [Text Truncation](#table-usage-text-truncation)
  17. * [Text Wrapping](#table-usage-text-wrapping)
  18. Produces a string that represents array data in a text table.
  19. ![Demo of table displaying a list of missions to the Moon.](./.README/demo.png)
  20. <a name="table-features"></a>
  21. ## Features
  22. * Works with strings containing [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) characters.
  23. * Works with strings containing [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code).
  24. * Configurable border characters.
  25. * Configurable content alignment per column.
  26. * Configurable content padding per column.
  27. * Configurable column width.
  28. * Text wrapping.
  29. <a name="table-usage"></a>
  30. ## Usage
  31. Table data is described using an array (rows) of array (cells).
  32. ```js
  33. import {
  34. table
  35. } from 'table';
  36. // Using commonjs?
  37. // const {table} = require('table');
  38. let data,
  39. output;
  40. data = [
  41. ['0A', '0B', '0C'],
  42. ['1A', '1B', '1C'],
  43. ['2A', '2B', '2C']
  44. ];
  45. /**
  46. * @typedef {string} table~cell
  47. */
  48. /**
  49. * @typedef {table~cell[]} table~row
  50. */
  51. /**
  52. * @typedef {Object} table~columns
  53. * @property {string} alignment Cell content alignment (enum: left, center, right) (default: left).
  54. * @property {number} width Column width (default: auto).
  55. * @property {number} truncate Number of characters are which the content will be truncated (default: Infinity).
  56. * @property {number} paddingLeft Cell content padding width left (default: 1).
  57. * @property {number} paddingRight Cell content padding width right (default: 1).
  58. */
  59. /**
  60. * @typedef {Object} table~border
  61. * @property {string} topBody
  62. * @property {string} topJoin
  63. * @property {string} topLeft
  64. * @property {string} topRight
  65. * @property {string} bottomBody
  66. * @property {string} bottomJoin
  67. * @property {string} bottomLeft
  68. * @property {string} bottomRight
  69. * @property {string} bodyLeft
  70. * @property {string} bodyRight
  71. * @property {string} bodyJoin
  72. * @property {string} joinBody
  73. * @property {string} joinLeft
  74. * @property {string} joinRight
  75. * @property {string} joinJoin
  76. */
  77. /**
  78. * Used to dynamically tell table whether to draw a line separating rows or not.
  79. * The default behavior is to always return true.
  80. *
  81. * @typedef {function} drawJoin
  82. * @param {number} index
  83. * @param {number} size
  84. * @return {boolean}
  85. */
  86. /**
  87. * @typedef {Object} table~config
  88. * @property {table~border} border
  89. * @property {table~columns[]} columns Column specific configuration.
  90. * @property {table~columns} columnDefault Default values for all columns. Column specific settings overwrite the default values.
  91. * @property {table~drawJoin} drawHorizontalLine
  92. */
  93. /**
  94. * Generates a text table.
  95. *
  96. * @param {table~row[]} rows
  97. * @param {table~config} config
  98. * @return {String}
  99. */
  100. output = table(data);
  101. console.log(output);
  102. ```
  103. ```
  104. ╔════╤════╤════╗
  105. ║ 0A │ 0B │ 0C ║
  106. ╟────┼────┼────╢
  107. ║ 1A │ 1B │ 1C ║
  108. ╟────┼────┼────╢
  109. ║ 2A │ 2B │ 2C ║
  110. ╚════╧════╧════╝
  111. ```
  112. <a name="table-usage-cell-content-alignment"></a>
  113. ### Cell Content Alignment
  114. `{string} config.columns[{number}].alignment` property controls content horizontal alignment within a cell.
  115. Valid values are: "left", "right" and "center".
  116. ```js
  117. let config,
  118. data,
  119. output;
  120. data = [
  121. ['0A', '0B', '0C'],
  122. ['1A', '1B', '1C'],
  123. ['2A', '2B', '2C']
  124. ];
  125. config = {
  126. columns: {
  127. 0: {
  128. alignment: 'left',
  129. minWidth: 10
  130. },
  131. 1: {
  132. alignment: 'center',
  133. minWidth: 10
  134. },
  135. 2: {
  136. alignment: 'right',
  137. minWidth: 10
  138. }
  139. }
  140. };
  141. output = table(data, config);
  142. console.log(output);
  143. ```
  144. ```
  145. ╔════════════╤════════════╤════════════╗
  146. ║ 0A │ 0B │ 0C ║
  147. ╟────────────┼────────────┼────────────╢
  148. ║ 1A │ 1B │ 1C ║
  149. ╟────────────┼────────────┼────────────╢
  150. ║ 2A │ 2B │ 2C ║
  151. ╚════════════╧════════════╧════════════╝
  152. ```
  153. <a name="table-usage-column-width"></a>
  154. ### Column Width
  155. `{number} config.columns[{number}].width` property restricts column width to a fixed width.
  156. ```js
  157. let data,
  158. output,
  159. options;
  160. data = [
  161. ['0A', '0B', '0C'],
  162. ['1A', '1B', '1C'],
  163. ['2A', '2B', '2C']
  164. ];
  165. options = {
  166. columns: {
  167. 1: {
  168. width: 10
  169. }
  170. }
  171. };
  172. output = table(data, options);
  173. console.log(output);
  174. ```
  175. ```
  176. ╔════╤════════════╤════╗
  177. ║ 0A │ 0B │ 0C ║
  178. ╟────┼────────────┼────╢
  179. ║ 1A │ 1B │ 1C ║
  180. ╟────┼────────────┼────╢
  181. ║ 2A │ 2B │ 2C ║
  182. ╚════╧════════════╧════╝
  183. ```
  184. <a name="table-usage-custom-border"></a>
  185. ### Custom Border
  186. `{object} config.border` property describes characters used to draw the table border.
  187. ```js
  188. let config,
  189. data,
  190. output;
  191. data = [
  192. ['0A', '0B', '0C'],
  193. ['1A', '1B', '1C'],
  194. ['2A', '2B', '2C']
  195. ];
  196. config = {
  197. border: {
  198. topBody: `─`,
  199. topJoin: `┬`,
  200. topLeft: `┌`,
  201. topRight: `┐`,
  202. bottomBody: `─`,
  203. bottomJoin: `┴`,
  204. bottomLeft: `└`,
  205. bottomRight: `┘`,
  206. bodyLeft: `│`,
  207. bodyRight: `│`,
  208. bodyJoin: `│`,
  209. joinBody: `─`,
  210. joinLeft: `├`,
  211. joinRight: `┤`,
  212. joinJoin: `┼`
  213. }
  214. };
  215. output = table(data, config);
  216. console.log(output);
  217. ```
  218. ```
  219. ┌────┬────┬────┐
  220. │ 0A │ 0B │ 0C │
  221. ├────┼────┼────┤
  222. │ 1A │ 1B │ 1C │
  223. ├────┼────┼────┤
  224. │ 2A │ 2B │ 2C │
  225. └────┴────┴────┘
  226. ```
  227. <a name="table-usage-draw-horizontal-line"></a>
  228. ### Draw Horizontal Line
  229. `{function} config.drawHorizontalLine` property is a function that is called for every non-content row in the table. The result of the function `{boolean}` determines whether a row is drawn.
  230. ```js
  231. let data,
  232. output,
  233. options;
  234. data = [
  235. ['0A', '0B', '0C'],
  236. ['1A', '1B', '1C'],
  237. ['2A', '2B', '2C'],
  238. ['3A', '3B', '3C'],
  239. ['4A', '4B', '4C']
  240. ];
  241. options = {
  242. /**
  243. * @typedef {function} drawJoin
  244. * @param {number} index
  245. * @param {number} size
  246. * @return {boolean}
  247. */
  248. drawHorizontalLine: (index, size) => {
  249. return index === 0 || index === 1 || index === size - 1 || index === size;
  250. }
  251. };
  252. output = table(data, options);
  253. console.log(output);
  254. ```
  255. ```
  256. ╔════╤════╤════╗
  257. ║ 0A │ 0B │ 0C ║
  258. ╟────┼────┼────╢
  259. ║ 1A │ 1B │ 1C ║
  260. ║ 2A │ 2B │ 2C ║
  261. ║ 3A │ 3B │ 3C ║
  262. ╟────┼────┼────╢
  263. ║ 4A │ 4B │ 4C ║
  264. ╚════╧════╧════╝
  265. ```
  266. <a name="table-usage-padding-cell-content"></a>
  267. ### Padding Cell Content
  268. `{number} config.columns[{number}].paddingLeft` and `{number} config.columns[{number}].paddingRight` properties control content padding within a cell. Property value represents a number of whitespaces used to pad the content.
  269. ```js
  270. let config,
  271. data,
  272. output;
  273. data = [
  274. ['0A', 'AABBCC', '0C'],
  275. ['1A', '1B', '1C'],
  276. ['2A', '2B', '2C']
  277. ];
  278. config = {
  279. columns: {
  280. 0: {
  281. paddingLeft: 3
  282. },
  283. 1: {
  284. width: 2,
  285. paddingRight: 3
  286. }
  287. }
  288. };
  289. output = table(data, config);
  290. console.log(output);
  291. ```
  292. ```
  293. ╔══════╤══════╤════╗
  294. ║ 0A │ AA │ 0C ║
  295. ║ │ BB │ ║
  296. ║ │ CC │ ║
  297. ╟──────┼──────┼────╢
  298. ║ 1A │ 1B │ 1C ║
  299. ╟──────┼──────┼────╢
  300. ║ 2A │ 2B │ 2C ║
  301. ╚══════╧══════╧════╝
  302. ```
  303. <a name="table-usage-predefined-border-templates"></a>
  304. ### Predefined Border Templates
  305. You can load one of the predefined border templates using `getBorderCharacters` function.
  306. ```js
  307. import {
  308. table,
  309. getBorderCharacters
  310. } from 'table';
  311. let config,
  312. data;
  313. data = [
  314. ['0A', '0B', '0C'],
  315. ['1A', '1B', '1C'],
  316. ['2A', '2B', '2C']
  317. ];
  318. config = {
  319. border: getBorderCharacters(`name of the template`)
  320. };
  321. table(data, config);
  322. ```
  323. ```
  324. # honeywell
  325. ╔════╤════╤════╗
  326. ║ 0A │ 0B │ 0C ║
  327. ╟────┼────┼────╢
  328. ║ 1A │ 1B │ 1C ║
  329. ╟────┼────┼────╢
  330. ║ 2A │ 2B │ 2C ║
  331. ╚════╧════╧════╝
  332. # norc
  333. ┌────┬────┬────┐
  334. │ 0A │ 0B │ 0C │
  335. ├────┼────┼────┤
  336. │ 1A │ 1B │ 1C │
  337. ├────┼────┼────┤
  338. │ 2A │ 2B │ 2C │
  339. └────┴────┴────┘
  340. # ramac (ASCII; for use in terminals that do not support Unicode characters)
  341. +----+----+----+
  342. | 0A | 0B | 0C |
  343. |----|----|----|
  344. | 1A | 1B | 1C |
  345. |----|----|----|
  346. | 2A | 2B | 2C |
  347. +----+----+----+
  348. # void (no borders; see "bordless table" section of the documentation)
  349. 0A 0B 0C
  350. 1A 1B 1C
  351. 2A 2B 2C
  352. ```
  353. Raise [an issue](https://github.com/gajus/table/issues) if you'd like to contribute a new border template.
  354. <a name="table-usage-predefined-border-templates-borderless-table"></a>
  355. #### Borderless Table
  356. Simply using "void" border character template creates a table with a lot of unnecessary spacing.
  357. To create a more plesant to the eye table, reset the padding and remove the joining rows, e.g.
  358. ```js
  359. let output;
  360. output = table(data, {
  361. border: getBorderCharacters(`void`),
  362. columnDefault: {
  363. paddingLeft: 0,
  364. paddingRight: 1
  365. },
  366. drawJoin: () => {
  367. return false
  368. }
  369. });
  370. console.log(output);
  371. ```
  372. ```
  373. 0A 0B 0C
  374. 1A 1B 1C
  375. 2A 2B 2C
  376. ```
  377. <a name="table-usage-streaming"></a>
  378. ### Streaming
  379. `table` package exports `createStream` function used to draw a table and append rows.
  380. `createStream` requires `{number} columnDefault.width` and `{number} columnCount` configuration properties.
  381. ```js
  382. import {
  383. createStream
  384. } from 'table';
  385. let config,
  386. stream;
  387. config = {
  388. columnDefault: {
  389. width: 50
  390. },
  391. columnCount: 1
  392. };
  393. stream = createStream(config);
  394. setInterval(() => {
  395. stream.write([new Date()]);
  396. }, 500);
  397. ```
  398. ![Streaming current date.](./.README/streaming.gif)
  399. `table` package uses ANSI escape codes to overwrite the output of the last line when a new row is printed.
  400. The underlying implementation is explained in this [Stack Overflow answer](http://stackoverflow.com/a/32938658/368691).
  401. Streaming supports all of the configuration properties and functionality of a static table (such as auto text wrapping, alignment and padding), e.g.
  402. ```js
  403. import {
  404. createStream
  405. } from 'table';
  406. import _ from 'lodash';
  407. let config,
  408. stream,
  409. i;
  410. config = {
  411. columnDefault: {
  412. width: 50
  413. },
  414. columnCount: 3,
  415. columns: {
  416. 0: {
  417. width: 10,
  418. alignment: 'right'
  419. },
  420. 1: {
  421. alignment: 'center',
  422. },
  423. 2: {
  424. width: 10
  425. }
  426. }
  427. };
  428. stream = createStream(config);
  429. i = 0;
  430. setInterval(() => {
  431. let random;
  432. random = _.sample('abcdefghijklmnopqrstuvwxyz', _.random(1, 30)).join('');
  433. stream.write([i++, new Date(), random]);
  434. }, 500);
  435. ```
  436. ![Streaming random data.](./.README/streaming-random.gif)
  437. <a name="table-usage-text-truncation"></a>
  438. ### Text Truncation
  439. To handle a content that overflows the container width, `table` package implements [text wrapping](#table-usage-text-wrapping). However, sometimes you may want to truncate content that is too long to be displayed in the table.
  440. `{number} config.columns[{number}].truncate` property (default: `Infinity`) truncates the text at the specified length.
  441. ```js
  442. let config,
  443. data,
  444. output;
  445. data = [
  446. ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
  447. ];
  448. config = {
  449. columns: {
  450. 0: {
  451. width: 20,
  452. truncate: 100
  453. }
  454. }
  455. };
  456. output = table(data, config);
  457. console.log(output);
  458. ```
  459. ```
  460. ╔══════════════════════╗
  461. ║ Lorem ipsum dolor si ║
  462. ║ t amet, consectetur ║
  463. ║ adipiscing elit. Pha ║
  464. ║ sellus pulvinar nibh ║
  465. ║ sed mauris conva... ║
  466. ╚══════════════════════╝
  467. ```
  468. <a name="table-usage-text-wrapping"></a>
  469. ### Text Wrapping
  470. `table` package implements auto text wrapping, i.e. text that has width greater than the container width will be separated into multiple lines, e.g.
  471. ```js
  472. let config,
  473. data,
  474. output;
  475. data = [
  476. ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
  477. ];
  478. config = {
  479. columns: {
  480. 0: {
  481. width: 20
  482. }
  483. }
  484. };
  485. output = table(data, config);
  486. console.log(output);
  487. ```
  488. ```
  489. ╔══════════════════════╗
  490. ║ Lorem ipsum dolor si ║
  491. ║ t amet, consectetur ║
  492. ║ adipiscing elit. Pha ║
  493. ║ sellus pulvinar nibh ║
  494. ║ sed mauris convallis ║
  495. ║ dapibus. Nunc venena ║
  496. ║ tis tempus nulla sit ║
  497. ║ amet viverra. ║
  498. ╚══════════════════════╝
  499. ```
  500. When `wrapWord` is `true` the text is broken at the nearest space or one of the special characters ("-", "_", "\", "/", ".", ",", ";"), e.g.
  501. ```js
  502. let config,
  503. data,
  504. output;
  505. data = [
  506. ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
  507. ];
  508. config = {
  509. columns: {
  510. 0: {
  511. width: 20,
  512. wrapWord: true
  513. }
  514. }
  515. };
  516. output = table(data, config);
  517. console.log(output);
  518. ```
  519. ```
  520. ╔══════════════════════╗
  521. ║ Lorem ipsum dolor ║
  522. ║ sit amet, ║
  523. ║ consectetur ║
  524. ║ adipiscing elit. ║
  525. ║ Phasellus pulvinar ║
  526. ║ nibh sed mauris ║
  527. ║ convallis dapibus. ║
  528. ║ Nunc venenatis ║
  529. ║ tempus nulla sit ║
  530. ║ amet viverra. ║
  531. ╚══════════════════════╝
  532. ```