Skip to content

Commit bf6be5e

Browse files
committed
added option to enable/disable colors
1 parent e7caf84 commit bf6be5e

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/app-settings.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
3+
import {ConsoleOutput} from './obj/console-output';
34

45
export class AppSettings {
56
static get appPath(): string {
@@ -29,6 +30,7 @@ export class AppSettings {
2930
);
3031

3132
this._settings = JSON.parse(settings) as Configuration;
33+
ConsoleOutput.showColors = this._settings.console.showColors;
3234
}
3335
}
3436

@@ -52,6 +54,7 @@ export interface Configuration {
5254
}
5355
},
5456
'console': {
55-
'tty': boolean
57+
'tty': boolean,
58+
'showColors': boolean
5659
}
5760
}

src/config_sample.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
}
1919
},
2020
"console": {
21-
"tty": false
21+
"tty": false,
22+
"showColors": false
2223
}
2324
}

src/obj/console-output.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,53 @@ import * as process from 'process';
33

44
export class ConsoleOutput {
55
private static lastWasLive = false;
6+
public static showColors = true;
67

78
public static log(message: string) {
8-
if(this.lastWasLive){
9-
console.log("");
9+
if (this.lastWasLive) {
10+
console.log('');
1011
this.lastWasLive = false;
1112
}
1213
console.log(`${message}`);
1314
}
1415

1516
public static error(message: string) {
16-
if(this.lastWasLive){
17-
console.log("");
17+
if (this.lastWasLive) {
18+
console.log('');
1819
this.lastWasLive = false;
1920
}
20-
console.error(`${message}`);
21+
const formattedStr = (ConsoleOutput.showColors) ? ConsoleOutput.format(message, 'error') : message;
22+
console.error(formattedStr);
2123
}
2224

2325
public static info(message: string) {
24-
if(this.lastWasLive){
25-
console.log("");
26+
if (this.lastWasLive) {
27+
console.log('');
2628
this.lastWasLive = false;
2729
}
28-
console.log(`${message}`);
30+
31+
const formattedStr = (ConsoleOutput.showColors) ? ConsoleOutput.format(message, 'info') : message;
32+
console.error(formattedStr);
2933
}
3034

3135
public static success(message: string) {
32-
if(this.lastWasLive){
33-
console.log("");
36+
if (this.lastWasLive) {
37+
console.log('');
3438
this.lastWasLive = false;
3539
}
36-
console.log(`${message}`);
40+
41+
const formattedStr = (ConsoleOutput.showColors) ? ConsoleOutput.format(message, 'success') : message;
42+
console.error(formattedStr);
3743
}
3844

3945
public static warning(message: string) {
40-
if(this.lastWasLive){
41-
console.log("");
46+
if (this.lastWasLive) {
47+
console.log('');
4248
this.lastWasLive = false;
4349
}
44-
console.log(`${message}`);
50+
51+
const formattedStr = (ConsoleOutput.showColors) ? ConsoleOutput.format(message, 'warning') : message;
52+
console.error(formattedStr);
4553
}
4654

4755
public static logLive = (message: string) => {
@@ -50,6 +58,19 @@ export class ConsoleOutput {
5058
readline.cursorTo(process.stdout, 0, null);
5159
process.stdout.write(message);
5260
ConsoleOutput.lastWasLive = true;
61+
};
62+
63+
public static format(message: string, type: 'error' | 'info' | 'success' | 'warning') {
64+
switch (type) {
65+
case 'info':
66+
return `\x1b[34m${message}\x1b[0m`;
67+
case 'warning':
68+
return `\x1b[33m${message}\x1b[0m`;
69+
case 'error':
70+
return `\x1b[31m${message}\x1b[0m`;
71+
case 'success':
72+
return `\x1b[32m${message}\x1b[0m`;
73+
}
5374
}
5475

5576

0 commit comments

Comments
 (0)