UNPKG

1.96 kBJavaScriptView Raw
1let logger = require('pretty-web-logger');
2
3function showColorMessage(msg, bgColor = 'white', ...params) {
4 let mgStyle = `background: ${bgColor}; color: white; display: block;`;
5 if (bgColor === 'yellow') { // different `color` as white / yellow dont look good
6 mgStyle = `background: ${bgColor}; color: black; display: block;`;
7 }
8 params.length > 0 ?
9 console.log('%c%s', mgStyle, msg, params) :
10 console.log('%c%s', mgStyle, msg);
11}
12
13class MessengerBrowser {
14 constructor(pkgInfo = {}) {
15 this.options = {
16 logger: false
17 };
18 this.pkgInfo = pkgInfo;
19 }
20 setOptions(opts = {}) {
21 this.options = Object.assign(this.options, opts);
22 }
23 version() {
24 return this.pkgInfo.version;
25 }
26 name() {
27 return this.pkgInfo.name;
28 }
29 log(msg, ...params) {
30 this.options.logger ? logger.log(msg, ...params) : console.log(msg, ...params);
31 console.log(msg, ...params);
32 }
33 info(msg, ...params) {
34 this.options.logger ? logger.info(msg, ...params) : showColorMessage(msg, 'blue', ...params);
35 }
36 note(msg, ...params) {
37 this.options.logger ? logger.info(msg, ...params) : showColorMessage(msg, 'orange', ...params);
38 }
39 success(msg, ...params) {
40 this.options.logger ? logger.info(msg, ...params) : showColorMessage(msg, 'green', ...params);
41 }
42 error(msg, ...params) {
43 this.options.logger ? logger.error(msg, ...params) : showColorMessage(msg, 'red', ...params);
44 }
45 warning(msg, ...params) {
46 this.options.logger ? logger.warning(msg, ...params) : showColorMessage(msg, 'yellow', ...params);
47 }
48 table(data) {
49 console.table(data);
50 }
51 dir(...params) {
52 console.dir(...params);
53 }
54 line(char = '', fgColor = 'white', width = 80) {
55 char = (char.length > 0) ? char.substring(0, 1) : '\u2584'; // '\u2584' <-- bigger box
56 console.log('%c%s', `color: ${fgColor}; display: block`, char.repeat(width));
57 }
58}
59// export default MessengerBrowser;
60module.exports = MessengerBrowser;