Skip to content

Commit

Permalink
Added BCLIP!
Browse files Browse the repository at this point in the history
  • Loading branch information
racerxdl committed May 25, 2018
1 parent 89246f4 commit f523814
Show file tree
Hide file tree
Showing 10 changed files with 518 additions and 14 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
____ _ ___ _
/ ___|___ _ __ | |_ __ _ / _ \ _ _ __ _ _ __ | |_ ___
| | / _ \| '_ \| __/ _` | | | | | | |/ _` | '_ \| __/ _ \
____ _ ___ _
/ ___|___ _ __ | |_ __ _ / _ \ _ _ __ _ _ __ | |_ ___
| | / _ \| '_ \| __/ _` | | | | | | |/ _` | '_ \| __/ _ \
| |__| (_) | | | | || (_| | |_| | |_| | (_| | | | | || (_) |
\____\___/|_| |_|\__\__,_|\__\_\\__,_|\__,_|_| |_|\__\___/
\____\___/|_| |_|\__\__,_|\__\_\\__,_|\__,_|_| |_|\__\___/


Welcome to the Quanto Commons repository!
These are tools / constants / models that are shared among
Expand All @@ -13,7 +13,7 @@ our projects. Feel free to contribute / make it better.
## QuantoColors

`QuantoColors` uses the `node-colors` package and makes a standard color
display over our applications. To use it just import `QuantoColors` from
display over our applications. To use it just import `QuantoColors` from
`quanto-commons` and run it:

```javascript
Expand Down Expand Up @@ -64,7 +64,7 @@ The second parameter is optional and denotes the second line.

## ErrorObject / ErrorCodes

The `ErrorObject` model is used across our applications to denote an
The `ErrorObject` model is used across our applications to denote an
error when returning or throwing an exception. It has five fields:

* `errorCode` => A string from `ErrorCodes`
Expand All @@ -73,5 +73,21 @@ error when returning or throwing an exception. It has five fields:
* `message` => A brief message saying why the error ocurred
* `errorDat` => An optional object that can contain extra data related to the error

The `ErrorCodes` is a type of enum that contains a map from a standard errorCode string to
The `ErrorCodes` is a type of enum that contains a map from a standard errorCode string to
a good name to be used on `ErrorObject` (or other places).


## Fatal "Clip" Exception Message

For the most waited feature of quanto-commons, here it is. Your master helper for fatal exceptions:

![Rainbow String](pics/fatal-exception.png)

Just use:
```javascript
import { boxMessage, bclipError } from 'quanto-commons';
const myFatalHelperMessage = boxMessage(bclipError(new Error('Fatal Exception Test')));
console.log(myFatalHelperMessage);
```

Have fun!
22 changes: 18 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

223 changes: 223 additions & 0 deletions dist/qlog/bclip.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion dist/qlog/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quanto-commons",
"version": "1.0.24",
"version": "1.0.25",
"description": "Quanto Common Tools",
"main": "dist/index.js",
"scripts": {
Expand Down
Binary file added pics/fatal-exception.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

export * from './tools';
export * from './models';
export * from './qlog/bclip';
export QLog from './qlog';
229 changes: 229 additions & 0 deletions src/qlog/bclip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/* eslint-disable no-plusplus */
/**
* Created by Lucas Teske on 25/05/18.
* @flow
*/

import {
undefinedOrNull,
} from '../tools';

import {
stripColors,
} from '../colors/tools';

const FullSizeLUT = {
// region Numbers
0: '0',
1: '1',
2: '2',
3: '3',
4: '4',
5: '5',
6: '6',
7: '7',
8: '8',
9: '9',
// endregion
// region Upper Case
A: 'A',
B: 'B',
C: 'C',
D: 'D',
E: 'E',
F: 'F',
G: 'G',
H: 'H',
I: 'I',
J: 'J',
K: 'K',
L: 'L',
M: 'M',
N: 'N',
O: 'O',
P: 'P',
Q: 'Q',
R: 'R',
S: 'S',
T: 'T',
U: 'U',
V: 'V',
W: 'W',
X: 'X',
Y: 'Y',
Z: 'Z',
// endregion
// region Lower Case
a: 'a',
b: 'b',
c: 'c',
d: 'd',
e: 'e',
f: 'f',
g: 'g',
h: 'h',
i: 'i',
j: 'j',
k: 'k',
l: 'l',
m: 'm',
n: 'n',
o: 'o',
p: 'p',
q: 'q',
r: 'r',
s: 's',
t: 't',
u: 'u',
v: 'v',
w: 'w',
x: 'x',
y: 'y',
z: 'z',
// endregion
// region Meta
',': ',',
'.': '.',
':': ':',
';': ';',
'!': '!',
'?': '?',
'"': '"',
'\'': ''',
'`': '`',
'^': '^',
'~': '~',
_: '_',
'&': '&',
'@': '@',
'#': '#',
'%': '%',
'+': '+',
'-': '-',
'*': '*',
'=': '=',
'<': '<',
'>': '>',
'(': '(',
')': ')',
'[': '[',
']': ']',
'{': '{',
'}': '}',
'|': '|',
'/': '/',
'\\': '\',
$: '$',
// endregion
};

const BClipLut = [
' ',
' ▄████▄ ',
' ▐▌ ▐▌ ',
' ▄▀▀█▀ ▐▌ ',
' ▄ ▐▄ ▐▌▀▀▄ ',
' ▐▀ ▄▄ ▀▌ ▄▀▀ ▀▄ ▀ ',
' ▐ ▀██▀ ▌▐ ▄██▄ ▌ ',
' ▀▄ ▄▄▀ ▐ ▀▀ ▌ ',
' █ ▀▄▄ ▄▀ ',
' █ █ █ ▐ ',
' █ █ ▐▌ █ ',
' █ █ ▐▌ █ ',
' ▐▌▐▌ █ █ ',
' ▐▌ █▄ ▐▌ █ ',
' █ ▀▀▀ ▐▌ ',
' ▐▌ █ ',
' █▄ ▄█ ',
' ▀████▀ ',
' ',
];

const BClipEmpty = ' ';

export const toFullSize = msg => msg
.split('')
.map(c => (!undefinedOrNull(FullSizeLUT[c]) ? FullSizeLUT[c] : c))
.join('');

export const boxMessage = (msg) => {
const lines = msg.split('\n');
const outLines = [];
const maxWidth = lines
.map(l => stripColors(l).length)
.reduce((a, b) => (a > b ? a : b));

outLines.push(` ${'╔'.white}${'═'.repeat(maxWidth + 2).white}${'╗'.white}`);

lines
.map(l => ` ${'║'.white} ${l}${' '.repeat(maxWidth - stripColors(l).length)} ${'║'.white}`)
.forEach((l) => { outLines.push(l); });

outLines.push(` ${'╚'.white}${'═'.repeat(maxWidth + 2).white}${'╝'.white}`);

return outLines.join('\n');
};

export const bclipMessage = (title, msg) => {
const lines = msg.split('\n');
const titleFull = title; // toFullSize(title); :(( broken on box
const outLines = [];
let curClipLine = 0;

const maxWidth = lines
.map(l => (stripColors(l).length + ' '.length))
.reduce((a, b) => (a > b ? a : b));

const titlePadding = (maxWidth - stripColors(titleFull).length) / 2;

outLines.push(`${BClipLut[curClipLine++]}`);
outLines.push(`${BClipLut[curClipLine++]}${' '.repeat(titlePadding)}${titleFull}${' '.repeat(titlePadding)}`);
outLines.push(`${BClipLut[curClipLine++]}`);
outLines.push(`${BClipLut[curClipLine++]}`);

lines.forEach((l) => {
const bclipLine = curClipLine > (BClipLut.length - 1) ? BClipEmpty : BClipLut[curClipLine];
outLines.push(`${bclipLine} ${l}`);
curClipLine += 1;
});

for (let i = curClipLine; i < BClipLut.length; i++) {
outLines.push(BClipLut[i]);
}

return outLines.join('\n');
};

export const bclipError = (excpt: Error) => {
const [name, ...rest] = excpt.stack.split('\n');
const title = name.red.bold;
const lines = rest;


if (lines[lines.length - 1] === '\u001b[39m') {
lines[lines.length - 2] += '\u001b[39m';
lines.splice(lines.length - 1, 1);
}

const outLines = [];
outLines.push('Looks like your app is crash.'.warn);
outLines.push('Do you need any help?'.warn.bold);
outLines.push('');

for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (stripColors(line).length > 80) {
let curLine = line;
outLines.push(`${curLine.substr(0, 80)} `);
curLine = curLine.substr(80);
while (curLine.length !== 0) {
outLines.push(` ${curLine.substr(0, 80)} `);
curLine = curLine.substr(80);
}
} else {
outLines.push(line);
}
}

return bclipMessage(title, outLines.join('\n'));
};
4 changes: 4 additions & 0 deletions src/qlog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class QLog {
}
}

bclip(...args) {

}

slash() {
this.__normalLog__('─────────────────────────────────────────────────────────────────────────────────────────────');
}
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
ErrorObject,
QLog,
ErrorCodes,
toFullSize,
bclipMessage,
boxMessage,
bclipError,
} from './dist';

import './dist/colors';
Expand Down Expand Up @@ -100,3 +104,13 @@ huebr
hurbaba
${'abc'.warn.bold.bgBlue}
`.warn);

const bclipm = bclipMessage('Hmm, something doesn\'t look right' , 'Looks like your app is crash.\nDo you need any help?\n');

const bmx = boxMessage(bclipm);

QLog.fatal(bmx);

QLog.fatal(boxMessage(bclipError(new Error('Fatal Exception Test'))));

console.log(toFullSize('hueheu brbr??'));

0 comments on commit f523814

Please sign in to comment.