Lines of Code | 83 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | 'use strict'; |
||
2 | |||
3 | class OutputFormatterStyle { |
||
4 | constructor(foreground = "white", background = "black", options = []) { |
||
5 | Object.assign(this, { |
||
6 | options: options, |
||
7 | colorList: { |
||
8 | 30: 'black', |
||
9 | 31: 'red', |
||
10 | 32: 'green', |
||
11 | 33: 'yellow', |
||
12 | 34: 'blue', |
||
13 | 35: 'magenta', |
||
14 | 36: 'cyan', |
||
15 | 37: 'white', |
||
16 | |||
17 | 39: 'white' |
||
18 | }, |
||
19 | |||
20 | backgroundList: { |
||
21 | 40: 'black', |
||
22 | 41: 'red', |
||
23 | 42: 'green', |
||
24 | 43: 'yellow', |
||
25 | 44: 'blue', |
||
26 | 45: 'magenta', |
||
27 | 46: 'cyan', |
||
28 | 47: 'white', |
||
29 | |||
30 | 49: 'black' |
||
31 | }, |
||
32 | |||
33 | colors: Object.assign($.terminal.ansi_colors.bold, { |
||
34 | white: $.terminal.ansi_colors.normal.white, |
||
35 | red: $.terminal.ansi_colors.normal.red |
||
36 | }) |
||
37 | }); |
||
38 | |||
39 | this.foreground = this.getColor(foreground); |
||
40 | this.background = this.getColor(background); |
||
41 | } |
||
42 | |||
43 | getColor(color) { |
||
44 | if (this.colors[color]) { |
||
45 | return this.colors[color]; |
||
46 | } else { |
||
47 | return color; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | apply(text) { |
||
52 | return `[[;${this.foreground};${this.background}]${$.terminal.escape_brackets(text)}]`; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | export default class OutputFormatter { |
||
57 | constructor() { |
||
58 | Object.assign(this, { |
||
59 | formatters: { |
||
60 | error: new OutputFormatterStyle('white', 'red'), |
||
61 | info: new OutputFormatterStyle('green'), |
||
62 | comment: new OutputFormatterStyle('yellow'), |
||
63 | question: new OutputFormatterStyle('magenta'), |
||
64 | } |
||
65 | }); |
||
66 | } |
||
67 | |||
68 | error(text) { |
||
69 | return this.formatters.error.apply(text); |
||
70 | } |
||
71 | |||
72 | info(text) { |
||
73 | return this.formatters.info.apply(text); |
||
74 | } |
||
75 | |||
76 | comment(text) { |
||
77 | return this.formatters.comment.apply(text); |
||
78 | } |
||
79 | |||
80 | question(text) { |
||
81 | return this.formatters.question.apply(text); |
||
82 | } |
||
83 | } |
||
84 |