Skip to content

Commit 1dde15a

Browse files
committed
setting
1 parent d08e15c commit 1dde15a

File tree

4 files changed

+157
-34
lines changed

4 files changed

+157
-34
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,27 @@ const logger = new Logger();
1919
// Both font and background color including:
2020
// 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
2121

22+
// Optional parameter
23+
const setting = {
24+
bold: true,
25+
dim: false,
26+
underscore: true,
27+
reverse: true,
28+
}
29+
30+
let message = 'anything you will put into console.log';
31+
2232
// normal
2333
logger.log(message)
2434
// set font color
25-
logger.fontColor('red', message);
35+
logger.fontColor('red', message, setting);
2636
// set background color
27-
logger.bgColor('yellow', message);
37+
logger.bgColor('yellow', message, setting);
2838
// set font and background color
2939
logger.setColor({
3040
font: 'black',
3141
bg: 'yellow'
32-
}, message);
42+
}, message, setting);
3343

3444
// Degug level, with prefix "DEBUG: "
3545
logger.degug(message);

change.log.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0.0.3
2+
- Add the third parameter `setting` for `fontColor`, `bgColor`, `setColor`
3+
4+
0.0.2
5+
- Implement `fontColor`, `bgColor`, `setColor` and other functions

index.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const CONFIG = {
22
SYSTEM: {
33
reset: "\x1b[0m",
4-
bright: "\x1b[1m",
4+
bold: "\x1b[1m",
55
dim: "\x1b[2m",
66
underscore: "\x1b[4m",
77
blink: "\x1b[5m",
@@ -36,37 +36,54 @@ class Logger {
3636

3737
}
3838

39-
fontColor(ticket, text) {
39+
fontColor(ticket, text, setting) {
40+
let command = '';
41+
if (setting) {
42+
command += this.checkSetting(setting);
43+
}
4044
if (ticket in CONFIG.FONT) {
41-
console.log(`${CONFIG.FONT[ticket]}%s${CONFIG.SYSTEM.reset}`, text);
45+
command += CONFIG.FONT[ticket];
4246
} else {
43-
console.log(text);
4447
this.warn("Font color not found! Use the default.")
4548
}
49+
command += '%s';
50+
command += CONFIG.SYSTEM.reset;
51+
console.log(command, text);
4652
}
4753

48-
bgColor(ticket, text) {
54+
bgColor(ticket, text, setting) {
55+
let command = '';
56+
if (setting) {
57+
command += this.checkSetting(setting);
58+
}
4959
if (ticket in CONFIG.BACKGROUND) {
50-
console.log(`${CONFIG.BACKGROUND[ticket]}%s${CONFIG.SYSTEM.reset}`, text);
60+
command += CONFIG.BACKGROUND[ticket];
5161
} else {
52-
console.log(text);
5362
this.warn("Background color not found! Use the default.")
5463
}
64+
command += '%s';
65+
command += CONFIG.SYSTEM.reset;
66+
console.log(command, text);
5567
}
5668

57-
setColor(ticketObj, text) {
58-
let colorSetting = '';
69+
setColor(ticketObj, text, setting) {
70+
let command = '';
71+
if (setting) {
72+
command += this.checkSetting(setting);
73+
}
5974
if (ticketObj.font in CONFIG.FONT) {
60-
colorSetting += CONFIG.FONT[ticketObj.font];
75+
command += CONFIG.FONT[ticketObj.font];
6176
} else {
6277
this.warn("Font color not found! Use the default.")
6378
}
6479
if (ticketObj.bg in CONFIG.BACKGROUND) {
65-
colorSetting += CONFIG.BACKGROUND[ticketObj.bg]
80+
command += CONFIG.BACKGROUND[ticketObj.bg]
6681
} else {
6782
this.warn("Background color not found! Use the default.")
6883
}
69-
console.log(`${colorSetting}%s${CONFIG.SYSTEM.reset}`, text);
84+
command += '%s';
85+
command += CONFIG.SYSTEM.reset;
86+
console.log(command, text);
7087
}
7188

7289
log(text) {
@@ -92,6 +109,23 @@ class Logger {
92109
degug(text) {
93110
this.fontColor('cyan', `DEBUG: ${text}`);
94111
}
112+
113+
checkSetting(setting) {
114+
const validSetting = ['bold', 'dim', 'underscore', 'reverse'];
115+
let command = '';
116+
for (const item in setting) {
117+
if (validSetting.indexOf(item) !== -1) {
118+
if (setting[item] === true) {
119+
command += CONFIG.SYSTEM[item];
120+
} else if (setting[item] !== false) {
121+
this.warn(`The value ${item} should be boolean.`)
122+
}
123+
} else {
124+
this.warn(`${item} is not valid in setting.`)
125+
}
126+
}
127+
return command;
128+
}
95129
}
96130

97131
module.exports = Logger;

test.js

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const Logger = require('./index');
22
const logger = new Logger();
33

4+
logger.log('************************');
5+
logger.log('*** Test Font Color ****');
6+
logger.log('************************');
47
logger.fontColor('red', 'Font in red.');
58
logger.fontColor('black', 'Font in black.');
69
logger.fontColor('green', 'Font in green.');
@@ -9,7 +12,27 @@ logger.fontColor('blue', 'Font in blue.');
912
logger.fontColor('magenta', 'Font in magenta.');
1013
logger.fontColor('cyan', 'Font in cyan.');
1114
logger.fontColor('white', 'Font in white.');
12-
logger.log('-------');
15+
logger.fontColor('blue', 'Bold, blue', {
16+
bold: true,
17+
underscore: false,
18+
});
19+
logger.fontColor('blue', 'Bold, dim, underscore, reverse', {
20+
bold: true,
21+
dim: true,
22+
underscore: true,
23+
reverse: true,
24+
});
25+
logger.fontColor('blue', 'Bold, underscore, blue.', {
26+
bold: true,
27+
dim: false,
28+
underscore: true,
29+
reverse: false,
30+
});
31+
logger.log('\n');
32+
33+
logger.log('******************************');
34+
logger.log('*** Test Background Color ****');
35+
logger.log('******************************');
1336
logger.bgColor('red', 'Background in red.');
1437
logger.bgColor('black', 'Background in black.');
1538
logger.bgColor('green', 'Background in green.');
@@ -18,38 +41,89 @@ logger.bgColor('blue', 'Background in blue.');
1841
logger.bgColor('magenta', 'Background in magenta.');
1942
logger.bgColor('cyan', 'Background in cyan.');
2043
logger.bgColor('white', 'Background in white.');
21-
logger.log('-------');
22-
logger.degug('This is debug mode');
23-
logger.error('This is error mode');
24-
logger.info('This is info mode');
25-
logger.warn('This is warn mode');
26-
logger.log('-------')
27-
logger.fontColor('test', 'Wrong font color test.');
28-
logger.bgColor('test', 'Wrong background color test.');
29-
logger.log('-------')
44+
logger.bgColor('blue', 'Bold, blue', {
45+
bold: true,
46+
underscore: false,
47+
});
48+
logger.bgColor('blue', 'Bold, dim, underscore, reverse', {
49+
bold: true,
50+
dim: true,
51+
underscore: true,
52+
reverse: true,
53+
});
54+
logger.bgColor('blue', 'Bold, underscore, blue.', {
55+
bold: true,
56+
dim: false,
57+
underscore: true,
58+
reverse: false,
59+
});
60+
logger.log('\n');
61+
62+
logger.log('***********************');
63+
logger.log('*** Test Set Color ****');
64+
logger.log('***********************');
3065
logger.setColor({
3166
font: 'red',
3267
bg: 'black'
3368
}, 'Red font in black background.');
34-
logger.log('-------')
3569
logger.setColor({
3670
font: 'blue',
3771
bg: 'yellow'
3872
}, 'blue font in yellow background.');
39-
logger.log('-------')
73+
logger.setColor({
74+
font: 'red',
75+
bg: 'green'
76+
}, 'Red font in green background.');
77+
logger.setColor({
78+
font: 'red',
79+
bg: 'green'
80+
}, 'Red font in green background, underscore.',{
81+
underscore: true,
82+
reverse: false,
83+
});
84+
logger.setColor({
85+
font: 'red',
86+
bg: 'green'
87+
}, 'Red font in green background, bold, underscore, reverse.',{
88+
bold: true,
89+
dim: false,
90+
underscore: true,
91+
reverse: true,
92+
});
93+
logger.log('\n');
94+
95+
logger.log('**********************');
96+
logger.log('*** Test Level Log ***');
97+
logger.log('**********************');
98+
logger.degug('This is debug mode');
99+
logger.error('This is error mode');
100+
logger.info('This is info mode');
101+
logger.warn('This is warn mode');
102+
logger.log('\n');
103+
104+
logger.log('*************************');
105+
logger.log('*** Test Wrong Usage ****');
106+
logger.log('*************************');
107+
logger.fontColor('test', 'Should be no color.');
108+
logger.bgColor('test', 'Should be no color.');
40109
logger.setColor({
41110
font: 'test',
42111
bg: 'blue'
43-
}, 'Wrong font color in blue background.');
44-
logger.log('-------')
112+
}, 'Font color warning.');
45113
logger.setColor({
46114
font: 'red',
47115
bg: 'test'
48-
}, 'Red font color in wrong background color.');
49-
logger.log('-------')
116+
}, 'Background color warning.');
50117
logger.setColor({
51118
font: 'test',
52119
bg: 'test'
53-
}, 'Wrong font color in wrong background color.');
54-
logger.log('-------')
55-
logger.log('should be reset')
120+
}, 'Font and background color warning.');
121+
logger.fontColor('red', 'Wrong setting.', {
122+
width: true,
123+
});
124+
logger.bgColor('red', 'Wrong setting.', {
125+
dim: 'true',
126+
});
127+
logger.bgColor('red', 'Wrong setting.', {
128+
bold: 4
129+
});

0 commit comments

Comments
 (0)