added custom type to table class

This commit is contained in:
Phil Kunz 2015-12-20 23:14:22 +01:00
parent bbe7ac0a28
commit 9c72b86d29
6 changed files with 80 additions and 26 deletions

View File

@ -30,14 +30,29 @@ The plugin produces beautiful output like this:
### Console Tables ### Console Tables
beautylog allows displaying data in nice tables for better overview. beautylog allows displaying data in nice tables for better overview.
There are different types of tables.
#### Custom
```javascript ```javascript
var bl = require('beautylog')("os"); //for use in OS console environment 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 1","success"]); // adds a row the myTable
myTable.push(["check 2","error"]); // 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.push(["check 3","error"]); // adds a row the myTable
myTable.print(); //prints myTable to the console 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) ![table.png](https://mediaserve.lossless.digital/github.com/pushrocks/beautylog/table.png)

View File

@ -1,23 +1,37 @@
/// <reference path="./index.ts" /> /// <reference path="./index.ts" />
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 () { var ConsoleTable = (function () {
function ConsoleTable(tableType) { function ConsoleTable(tableTypeArg, tableHeadArrayArg) {
switch (tableType) { if (tableHeadArrayArg === void 0) { tableHeadArrayArg = tableHelpers.makeRow(); }
switch (tableTypeArg) {
case "checks": case "checks":
this.tableHead = ['Check Item:'.cyan, 'Status:'.cyan]; this.tableHead = ['Check Item:'.cyan, 'Status:'.cyan];
break; break;
case "custom":
this.tableHead = tableHeadArrayArg;
break;
default: default:
break; break;
} }
this.rows = []; this.rows = [];
this.type = tableType; this.type = tableTypeArg;
} }
ConsoleTable.prototype.push = function (row) { ConsoleTable.prototype.push = function (row) {
this.rows.push(row); this.rows.push(row);
}; };
ConsoleTable.prototype.print = function () { ConsoleTable.prototype.print = function () {
var table = new BeautylogOsTable.cliTable({ var table = new BeautylogOsTable.cliTable({
head: this.tableHead, head: this.tableHead
colWidths: [20, 20]
}); });
for (var row in this.rows) { for (var row in this.rows) {
if (this.rows[row][1] == "success") { if (this.rows[row][1] == "success") {
@ -154,8 +168,8 @@ var BeautylogOsTable;
function init() { function init() {
BeautylogOsTable.cliTable = require("cli-table2"); BeautylogOsTable.cliTable = require("cli-table2");
var beautylogOsTable = {}; var beautylogOsTable = {};
beautylogOsTable.new = function (type) { beautylogOsTable.new = function (typeArg, tableHeadArrayArg) {
var newConsoleTable = new ConsoleTable(type); var newConsoleTable = new ConsoleTable(typeArg, tableHeadArrayArg);
return newConsoleTable; return newConsoleTable;
}; };
return beautylogOsTable; return beautylogOsTable;

15
test.js
View File

@ -26,9 +26,14 @@ console.log("*** start browser console test (Might look weird in OS console and
beautyLogBrowser.log("hello"); beautyLogBrowser.log("hello");
console.log("*** end browser console test ***"); console.log("*** end browser console test ***");
console.log("*** start table test ***"); console.log("*** start table test ***");
var testTable = beautyLogOs.table.new("checks"); (function () {
testTable.push(['check1', 'success']); var testTable1 = beautyLogOs.table.new("checks");
testTable.push(['check2', 'error']); testTable1.push(['check1', 'success']);
testTable.push(['check3', 'error']); testTable1.push(['check2', 'error']);
testTable.print(); 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 ***"); console.log("*** end table test ***");

View File

@ -1,27 +1,38 @@
/// <reference path="./index.ts" /> /// <reference path="./index.ts" />
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 { class ConsoleTable {
tableHead:string[]; tableHead:string[];
rows; rows;
type:string; type:string;
constructor(tableType:string) { constructor(tableTypeArg:string,tableHeadArrayArg:string[] = tableHelpers.makeRow()) {
switch (tableType) { switch (tableTypeArg) {
case "checks": case "checks":
this.tableHead = ['Check Item:'.cyan,'Status:'.cyan]; this.tableHead = ['Check Item:'.cyan,'Status:'.cyan];
break; break;
case "custom":
this.tableHead = tableHeadArrayArg;
break;
default: default:
break; break;
} }
this.rows = []; this.rows = [];
this.type = tableType; this.type = tableTypeArg;
} }
push(row:string[]){ push(row:string[]){
this.rows.push(row); this.rows.push(row);
} }
print() { print() {
var table = new BeautylogOsTable.cliTable({ var table = new BeautylogOsTable.cliTable({
head: this.tableHead, head: this.tableHead
colWidths: [20, 20]
}); });
for (var row in this.rows){ for (var row in this.rows){
if(this.rows[row][1] == "success"){ if(this.rows[row][1] == "success"){

View File

@ -5,8 +5,8 @@ module BeautylogOsTable {
cliTable = require("cli-table2"); cliTable = require("cli-table2");
var beautylogOsTable:any = {}; var beautylogOsTable:any = {};
beautylogOsTable.new = function(type:string) { beautylogOsTable.new = function(typeArg:string,tableHeadArrayArg?) {
var newConsoleTable = new ConsoleTable(type); var newConsoleTable = new ConsoleTable(typeArg,tableHeadArrayArg);
return newConsoleTable; return newConsoleTable;
}; };
return beautylogOsTable; return beautylogOsTable;

View File

@ -33,9 +33,18 @@ beautyLogBrowser.log("hello");
console.log("*** end browser console test ***"); console.log("*** end browser console test ***");
console.log("*** start table test ***"); console.log("*** start table test ***");
var testTable = beautyLogOs.table.new("checks"); (function(){
testTable.push(['check1','success']); var testTable1 = beautyLogOs.table.new("checks");
testTable.push(['check2','error']); testTable1.push(['check1','success']);
testTable.push(['check3','error']); testTable1.push(['check2','error']);
testTable.print(); 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 ***"); console.log("*** end table test ***");