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
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)

View File

@ -1,23 +1,37 @@
/// <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 () {
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;

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");
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 ***");

View File

@ -1,27 +1,38 @@
/// <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 {
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"){

View File

@ -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;

View File

@ -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 ***");