From 9c72b86d293d0d767e7e4da168f7a897ba24da22 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sun, 20 Dec 2015 23:14:22 +0100 Subject: [PATCH] added custom type to table class --- README.md | 19 +++++++++++++++++-- index.js | 28 +++++++++++++++++++++------- test.js | 15 ++++++++++----- ts/beautylog.classes.ts | 21 ++++++++++++++++----- ts/beautylog.os.table.ts | 4 ++-- ts/test.ts | 19 ++++++++++++++----- 6 files changed, 80 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 7bf70b2..3f9ea6e 100644 --- a/README.md +++ b/README.md @@ -30,14 +30,29 @@ The plugin produces beautiful output like this: ### Console Tables beautylog allows displaying data in nice tables for better overview. +There are different types of tables. + +#### Custom + ```javascript var bl = require('beautylog')("os"); //for use in OS console environment -var myTable = bl.table.new("checks"); //you can specify a format like "checks" to trigger things like the green and red badges +var myTable = bl.table.new("custom",["Heading1".blue,"Heading2".blue,"Heading3".blue]); // type "custom" myTable.push(["check 1","success"]); // adds a row the myTable myTable.push(["check 2","error"]); // adds a row the myTable myTable.push(["check 3","error"]); // adds a row the myTable myTable.print(); //prints myTable to the console ``` -The table from the code above looks like this: +#### Checks + +```javascript +var bl = require('beautylog')("os"); //for use in OS console environment +var myTable = bl.table.new("checks"); // type checks +myTable.push(["check 1","success"]); // adds a row the myTable +myTable.push(["check 2","error"]); // adds a row the myTable +myTable.push(["check 3","error"]); // adds a row the myTable +myTable.print(); //prints myTable to the console +``` + +The table from the code with type "checks" above looks like this: ![table.png](https://mediaserve.lossless.digital/github.com/pushrocks/beautylog/table.png) \ No newline at end of file diff --git a/index.js b/index.js index e436521..f961b84 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,37 @@ /// +var tableHelpers = { + makeRow: function (cellCounterArg, colorArg) { + if (cellCounterArg === void 0) { cellCounterArg = 2; } + if (colorArg === void 0) { colorArg = "cyan"; } + var rowArray = []; + for (var i = 0; i < (cellCounterArg); i++) { + rowArray.push(String(i + 1).cyan); + } + return rowArray; + } +}; var ConsoleTable = (function () { - function ConsoleTable(tableType) { - switch (tableType) { + function ConsoleTable(tableTypeArg, tableHeadArrayArg) { + if (tableHeadArrayArg === void 0) { tableHeadArrayArg = tableHelpers.makeRow(); } + switch (tableTypeArg) { case "checks": this.tableHead = ['Check Item:'.cyan, 'Status:'.cyan]; break; + case "custom": + this.tableHead = tableHeadArrayArg; + break; default: break; } this.rows = []; - this.type = tableType; + this.type = tableTypeArg; } ConsoleTable.prototype.push = function (row) { this.rows.push(row); }; ConsoleTable.prototype.print = function () { var table = new BeautylogOsTable.cliTable({ - head: this.tableHead, - colWidths: [20, 20] + head: this.tableHead }); for (var row in this.rows) { if (this.rows[row][1] == "success") { @@ -154,8 +168,8 @@ var BeautylogOsTable; function init() { BeautylogOsTable.cliTable = require("cli-table2"); var beautylogOsTable = {}; - beautylogOsTable.new = function (type) { - var newConsoleTable = new ConsoleTable(type); + beautylogOsTable.new = function (typeArg, tableHeadArrayArg) { + var newConsoleTable = new ConsoleTable(typeArg, tableHeadArrayArg); return newConsoleTable; }; return beautylogOsTable; diff --git a/test.js b/test.js index e700c10..c9d27c4 100644 --- a/test.js +++ b/test.js @@ -26,9 +26,14 @@ console.log("*** start browser console test (Might look weird in OS console and beautyLogBrowser.log("hello"); console.log("*** end browser console test ***"); console.log("*** start table test ***"); -var testTable = beautyLogOs.table.new("checks"); -testTable.push(['check1', 'success']); -testTable.push(['check2', 'error']); -testTable.push(['check3', 'error']); -testTable.print(); +(function () { + var testTable1 = beautyLogOs.table.new("checks"); + testTable1.push(['check1', 'success']); + testTable1.push(['check2', 'error']); + testTable1.push(['check3', 'error']); + testTable1.print(); + var testTable2 = beautyLogOs.table.new("custom", ["Column1".red, "Column2".blue, "Column3".cyan]); + testTable2.push(["Hey", "this", "works"]); + testTable2.print(); +})(); console.log("*** end table test ***"); diff --git a/ts/beautylog.classes.ts b/ts/beautylog.classes.ts index 7bbca91..98a2d8c 100644 --- a/ts/beautylog.classes.ts +++ b/ts/beautylog.classes.ts @@ -1,27 +1,38 @@ /// +var tableHelpers = { + makeRow: function(cellCounterArg:number = 2,colorArg:string = "cyan"){ + var rowArray = []; + for (var i = 0; i < (cellCounterArg); i++) { + rowArray.push(String(i + 1).cyan); + } + return rowArray; + } +}; class ConsoleTable { tableHead:string[]; rows; type:string; - constructor(tableType:string) { - switch (tableType) { + constructor(tableTypeArg:string,tableHeadArrayArg:string[] = tableHelpers.makeRow()) { + switch (tableTypeArg) { case "checks": this.tableHead = ['Check Item:'.cyan,'Status:'.cyan]; break; + case "custom": + this.tableHead = tableHeadArrayArg; + break; default: break; } this.rows = []; - this.type = tableType; + this.type = tableTypeArg; } push(row:string[]){ this.rows.push(row); } print() { var table = new BeautylogOsTable.cliTable({ - head: this.tableHead, - colWidths: [20, 20] + head: this.tableHead }); for (var row in this.rows){ if(this.rows[row][1] == "success"){ diff --git a/ts/beautylog.os.table.ts b/ts/beautylog.os.table.ts index b5f1ce3..e4a2e7b 100644 --- a/ts/beautylog.os.table.ts +++ b/ts/beautylog.os.table.ts @@ -5,8 +5,8 @@ module BeautylogOsTable { cliTable = require("cli-table2"); var beautylogOsTable:any = {}; - beautylogOsTable.new = function(type:string) { - var newConsoleTable = new ConsoleTable(type); + beautylogOsTable.new = function(typeArg:string,tableHeadArrayArg?) { + var newConsoleTable = new ConsoleTable(typeArg,tableHeadArrayArg); return newConsoleTable; }; return beautylogOsTable; diff --git a/ts/test.ts b/ts/test.ts index 798f40c..5cd3268 100644 --- a/ts/test.ts +++ b/ts/test.ts @@ -33,9 +33,18 @@ beautyLogBrowser.log("hello"); console.log("*** end browser console test ***"); console.log("*** start table test ***"); -var testTable = beautyLogOs.table.new("checks"); -testTable.push(['check1','success']); -testTable.push(['check2','error']); -testTable.push(['check3','error']); -testTable.print(); +(function(){ + var testTable1 = beautyLogOs.table.new("checks"); + testTable1.push(['check1','success']); + testTable1.push(['check2','error']); + testTable1.push(['check3','error']); + testTable1.print(); + + var testTable2 = beautyLogOs.table.new("custom",["Column1".red,"Column2".blue,"Column3".cyan]); + testTable2.push(["Hey","this","works"]); + testTable2.print(); +})(); + + + console.log("*** end table test ***"); \ No newline at end of file