[Pkg-javascript-commits] [node-ansi-color-table] 01/02: Imported Upstream version 1.0.0
Jérémy Lal
kapouer at moszumanska.debian.org
Sat Jan 4 17:29:46 UTC 2014
This is an automated email from the git hooks/post-receive script.
kapouer pushed a commit to branch master
in repository node-ansi-color-table.
commit dc8604c0798a06c0cc2bdb99e156fb2d03e7f655
Author: Jérémy Lal <kapouer at melix.org>
Date: Sat Jan 4 18:27:27 2014 +0100
Imported Upstream version 1.0.0
---
.gitignore | 1 +
.travis.yml | 5 +++
LICENSE | 20 ++++++++++
README.md | 76 +++++++++++++++++++++++++++++++++++++
examples/custom-opts.js | 15 ++++++++
examples/redirect_stream.js | 21 +++++++++++
examples/users.js | 11 ++++++
index.js | 92 +++++++++++++++++++++++++++++++++++++++++++++
lib/padsegments.js | 23 ++++++++++++
package.json | 23 ++++++++++++
test/cells.js | 28 ++++++++++++++
test/padsegments.js | 57 ++++++++++++++++++++++++++++
12 files changed, 372 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..99cdc74
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.10"
+ - "0.11"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..4a09251
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Quim Calpe
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..1b68d30
--- /dev/null
+++ b/README.md
@@ -0,0 +1,76 @@
+ANSI COLOR TABLE
+================
+Tables for cli/stdout supporting colors using [ansi](https://github.com/TooTallNate/ansi.js) npm module.
+Based on [text-table](https://github.com/substack/text-table) but using ansi cursor, not color codes.
+
+[](https://travis-ci.org/quimcalpe/ansi-color-table)
+
+###Examples
+**Table with colors and default options**
+```javascript
+var table = require('ansi-color-table');
+
+var data = [
+ [ "Username".blue().underline(), "Email".blue().underline(), "active".blue().underline() ],
+ [ "john.smith", "john at gimail.com".yellow(), "yes".green() ],
+ [ "steve.balmes", "sbalmes at hotma.il".yellow(), "yes".green() ],
+ [ "james.durango", "james.durango at greenvine.com".yellow(), "no".red() ],
+ [ "elisa.polite", "epolite@".yellow(), "yes".green() ]
+];
+
+table(data);
+```
+**Custom align, custom separator, centered header**
+```javascript
+var table = require('../');
+
+var data = [
+ [ "Username".blue().underline(), "Email".blue().underline(), "Age".blue().underline(), "Active".blue().underline() ],
+ [ "john.smith", "john at gimail.com".yellow(), "9".white(), "yes".green() ],
+ [ "steve.balmes", "sbalmes at hotma.il".yellow(), "19".white(), "yes".green() ],
+ [ "james.durango", "james.durango at greenvine.com".yellow(), "99".white(), "no".red() ],
+ [ "elisa.polite", "epolite@".yellow(), "109".white(), "yes".green() ]
+];
+
+table(data, {
+ align : [ "left", "left", "right", "center" ],
+ separator : " | ",
+ headerAlign: "center"
+});
+```
+**Redirect output to custom stream intead of `process.stdout`**
+```javascript
+var table = require('../');
+var Stream = require('stream');
+
+var tableData = [
+ [ "Username".blue().underline(), "Email".blue().underline(), "active".blue().underline() ],
+ [ "john.smith", "john at gimail.com".yellow(), "yes".green() ],
+ [ "steve.balmes", "sbalmes at hotma.il".yellow(), "yes".green() ]
+];
+
+var output_stream = new Stream();
+var output_data = "";
+output_stream.write = function (data) {
+ output_data += data;
+};
+output_stream.end = function () {
+ // do something with data
+ console.log(output_data);
+};
+
+table(tableData, {}, output_stream);
+```
+### Installation
+With [npm](https://npmjs.org) do:
+```bash
+$ npm install ansi-color-table
+```
+
+### Testing
+```bash
+$ npm test
+```
+
+### License
+MIT
\ No newline at end of file
diff --git a/examples/custom-opts.js b/examples/custom-opts.js
new file mode 100644
index 0000000..8746fa4
--- /dev/null
+++ b/examples/custom-opts.js
@@ -0,0 +1,15 @@
+var table = require('../');
+
+var data = [
+ [ "Username".blue().underline(), "Email".blue().underline(), "Age".blue().underline(), "Active".blue().underline() ],
+ [ "john.smith", "john at gimail.com".yellow(), "9".white(), "yes".green() ],
+ [ "steve.balmes", "sbalmes at hotma.il".yellow(), "19".white(), "yes".green() ],
+ [ "james.durango", "james.durango at greenvine.com".yellow(), "99".white(), "no".red() ],
+ [ "elisa.polite", "epolite@".yellow(), "109".white(), "yes".green() ]
+];
+
+table(data, {
+ align : [ "left", "left", "right", "center" ],
+ separator : " | ",
+ headerAlign: "center"
+});
diff --git a/examples/redirect_stream.js b/examples/redirect_stream.js
new file mode 100644
index 0000000..9fe1b0f
--- /dev/null
+++ b/examples/redirect_stream.js
@@ -0,0 +1,21 @@
+var table = require('../');
+var Stream = require('stream');
+
+var tableData = [
+ [ "Username".blue().underline(), "Email".blue().underline(), "active".blue().underline() ],
+ [ "john.smith", "john at gimail.com".yellow(), "yes".green() ],
+ [ "steve.balmes", "sbalmes at hotma.il".yellow(), "yes".green() ]
+];
+
+var output_stream = new Stream();
+var output_data = "";
+output_stream.write = function (data) {
+ output_data += data;
+};
+output_stream.end = function () {
+ // do something with data
+ console.log(output_data);
+};
+
+table(tableData, {}, output_stream);
+
diff --git a/examples/users.js b/examples/users.js
new file mode 100644
index 0000000..f251633
--- /dev/null
+++ b/examples/users.js
@@ -0,0 +1,11 @@
+var table = require('../');
+
+var data = [
+ [ "Username".blue().underline(), "Email".blue().underline(), "active".blue().underline() ],
+ [ "john.smith", "john at gimail.com".yellow(), "yes".green() ],
+ [ "steve.balmes", "sbalmes at hotma.il".yellow(), "yes".green() ],
+ [ "james.durango", "james.durango at greenvine.com".yellow(), "no".red() ],
+ [ "elisa.polite", "epolite@".yellow(), "yes".green() ]
+];
+
+table(data);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..a56737d
--- /dev/null
+++ b/index.js
@@ -0,0 +1,92 @@
+var ansi = require("ansi");
+var padSegments = require("./lib/padsegments");
+
+var colors = [
+ "white",
+ "black",
+ "blue",
+ "cyan",
+ "green",
+ "magenta",
+ "red",
+ "yellow",
+ "grey",
+ "brightBlack",
+ "brightRed",
+ "brightGreen",
+ "brightYellow",
+ "brightBlue",
+ "brightMagenta",
+ "brightCyan",
+ "brightWhite"
+];
+var styles = [
+ "bold",
+ "italic",
+ "underline",
+ "inverse"
+];
+
+colors.forEach(function (color) {
+ String.prototype[color] = function () {
+ this._color = color;
+ return this;
+ };
+});
+styles.forEach(function (style) {
+ String.prototype[style] = function () {
+ if ( !this._styles ) this._styles = [];
+ this._styles.push(style);
+ return this;
+ };
+});
+
+function printTable(table, opts, stream) {
+ opts = opts || { align : [] };
+ if ( opts.align === undefined ) opts.align = [];
+ // compute row length
+ if ( table.length === 0 ) return;
+ // output stream
+ stream = stream || process.stdout;
+ var cursor = ansi(stream);
+ var numCols = table.reduce(function (maxCols, col) {
+ return ( col.length > maxCols ) ? col.length : maxCols;
+ }, 0);
+ var maxColumnLengths = Array(numCols);
+ for ( var i = 0; i < numCols; i++ ) maxColumnLengths[i] = 0;
+ table.map(function (row) {
+ var i = 0;
+ for ( ; i < numCols; i++ ) {
+ if ( row[i] !== undefined && row[i].length > maxColumnLengths[i] ) {
+ maxColumnLengths[i] = row[i].length;
+ }
+ }
+ });
+
+ // print table
+ var output = [];
+ var segments;
+ table.forEach(function (row, indexRow) {
+ row.forEach(function (col, indexCol) {
+ var align = ( opts.headerAlign && indexRow === 0 ) ? opts.headerAlign : opts.align[indexCol];
+ segments = padSegments(col.toString(), maxColumnLengths[indexCol], align);
+ stream.write(segments.left);
+ if ( col._color ) {
+ cursor[col._color]();
+ }
+ if ( col._styles ) {
+ col._styles.forEach( function (style) {
+ cursor[style]();
+ });
+ }
+ stream.write(col.toString());
+ cursor.reset();
+ stream.write(segments.right);
+ if ( indexCol < row.length-1 ) stream.write(opts.separator || " ");
+ });
+ stream.write("\n");
+ });
+ if ( stream != process.stdout ) stream.end();
+}
+
+module.exports = exports = printTable;
\ No newline at end of file
diff --git a/lib/padsegments.js b/lib/padsegments.js
new file mode 100644
index 0000000..6f2b3df
--- /dev/null
+++ b/lib/padsegments.js
@@ -0,0 +1,23 @@
+module.exports = exports = function (str, padLen, align, padChar) {
+ var left = "";
+ var right = "";
+ if ( str.length >= padLen ) return { left: left, right: right };
+ if ( padChar === undefined ) padChar = " ";
+ if ( padChar.length > 1 ) padChar = padChar.substr(0, 1);
+ switch ( align ) {
+ case "center":
+ var availableSlots = padLen - str.length;
+ right = Math.ceil(availableSlots / 2);
+ left = availableSlots - right;
+ right = Array(right+1).join(padChar);
+ left = Array(left+1).join(padChar);
+ break;
+ case "right":
+ left = Array(padLen + 1 - str.length).join(padChar);
+ break;
+ default:
+ right = Array(padLen + 1 - str.length).join(padChar);
+ break;
+ }
+ return { left: left, right: right };
+};
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..21d9515
--- /dev/null
+++ b/package.json
@@ -0,0 +1,23 @@
+{
+ "name": "ansi-color-table",
+ "version": "1.0.0",
+ "description": "Borderless tables for console with ansi colors and formatting support",
+ "main": "index.js",
+ "scripts": {
+ "test": "tap ./test",
+ "start": "node index.js"
+ },
+ "author": "Quim Calpe <quim at kalpe.com> (https://github.com/quimcalpe)",
+ "homepage": "https://github.com/quimcalpe/ansi-color-table",
+ "license": "MIT",
+ "dependencies": {
+ "ansi": "~0.2.1"
+ },
+ "devDependencies": {
+ "tap": "~0.4.6"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/quimcalpe/ansi-color-table.git"
+ }
+}
diff --git a/test/cells.js b/test/cells.js
new file mode 100644
index 0000000..f8ebe0c
--- /dev/null
+++ b/test/cells.js
@@ -0,0 +1,28 @@
+var test = require('tap').test;
+var table = require('../');
+var Stream = require('stream');
+var tableData = [
+ [ "Username".blue().underline(), "Email".blue().underline(), "active".blue().underline() ],
+ [ "john.smith", "john at gimail.com".yellow(), "yes".green() ],
+ [ "steve.balmes", "sbalmes at hotma.il".yellow(), "yes".green() ],
+ [ "james.durango", "james.durango at greenvine.com".yellow(), "no".red() ],
+ [ "elisa.polite", "epolite at 1234.com".yellow(), "yes".green() ]
+];
+
+test('Read content from random cells', function (t) {
+ t.plan(2);
+ var output = new Stream();
+ output.writable = true;
+ output.write = function (data) {
+ console.log("write"+data);
+ if ( data == tableData[0][0] && data == "Username" ) {
+ t.pass();
+ }
+ if ( data == tableData[4][1] && data == "epolite at 1234.com" ) {
+ t.pass();
+ }
+ };
+ output.end = function () {};
+ table(tableData, {}, output);
+});
+
diff --git a/test/padsegments.js b/test/padsegments.js
new file mode 100644
index 0000000..94c6e87
--- /dev/null
+++ b/test/padsegments.js
@@ -0,0 +1,57 @@
+var test = require('tap').test;
+var padSegments = require('../lib/padsegments');
+
+test('Left align', function (t) {
+ var string = "Hello World!";
+ var segments = padSegments(string, 15);
+ t.equal(segments.left, "");
+ t.equal(segments.right, " ");
+ segments = padSegments(string, 15, "left");
+ t.equal(segments.left, "");
+ t.equal(segments.right, " ");
+ t.end();
+});
+
+test('Right align', function (t) {
+ var string = "Hello World!";
+ var segments = padSegments(string, 15, "right");
+ t.equal(segments.left, " ");
+ t.equal(segments.right, "");
+ t.end();
+});
+
+test('Center align', function (t) {
+ var string = "Hello World!";
+ var segments = padSegments(string, 15, "center");
+ t.equal(segments.left, " ");
+ t.equal(segments.right, " ");
+ segments = padSegments(string, 16, "center");
+ t.equal(segments.left, " ");
+ t.equal(segments.right, " ");
+ t.end();
+});
+
+test('String bigger than demanded pad', function (t) {
+ var string = "Hello World!";
+ var segments = padSegments(string, 10);
+ t.equal(segments.left, "");
+ t.equal(segments.right, "");
+ t.end();
+});
+
+test('Custom padChar', function (t) {
+ var string = "Hello World!";
+ var segments = padSegments(string, 15, "left", "*");
+ t.equal(segments.left, "");
+ t.equal(segments.right, "***");
+ t.end();
+});
+
+test('Custom padChar discard extra-chars', function (t) {
+ var string = "Hello World!";
+ var segments = padSegments(string, 15, "left", "AB");
+ t.equal(segments.left, "");
+ t.equal(segments.right, "AAA");
+ t.end();
+});
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ansi-color-table.git
More information about the Pkg-javascript-commits
mailing list