Ohm-Management - Projektarbeit B-ME
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.

README.md 16KB

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