add code function

This commit is contained in:
Philipp Kunz 2016-01-30 06:19:44 +01:00
parent 1f9a2257fc
commit ea06f32ce0
14 changed files with 384 additions and 141 deletions

View File

@ -26,6 +26,10 @@ bl.log('some error message','error');
The plugin produces beautiful output like this: The plugin produces beautiful output like this:
![console.png](https://mediaserve.lossless.digital/github.com/pushrocks/beautylog/console.png) ![console.png](https://mediaserve.lossless.digital/github.com/pushrocks/beautylog/console.png)
### Code Highlighting
### Console Tables ### Console Tables
beautylog allows displaying data in nice tables for better overview. beautylog allows displaying data in nice tables for better overview.
@ -54,4 +58,5 @@ myTable.print(); //prints myTable to the console
``` ```
The table from the code with type "checks" above looks like this: 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)

120
code.css Normal file
View File

@ -0,0 +1,120 @@
.hljs {
display: block;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}
.hljs-comment,
.hljs-template_comment,
.diff .hljs-header,
.hljs-javadoc {
color: #998;
font-style: italic;
}
.hljs-keyword,
.css .rule .hljs-keyword,
.hljs-winutils,
.javascript .hljs-title,
.nginx .hljs-title,
.hljs-subst,
.hljs-request,
.hljs-status {
color: #333;
font-weight: bold;
}
.hljs-number,
.hljs-hexcolor,
.ruby .hljs-constant {
color: #099;
}
.hljs-string,
.hljs-tag .hljs-value,
.hljs-phpdoc,
.tex .hljs-formula {
color: #d14;
}
.hljs-title,
.hljs-id,
.coffeescript .hljs-params,
.scss .hljs-preprocessor {
color: #900;
font-weight: bold;
}
.javascript .hljs-title,
.lisp .hljs-title,
.clojure .hljs-title,
.hljs-subst {
font-weight: normal;
}
.hljs-class .hljs-title,
.haskell .hljs-type,
.vhdl .hljs-literal,
.tex .hljs-command {
color: #458;
font-weight: bold;
}
.hljs-tag,
.hljs-tag .hljs-title,
.hljs-rules .hljs-property,
.django .hljs-tag .hljs-keyword {
color: #000080;
font-weight: normal;
}
.hljs-attribute,
.hljs-variable,
.lisp .hljs-body {
color: #008080;
}
.hljs-regexp {
color: #009926;
}
.hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.lisp .hljs-keyword,
.tex .hljs-special,
.hljs-prompt {
color: #990073;
}
.hljs-built_in,
.lisp .hljs-title,
.clojure .hljs-built_in {
color: #0086b3;
}
.hljs-preprocessor,
.hljs-pragma,
.hljs-pi,
.hljs-doctype,
.hljs-shebang,
.hljs-cdata {
color: #999;
font-weight: bold;
}
.hljs-deletion {
background: #fdd;
}
.hljs-addition {
background: #dfd;
}
.diff .hljs-change {
background: #0086b3;
}
.hljs-chunk {
color: #aaa;
}

16
index.d.ts vendored
View File

@ -1,8 +1,6 @@
/// <reference path="ts/typings/tsd.d.ts" /> /// <reference path="ts/typings/tsd.d.ts" />
declare module BeautylogPlugins { declare module BeautylogPlugins {
var init: () => { var init: () => any;
smartenv: any;
};
} }
declare var tableHelpers: { declare var tableHelpers: {
makeRow: (cellCounterArg?: number, colorArg?: string) => any[]; makeRow: (cellCounterArg?: number, colorArg?: string) => any[];
@ -18,14 +16,18 @@ declare class ConsoleTable {
declare module BeautylogNode { declare module BeautylogNode {
function init(): any; function init(): any;
} }
declare module BeautylogOsTable { declare module BeautylogNodeLog {
var init: () => (logText?: string, logType?: string) => boolean;
}
declare module BeautylogNodeCode {
var init: () => (codeString: any, options?: any) => void;
}
declare module BeautylogNodeTable {
var cliTable: any; var cliTable: any;
function init(): any; function init(): any;
} }
declare module BeautylogBrowser { declare module BeautylogBrowser {
function init(): any; function init(): any;
} }
declare var plugins: { declare var plugins: any;
smartenv: any;
};
declare var beautylog: any; declare var beautylog: any;

180
index.js
View File

@ -42,7 +42,7 @@ var ConsoleTable = (function () {
this.rows.push(row); this.rows.push(row);
}; };
ConsoleTable.prototype.print = function () { ConsoleTable.prototype.print = function () {
var table = new BeautylogOsTable.cliTable({ var table = new BeautylogNodeTable.cliTable({
head: this.tableHead head: this.tableHead
}); });
for (var row in this.rows) { for (var row in this.rows) {
@ -63,64 +63,12 @@ var ConsoleTable = (function () {
var BeautylogNode; var BeautylogNode;
(function (BeautylogNode) { (function (BeautylogNode) {
function init() { function init() {
var colors = require("colors"); plugins.colors = require("colors");
var clc = require("cli-color"); plugins.clc = require("cli-color");
var beautylogNode = {}; //object to append to all public facing functions var beautylogNode = {
var localBl; // object to append to all private params and functions log: BeautylogNodeLog.init(),
localBl = {}; code: BeautylogNodeCode.init()
localBl.dirPrefix = clc.bgXterm(39).xterm(231).bold(' DIR ') + ' '; }; //object to append to all public facing functions
localBl.errorPrefix = ' Error: '.bgRed.white.bold + ' ';
localBl.infoPrefix = clc.bgXterm(198).xterm(231).bold(' INFO ') + ' ';
localBl.normalPrefix = ' Log: '.bgCyan.white.bold + ' ';
localBl.okPrefix = ' '.bgGreen + ' OK! '.bgBlack.green.bold + ' ';
localBl.successPrefix = ' Success: '.bgGreen.white.bold + ' ';
localBl.warnPrefix = ' '.bgYellow + ' Warn: '.bgBlack.yellow.bold + ' ';
/**
*
* @param logText
* @param logType
* @returns {boolean}
*/
beautylogNode.log = function (logText, logType) {
if (logText === void 0) { logText = 'empty log'; }
if (logType === void 0) { logType = 'normal'; }
try {
switch (logType) {
case 'dir':
logText = localBl.dirPrefix + clc.xterm(26)(logText);
break;
case 'error':
logText = localBl.errorPrefix + logText.red.bold;
break;
case 'info':
logText = localBl.infoPrefix + clc.xterm(198)(logText);
break;
case 'normal':
logText = localBl.normalPrefix + logText.cyan.bold;
break;
case 'ok':
logText = localBl.okPrefix + logText.bold;
break;
case 'success':
logText = localBl.successPrefix + logText.green.bold;
break;
case 'warn':
logText = localBl.warnPrefix + logText.bold;
break;
case 'log':
default:
logText.blue.bold;
console.log(('unknown logType for "' + logText + '"').red.bold);
break;
}
console.log(logText);
return true;
}
catch (error) {
console.log(localBl.errorPrefix + 'You seem to have tried logging something strange'.red.bold + error);
return false;
}
};
/** /**
* logs an directory to console * logs an directory to console
* @param logText * @param logText
@ -169,16 +117,116 @@ var BeautylogNode;
beautylogNode.warn = function (logText) { beautylogNode.warn = function (logText) {
return beautylogNode.log(logText, 'warn'); return beautylogNode.log(logText, 'warn');
}; };
beautylogNode.table = BeautylogOsTable.init(); beautylogNode.table = BeautylogNodeTable.init();
return beautylogNode; return beautylogNode;
} }
BeautylogNode.init = init; BeautylogNode.init = init;
})(BeautylogNode || (BeautylogNode = {})); })(BeautylogNode || (BeautylogNode = {}));
/// <reference path="./index.ts" /> /// <reference path="./index.ts" />
var BeautylogOsTable; var BeautylogNodeLog;
(function (BeautylogOsTable) { (function (BeautylogNodeLog) {
BeautylogNodeLog.init = function () {
var localBl = {
dirPrefix: plugins.clc.bgXterm(39).xterm(231).bold(' DIR ') + ' ',
errorPrefix: ' Error: '.bgRed.white.bold + ' ',
infoPrefix: plugins.clc.bgXterm(198).xterm(231).bold(' INFO ') + ' ',
normalPrefix: ' Log: '.bgCyan.white.bold + ' ',
okPrefix: ' '.bgGreen + ' OK! '.bgBlack.green.bold + ' ',
successPrefix: ' Success: '.bgGreen.white.bold + ' ',
warnPrefix: ' '.bgYellow + ' Warn: '.bgBlack.yellow.bold + ' '
};
/**
*
* @param logText
* @param logType
* @returns {boolean}
*/
var logFunction = function (logText, logType) {
if (logText === void 0) { logText = 'empty log'; }
if (logType === void 0) { logType = 'normal'; }
try {
switch (logType) {
case 'dir':
logText = localBl.dirPrefix + plugins.clc.xterm(26)(logText);
break;
case 'error':
logText = localBl.errorPrefix + logText.red.bold;
break;
case 'info':
logText = localBl.infoPrefix + plugins.clc.xterm(198)(logText);
break;
case 'normal':
logText = localBl.normalPrefix + logText.cyan.bold;
break;
case 'ok':
logText = localBl.okPrefix + logText.bold;
break;
case 'success':
logText = localBl.successPrefix + logText.green.bold;
break;
case 'warn':
logText = localBl.warnPrefix + logText.bold;
break;
case 'log':
default:
logText.blue.bold;
console.log(('unknown logType for "' + logText + '"').red.bold);
break;
}
console.log(logText);
return true;
}
catch (error) {
console.log(localBl.errorPrefix + 'You seem to have tried logging something strange'.red.bold + error);
return false;
}
};
return logFunction;
};
})(BeautylogNodeLog || (BeautylogNodeLog = {}));
/// <reference path="./index.ts" />
var BeautylogNodeCode;
(function (BeautylogNodeCode) {
BeautylogNodeCode.init = function () {
var consoleHighlight = function (code, language) {
var fs = require('fs'), path = require('path'), htmlout = require('html2console'), hljs = require('highlight.js');
var css = fs.readFileSync(path.join(__dirname, 'code.css'), 'utf8');
var result;
if (typeof language === "undefined") {
result = hljs.highlight(language, code);
}
else {
result = hljs.highlightAuto(code);
}
;
var html = result.value;
var output = htmlout.withCSS(css);
//console.log(html);
return output('<pre class="hljs">' + html + '</pre>');
};
var codeFunction = function (codeString, options) {
var codeSnippet = {
source: codeString,
highlighted: "default"
};
if (typeof codeString != "string") {
console.log("beautylog.code() expects a string as first argument!");
return;
}
;
if (typeof options != "undefined") {
codeSnippet.highlighted = consoleHighlight(codeSnippet.source, options.language);
console.log(codeSnippet.highlighted);
}
};
return codeFunction;
};
})(BeautylogNodeCode || (BeautylogNodeCode = {}));
/// <reference path="./index.ts" />
var BeautylogNodeTable;
(function (BeautylogNodeTable) {
function init() { function init() {
BeautylogOsTable.cliTable = require("cli-table2"); BeautylogNodeTable.cliTable = require("cli-table2");
var beautylogOsTable = {}; var beautylogOsTable = {};
beautylogOsTable.new = function (typeArg, tableHeadArrayArg) { beautylogOsTable.new = function (typeArg, tableHeadArrayArg) {
var newConsoleTable = new ConsoleTable(typeArg, tableHeadArrayArg); var newConsoleTable = new ConsoleTable(typeArg, tableHeadArrayArg);
@ -186,8 +234,8 @@ var BeautylogOsTable;
}; };
return beautylogOsTable; return beautylogOsTable;
} }
BeautylogOsTable.init = init; BeautylogNodeTable.init = init;
})(BeautylogOsTable || (BeautylogOsTable = {})); })(BeautylogNodeTable || (BeautylogNodeTable = {}));
/// <reference path="./index.ts" /> /// <reference path="./index.ts" />
var BeautylogBrowser; var BeautylogBrowser;
(function (BeautylogBrowser) { (function (BeautylogBrowser) {
@ -216,6 +264,8 @@ var BeautylogBrowser;
/// <reference path="./beautylog.plugins.ts" /> /// <reference path="./beautylog.plugins.ts" />
/// <reference path="./beautylog.classes.ts" /> /// <reference path="./beautylog.classes.ts" />
/// <reference path="./beautylog.node.ts" /> /// <reference path="./beautylog.node.ts" />
/// <reference path="./beautylog.node.log.ts" />
/// <reference path="./beautylog.node.code.ts" />
/// <reference path="./beautylog.node.table.ts" /> /// <reference path="./beautylog.node.table.ts" />
/// <reference path="./beautylog.browser.ts" /> /// <reference path="./beautylog.browser.ts" />
var plugins = BeautylogPlugins.init(); var plugins = BeautylogPlugins.init();

View File

@ -33,12 +33,14 @@
"cli-color": "^1.1.0", "cli-color": "^1.1.0",
"cli-table2": "^0.1.9", "cli-table2": "^0.1.9",
"colors": "1.1.2", "colors": "1.1.2",
"highlight.js": "^9.1.0",
"html2console": "0.0.4",
"smartenv": "0.0.15" "smartenv": "0.0.15"
}, },
"devDependencies": { "devDependencies": {
"easyserve": "0.0.4", "easyserve": "0.0.5",
"gulp": "^3.9.0", "gulp": "^3.9.0",
"gulp-browser": "0.0.18", "gulp-browser": "1.0.3",
"npmts": "^1.0.9" "npmts": "^1.0.12"
} }
} }

View File

@ -34,3 +34,7 @@ console.log("*** start table test ***");
testTable2.print(); testTable2.print();
})(); })();
console.log("*** end table test ***"); console.log("*** end table test ***");
console.log("*** start code test ***");
beautyLog.code("var test = 3; function(){}\n", {
language: "javascript"
});

View File

@ -31,7 +31,7 @@ class ConsoleTable {
this.rows.push(row); this.rows.push(row);
} }
print() { print() {
var table = new BeautylogOsTable.cliTable({ var table = new BeautylogNodeTable.cliTable({
head: this.tableHead head: this.tableHead
}); });
for (var row in this.rows){ for (var row in this.rows){

43
ts/beautylog.node.code.ts Normal file
View File

@ -0,0 +1,43 @@
/// <reference path="./index.ts" />
module BeautylogNodeCode {
export var init = function() {
var consoleHighlight = function(code:string,language:string) {
var fs = require('fs'),
path = require('path'),
htmlout = require('html2console'),
hljs = require('highlight.js');
var css = fs.readFileSync(path.join(__dirname, 'code.css'), 'utf8');
var result;
if (typeof language === "undefined"){
result = hljs.highlight(language, code)
} else {
result = hljs.highlightAuto(code);
};
var html = result.value;
var output = htmlout.withCSS(css);
//console.log(html);
return output('<pre class="hljs">' + html + '</pre>');
};
var codeFunction = function(codeString,options?){
var codeSnippet = {
source:codeString,
highlighted:"default"
};
if (typeof codeString != "string"){
console.log("beautylog.code() expects a string as first argument!");
return;
};
if (typeof options != "undefined"){
codeSnippet.highlighted = consoleHighlight(codeSnippet.source,options.language);
console.log(codeSnippet.highlighted)
}
};
return codeFunction;
}
}

61
ts/beautylog.node.log.ts Normal file
View File

@ -0,0 +1,61 @@
/// <reference path="./index.ts" />
module BeautylogNodeLog {
export var init = function(){
var localBl = {
dirPrefix: plugins.clc.bgXterm(39).xterm(231).bold(' DIR ') + ' ',
errorPrefix: ' Error: '.bgRed.white.bold + ' ',
infoPrefix: plugins.clc.bgXterm(198).xterm(231).bold(' INFO ') + ' ',
normalPrefix: ' Log: '.bgCyan.white.bold + ' ',
okPrefix: ' '.bgGreen + ' OK! '.bgBlack.green.bold + ' ',
successPrefix: ' Success: '.bgGreen.white.bold + ' ',
warnPrefix: ' '.bgYellow + ' Warn: '.bgBlack.yellow.bold + ' '
};
/**
*
* @param logText
* @param logType
* @returns {boolean}
*/
var logFunction = function(logText:string = 'empty log', logType:string = 'normal') {
try {
switch (logType) {
case 'dir':
logText = localBl.dirPrefix + plugins.clc.xterm(26)(logText);
break;
case 'error':
logText = localBl.errorPrefix + logText.red.bold;
break;
case 'info':
logText = localBl.infoPrefix + plugins.clc.xterm(198)(logText);
break;
case 'normal':
logText = localBl.normalPrefix + logText.cyan.bold;
break;
case 'ok':
logText = localBl.okPrefix + logText.bold;
break;
case 'success':
logText = localBl.successPrefix + logText.green.bold;
break;
case 'warn':
logText = localBl.warnPrefix + logText.bold;
break;
case 'log':
default:
logText.blue.bold;
console.log(('unknown logType for "' + logText + '"').red.bold);
break;
}
console.log(logText);
return true;
}
catch (error) {
console.log(localBl.errorPrefix + 'You seem to have tried logging something strange'.red.bold + error);
return false;
}
};
return logFunction;
}
}

View File

@ -1,5 +1,5 @@
/// <reference path="./index.ts" /> /// <reference path="./index.ts" />
module BeautylogOsTable { module BeautylogNodeTable {
export var cliTable; export var cliTable;
export function init() { export function init() {
cliTable = require("cli-table2"); cliTable = require("cli-table2");

View File

@ -1,67 +1,13 @@
/// <reference path="./index.ts" /> /// <reference path="./index.ts" />
module BeautylogNode { module BeautylogNode {
export function init() { export function init() {
var colors = require("colors"); plugins.colors = require("colors");
var clc = require("cli-color"); plugins.clc = require("cli-color");
var beautylogNode:any = {}; //object to append to all public facing functions
var localBl:any; // object to append to all private params and functions
localBl = {};
localBl.dirPrefix = clc.bgXterm(39).xterm(231).bold(' DIR ') + ' ';
localBl.errorPrefix = ' Error: '.bgRed.white.bold + ' ';
localBl.infoPrefix = clc.bgXterm(198).xterm(231).bold(' INFO ') + ' ';
localBl.normalPrefix = ' Log: '.bgCyan.white.bold + ' ';
localBl.okPrefix = ' '.bgGreen + ' OK! '.bgBlack.green.bold + ' ';
localBl.successPrefix = ' Success: '.bgGreen.white.bold + ' ';
localBl.warnPrefix = ' '.bgYellow + ' Warn: '.bgBlack.yellow.bold + ' ';
/**
*
* @param logText
* @param logType
* @returns {boolean}
*/
beautylogNode.log = (logText:string = 'empty log', logType:string = 'normal') => {
try {
switch (logType) {
case 'dir':
logText = localBl.dirPrefix + clc.xterm(26)(logText);
break;
case 'error':
logText = localBl.errorPrefix + logText.red.bold;
break;
case 'info':
logText = localBl.infoPrefix + clc.xterm(198)(logText);
break;
case 'normal':
logText = localBl.normalPrefix + logText.cyan.bold;
break;
case 'ok':
logText = localBl.okPrefix + logText.bold;
break;
case 'success':
logText = localBl.successPrefix + logText.green.bold;
break;
case 'warn':
logText = localBl.warnPrefix + logText.bold;
break;
case 'log':
default:
logText.blue.bold;
console.log(('unknown logType for "' + logText + '"').red.bold);
break;
}
console.log(logText);
return true;
}
catch (error) {
console.log(localBl.errorPrefix + 'You seem to have tried logging something strange'.red.bold + error);
return false;
}
};
var beautylogNode:any = {
log:BeautylogNodeLog.init(),
code:BeautylogNodeCode.init()
}; //object to append to all public facing functions
/** /**
* logs an directory to console * logs an directory to console
@ -118,7 +64,7 @@ module BeautylogNode {
return beautylogNode.log(logText, 'warn'); return beautylogNode.log(logText, 'warn');
}; };
beautylogNode.table = BeautylogOsTable.init(); beautylogNode.table = BeautylogNodeTable.init();
return beautylogNode; return beautylogNode;
} }

View File

@ -1,7 +1,7 @@
/// <reference path="./index.ts" /> /// <reference path="./index.ts" />
module BeautylogPlugins { module BeautylogPlugins {
export var init = function(){ export var init = function(){
var plugins = { var plugins:any = {
smartenv: require("smartenv") smartenv: require("smartenv")
}; };
return plugins; return plugins;

View File

@ -2,6 +2,8 @@
/// <reference path="./beautylog.plugins.ts" /> /// <reference path="./beautylog.plugins.ts" />
/// <reference path="./beautylog.classes.ts" /> /// <reference path="./beautylog.classes.ts" />
/// <reference path="./beautylog.node.ts" /> /// <reference path="./beautylog.node.ts" />
/// <reference path="./beautylog.node.log.ts" />
/// <reference path="./beautylog.node.code.ts" />
/// <reference path="./beautylog.node.table.ts" /> /// <reference path="./beautylog.node.table.ts" />
/// <reference path="./beautylog.browser.ts" /> /// <reference path="./beautylog.browser.ts" />

View File

@ -43,4 +43,12 @@ console.log("*** start table test ***");
console.log("*** end table test ***"); console.log("*** end table test ***");
console.log("*** start code test ***");
beautyLog.code(
"var test = 3; function(){}\n",
{
language:"javascript"
}
);