From ea06f32ce0ca2fe68279c6ad4a01e6f0ad589ec8 Mon Sep 17 00:00:00 2001 From: PhilKunz Date: Sat, 30 Jan 2016 06:19:44 +0100 Subject: [PATCH] add code function --- README.md | 7 +- code.css | 120 +++++++++++++++++++++++++ index.d.ts | 16 ++-- index.js | 180 +++++++++++++++++++++++-------------- package.json | 8 +- test.js | 4 + ts/beautylog.classes.ts | 2 +- ts/beautylog.node.code.ts | 43 +++++++++ ts/beautylog.node.log.ts | 61 +++++++++++++ ts/beautylog.node.table.ts | 2 +- ts/beautylog.node.ts | 68 ++------------ ts/beautylog.plugins.ts | 2 +- ts/index.ts | 2 + ts/test.ts | 10 ++- 14 files changed, 384 insertions(+), 141 deletions(-) create mode 100644 code.css create mode 100644 ts/beautylog.node.code.ts create mode 100644 ts/beautylog.node.log.ts diff --git a/README.md b/README.md index 3044837..0b28de1 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ bl.log('some error message','error'); The plugin produces beautiful output like this: ![console.png](https://mediaserve.lossless.digital/github.com/pushrocks/beautylog/console.png) +### Code Highlighting + + + ### Console Tables 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: -![table.png](https://mediaserve.lossless.digital/github.com/pushrocks/beautylog/table.png) \ No newline at end of file +![table.png](https://mediaserve.lossless.digital/github.com/pushrocks/beautylog/table.png) + diff --git a/code.css b/code.css new file mode 100644 index 0000000..b4c305f --- /dev/null +++ b/code.css @@ -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; +} \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 34beba1..acc5e01 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,8 +1,6 @@ /// declare module BeautylogPlugins { - var init: () => { - smartenv: any; - }; + var init: () => any; } declare var tableHelpers: { makeRow: (cellCounterArg?: number, colorArg?: string) => any[]; @@ -18,14 +16,18 @@ declare class ConsoleTable { declare module BeautylogNode { 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; function init(): any; } declare module BeautylogBrowser { function init(): any; } -declare var plugins: { - smartenv: any; -}; +declare var plugins: any; declare var beautylog: any; diff --git a/index.js b/index.js index 211a127..4e6ea42 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,7 @@ var ConsoleTable = (function () { this.rows.push(row); }; ConsoleTable.prototype.print = function () { - var table = new BeautylogOsTable.cliTable({ + var table = new BeautylogNodeTable.cliTable({ head: this.tableHead }); for (var row in this.rows) { @@ -63,64 +63,12 @@ var ConsoleTable = (function () { var BeautylogNode; (function (BeautylogNode) { function init() { - var colors = require("colors"); - var clc = require("cli-color"); - var beautylogNode = {}; //object to append to all public facing functions - var localBl; // 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 = 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; - } - }; + plugins.colors = require("colors"); + plugins.clc = require("cli-color"); + var beautylogNode = { + log: BeautylogNodeLog.init(), + code: BeautylogNodeCode.init() + }; //object to append to all public facing functions /** * logs an directory to console * @param logText @@ -169,16 +117,116 @@ var BeautylogNode; beautylogNode.warn = function (logText) { return beautylogNode.log(logText, 'warn'); }; - beautylogNode.table = BeautylogOsTable.init(); + beautylogNode.table = BeautylogNodeTable.init(); return beautylogNode; } BeautylogNode.init = init; })(BeautylogNode || (BeautylogNode = {})); /// -var BeautylogOsTable; -(function (BeautylogOsTable) { +var BeautylogNodeLog; +(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 = {})); +/// +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('
' + html + '
'); + }; + 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 = {})); +/// +var BeautylogNodeTable; +(function (BeautylogNodeTable) { function init() { - BeautylogOsTable.cliTable = require("cli-table2"); + BeautylogNodeTable.cliTable = require("cli-table2"); var beautylogOsTable = {}; beautylogOsTable.new = function (typeArg, tableHeadArrayArg) { var newConsoleTable = new ConsoleTable(typeArg, tableHeadArrayArg); @@ -186,8 +234,8 @@ var BeautylogOsTable; }; return beautylogOsTable; } - BeautylogOsTable.init = init; -})(BeautylogOsTable || (BeautylogOsTable = {})); + BeautylogNodeTable.init = init; +})(BeautylogNodeTable || (BeautylogNodeTable = {})); /// var BeautylogBrowser; (function (BeautylogBrowser) { @@ -216,6 +264,8 @@ var BeautylogBrowser; /// /// /// +/// +/// /// /// var plugins = BeautylogPlugins.init(); diff --git a/package.json b/package.json index c147951..72c5c0f 100644 --- a/package.json +++ b/package.json @@ -33,12 +33,14 @@ "cli-color": "^1.1.0", "cli-table2": "^0.1.9", "colors": "1.1.2", + "highlight.js": "^9.1.0", + "html2console": "0.0.4", "smartenv": "0.0.15" }, "devDependencies": { - "easyserve": "0.0.4", + "easyserve": "0.0.5", "gulp": "^3.9.0", - "gulp-browser": "0.0.18", - "npmts": "^1.0.9" + "gulp-browser": "1.0.3", + "npmts": "^1.0.12" } } diff --git a/test.js b/test.js index 763b9e8..d761be5 100644 --- a/test.js +++ b/test.js @@ -34,3 +34,7 @@ console.log("*** start table test ***"); testTable2.print(); })(); console.log("*** end table test ***"); +console.log("*** start code test ***"); +beautyLog.code("var test = 3; function(){}\n", { + language: "javascript" +}); diff --git a/ts/beautylog.classes.ts b/ts/beautylog.classes.ts index 98a2d8c..86cc71c 100644 --- a/ts/beautylog.classes.ts +++ b/ts/beautylog.classes.ts @@ -31,7 +31,7 @@ class ConsoleTable { this.rows.push(row); } print() { - var table = new BeautylogOsTable.cliTable({ + var table = new BeautylogNodeTable.cliTable({ head: this.tableHead }); for (var row in this.rows){ diff --git a/ts/beautylog.node.code.ts b/ts/beautylog.node.code.ts new file mode 100644 index 0000000..1389324 --- /dev/null +++ b/ts/beautylog.node.code.ts @@ -0,0 +1,43 @@ +/// +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('
' + html + '
'); + }; + + + 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; + } +} \ No newline at end of file diff --git a/ts/beautylog.node.log.ts b/ts/beautylog.node.log.ts new file mode 100644 index 0000000..a11aa9b --- /dev/null +++ b/ts/beautylog.node.log.ts @@ -0,0 +1,61 @@ +/// +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; + } +} \ No newline at end of file diff --git a/ts/beautylog.node.table.ts b/ts/beautylog.node.table.ts index e4a2e7b..6299174 100644 --- a/ts/beautylog.node.table.ts +++ b/ts/beautylog.node.table.ts @@ -1,5 +1,5 @@ /// -module BeautylogOsTable { +module BeautylogNodeTable { export var cliTable; export function init() { cliTable = require("cli-table2"); diff --git a/ts/beautylog.node.ts b/ts/beautylog.node.ts index 06b12b9..147abde 100644 --- a/ts/beautylog.node.ts +++ b/ts/beautylog.node.ts @@ -1,67 +1,13 @@ /// module BeautylogNode { export function init() { - var colors = require("colors"); - var 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; - } - }; - + plugins.colors = require("colors"); + plugins.clc = require("cli-color"); + var beautylogNode:any = { + log:BeautylogNodeLog.init(), + code:BeautylogNodeCode.init() + }; //object to append to all public facing functions /** * logs an directory to console @@ -118,7 +64,7 @@ module BeautylogNode { return beautylogNode.log(logText, 'warn'); }; - beautylogNode.table = BeautylogOsTable.init(); + beautylogNode.table = BeautylogNodeTable.init(); return beautylogNode; } diff --git a/ts/beautylog.plugins.ts b/ts/beautylog.plugins.ts index c1f50bc..b631d36 100644 --- a/ts/beautylog.plugins.ts +++ b/ts/beautylog.plugins.ts @@ -1,7 +1,7 @@ /// module BeautylogPlugins { export var init = function(){ - var plugins = { + var plugins:any = { smartenv: require("smartenv") }; return plugins; diff --git a/ts/index.ts b/ts/index.ts index fd43f6b..4fe34aa 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -2,6 +2,8 @@ /// /// /// +/// +/// /// /// diff --git a/ts/test.ts b/ts/test.ts index dba9d9c..be75e5c 100644 --- a/ts/test.ts +++ b/ts/test.ts @@ -43,4 +43,12 @@ console.log("*** start table test ***"); -console.log("*** end table test ***"); \ No newline at end of file +console.log("*** end table test ***"); + +console.log("*** start code test ***"); +beautyLog.code( + "var test = 3; function(){}\n", + { + language:"javascript" + } +); \ No newline at end of file