UNPKG

3.75 kBJavaScriptView Raw
1var $, M, _, kleur, m;
2
3$ = {};
4
5$.root = require('../dist/root');
6
7$.home = require('../dist/home');
8
9$.parseString = require('../dist/parseString');
10
11$.i = require('../dist/i');
12
13_ = {};
14
15_.trim = require('lodash/trim');
16
17_.padStart = require('lodash/padStart');
18
19_.floor = require('lodash/floor');
20
21_.now = require('lodash/now');
22
23_.repeat = require('lodash/repeat');
24
25kleur = require('kleur');
26
27M = (function() {
28 class M {
29 execute(...arg) {
30 var msg, text, type;
31 if (!arg.length) {
32 return this;
33 }
34 [type, text] = (function() {
35 switch (arg.length) {
36 case 1:
37 return ['default', arg[0]];
38 case 2:
39 return arg;
40 default:
41 throw new Error('info/error: invalid argument length');
42 }
43 })();
44 if (this.$isSilent) {
45 return text;
46 }
47 msg = _.trim($.parseString(text));
48 if (!msg.length) {
49 return text;
50 }
51 $.i(this.render(type, msg));
52 return text; // return
53 }
54
55 getStringTime() {
56 var date, item, listTime;
57 date = new Date();
58 listTime = [date.getHours(), date.getMinutes(), date.getSeconds()];
59 return ((function() {
60 var i, len, results;
61 results = [];
62 for (i = 0, len = listTime.length; i < len; i++) {
63 item = listTime[i];
64 // return
65 results.push(_.padStart(item, 2, 0));
66 }
67 return results;
68 })()).join(':');
69 }
70
71 render(type, string) {
72 return [this.renderTime(), this['$separator'], this.renderType(type), this.renderContent(string)].join('');
73 }
74
75 renderContent(string) {
76 var msg;
77 // 'xxx'
78 msg = this.renderPath(string).replace(/'.*?'/g, function(text) {
79 var cont;
80 cont = text.replace(/'/g, '');
81 if (!cont.length) {
82 return "''";
83 }
84 return kleur.magenta(cont);
85 });
86 return msg; // return
87 }
88
89 renderPath(string) {
90 return string.replace(this['$reg-root'], '.').replace(this['$reg-home'], '~');
91 }
92
93 renderTime() {
94 var cache, stringTime, ts;
95 cache = this['$cache-time'];
96 ts = _.floor(_.now(), -3);
97 if (ts === cache[0]) {
98 return cache[1];
99 }
100 cache[0] = ts;
101 stringTime = kleur.gray(`[${this.getStringTime()}]`);
102 // return
103 return cache[1] = `${stringTime} `;
104 }
105
106 renderType(type) {
107 var base;
108 type = _.trim($.parseString(type)).toLowerCase();
109 return (base = this['$cache-type'])[type] || (base[type] = (function() {
110 var stringContent, stringPad;
111 if (type === 'default') {
112 return '';
113 }
114 stringContent = kleur.cyan().underline(type);
115 stringPad = _.repeat(' ', 10 - type.length);
116 return `${stringContent}${stringPad
117 // return
118} `;
119 })());
120 }
121
122 async silence_(fn_) {
123 var result;
124 this.$isSilent = true;
125 result = (await (typeof fn_ === "function" ? fn_() : void 0));
126 this.$isSilent = false;
127 return result; // return
128 }
129
130 };
131
132 /*
133 $cache-time
134 $cache-type
135 $isSilent
136 $reg-home
137 $reg-root
138 $separator
139 ---
140 execute(arg...)
141 getStringTime()
142 render(type, string)
143 renderContent(string)
144 renderPath(string)
145 renderTime()
146 renderType(type)
147 silence_(fn_)
148 */
149 M.prototype['$cache-time'] = [];
150
151 M.prototype['$cache-type'] = {};
152
153 M.prototype['$isSilent'] = false;
154
155 M.prototype['$reg-root'] = new RegExp(`^${$.root()}`, 'g');
156
157 M.prototype['$reg-home'] = new RegExp(`^${$.home()}`, 'g');
158
159 M.prototype['$separator'] = `${kleur.gray('›')} `;
160
161 return M;
162
163}).call(this);
164
165
166// return
167m = new M();
168
169module.exports = function(...arg) {
170 return m.execute(...arg);
171};