|
| 1 | +export class PrettyConsole { |
| 2 | + closeByNewLine = true; |
| 3 | + useIcons = true; |
| 4 | + logsTitle = "LOGS"; |
| 5 | + warningsTitle = "WARNINGS"; |
| 6 | + errorsTitle = "ERRORS"; |
| 7 | + informationsTitle = "INFORMATIONS"; |
| 8 | + successesTitle = "SUCCESS"; |
| 9 | + debugsTitle = "DEBUG"; |
| 10 | + assertsTitle = "ASSERT"; |
| 11 | + #getColor(foregroundColor = "", backgroundColor = "") { |
| 12 | + let fgc = "\x1b[37m"; |
| 13 | + switch (foregroundColor.trim().toLowerCase()) { |
| 14 | + case "black": |
| 15 | + fgc = "\x1b[30m"; |
| 16 | + break; |
| 17 | + case "red": |
| 18 | + fgc = "\x1b[31m"; |
| 19 | + break; |
| 20 | + case "green": |
| 21 | + fgc = "\x1b[32m"; |
| 22 | + break; |
| 23 | + case "yellow": |
| 24 | + fgc = "\x1b[33m"; |
| 25 | + break; |
| 26 | + case "blue": |
| 27 | + fgc = "\x1b[34m"; |
| 28 | + break; |
| 29 | + case "magenta": |
| 30 | + fgc = "\x1b[35m"; |
| 31 | + break; |
| 32 | + case "cyan": |
| 33 | + fgc = "\x1b[36m"; |
| 34 | + break; |
| 35 | + case "white": |
| 36 | + fgc = "\x1b[37m"; |
| 37 | + break; |
| 38 | + } |
| 39 | + |
| 40 | + let bgc = ""; |
| 41 | + switch (backgroundColor.trim().toLowerCase()) { |
| 42 | + case "black": |
| 43 | + bgc = "\x1b[40m"; |
| 44 | + break; |
| 45 | + case "red": |
| 46 | + bgc = "\x1b[44m"; |
| 47 | + break; |
| 48 | + case "green": |
| 49 | + bgc = "\x1b[44m"; |
| 50 | + break; |
| 51 | + case "yellow": |
| 52 | + bgc = "\x1b[43m"; |
| 53 | + break; |
| 54 | + case "blue": |
| 55 | + bgc = "\x1b[44m"; |
| 56 | + break; |
| 57 | + case "magenta": |
| 58 | + bgc = "\x1b[45m"; |
| 59 | + break; |
| 60 | + case "cyan": |
| 61 | + bgc = "\x1b[46m"; |
| 62 | + break; |
| 63 | + case "white": |
| 64 | + bgc = "\x1b[47m"; |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + return `${fgc}${bgc}`; |
| 69 | + } |
| 70 | + #getColorReset() { |
| 71 | + return "\x1b[0m"; |
| 72 | + } |
| 73 | + clear() { |
| 74 | + console.clear(); |
| 75 | + } |
| 76 | + print(foregroundColor = "white", backgroundColor = "black", ...strings) { |
| 77 | + const c = this.#getColor(foregroundColor, backgroundColor); |
| 78 | + // turns objects into printable strings |
| 79 | + strings = strings.map((item) => { |
| 80 | + if (typeof item === "object") item = JSON.stringify(item); |
| 81 | + return item; |
| 82 | + }); |
| 83 | + console.log(c, strings.join(""), this.#getColorReset()); |
| 84 | + if (this.closeByNewLine) console.log(""); |
| 85 | + } |
| 86 | + log(...strings) { |
| 87 | + const fg = "white"; |
| 88 | + const bg = ""; |
| 89 | + const icon = "\u25ce"; |
| 90 | + const groupTile = ` ${this.logsTitle}`; |
| 91 | + if (strings.length > 1) { |
| 92 | + const c = this.#getColor(fg, bg); |
| 93 | + console.group(c, (this.useIcons ? icon : "") + groupTile); |
| 94 | + const nl = this.closeByNewLine; |
| 95 | + this.closeByNewLine = false; |
| 96 | + strings.forEach((item) => { |
| 97 | + this.print(fg, bg, item, this.#getColorReset()); |
| 98 | + }); |
| 99 | + this.closeByNewLine = nl; |
| 100 | + console.groupEnd(); |
| 101 | + if (nl) console.log(); |
| 102 | + } else { |
| 103 | + this.print( |
| 104 | + fg, |
| 105 | + bg, |
| 106 | + strings.map((item) => { |
| 107 | + return `${this.useIcons ? `${icon} ` : ""}${item}`; |
| 108 | + }) |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | + warn(...strings) { |
| 113 | + const fg = "yellow"; |
| 114 | + const bg = ""; |
| 115 | + const icon = "\u26a0"; |
| 116 | + const groupTile = ` ${this.warningsTitle}`; |
| 117 | + if (strings.length > 1) { |
| 118 | + const c = this.#getColor(fg, bg); |
| 119 | + console.group(c, (this.useIcons ? icon : "") + groupTile); |
| 120 | + const nl = this.closeByNewLine; |
| 121 | + this.closeByNewLine = false; |
| 122 | + strings.forEach((item) => { |
| 123 | + this.print(fg, bg, item, this.#getColorReset()); |
| 124 | + }); |
| 125 | + this.closeByNewLine = nl; |
| 126 | + console.groupEnd(); |
| 127 | + if (nl) console.log(); |
| 128 | + } else { |
| 129 | + this.print( |
| 130 | + fg, |
| 131 | + bg, |
| 132 | + strings.map((item) => { |
| 133 | + return `${this.useIcons ? `${icon} ` : ""}${item}`; |
| 134 | + }) |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + error(...strings) { |
| 139 | + const fg = "red"; |
| 140 | + const bg = ""; |
| 141 | + const icon = "\u26D4"; |
| 142 | + const groupTile = ` ${this.errorsTitle}`; |
| 143 | + if (strings.length > 1) { |
| 144 | + const c = this.#getColor(fg, bg); |
| 145 | + console.group(c, (this.useIcons ? icon : "") + groupTile); |
| 146 | + const nl = this.closeByNewLine; |
| 147 | + this.closeByNewLine = false; |
| 148 | + strings.forEach((item) => { |
| 149 | + this.print(fg, bg, item); |
| 150 | + }); |
| 151 | + this.closeByNewLine = nl; |
| 152 | + console.groupEnd(); |
| 153 | + if (nl) console.log(); |
| 154 | + } else { |
| 155 | + this.print( |
| 156 | + fg, |
| 157 | + bg, |
| 158 | + strings.map((item) => { |
| 159 | + return `${this.useIcons ? `${icon} ` : ""}${item}`; |
| 160 | + }) |
| 161 | + ); |
| 162 | + } |
| 163 | + } |
| 164 | + info(...strings) { |
| 165 | + const fg = "blue"; |
| 166 | + const bg = ""; |
| 167 | + const icon = "\u2139"; |
| 168 | + const groupTile = ` ${this.informationsTitle}`; |
| 169 | + if (strings.length > 1) { |
| 170 | + const c = this.#getColor(fg, bg); |
| 171 | + console.group(c, (this.useIcons ? icon : "") + groupTile); |
| 172 | + const nl = this.closeByNewLine; |
| 173 | + this.closeByNewLine = false; |
| 174 | + strings.forEach((item) => { |
| 175 | + this.print(fg, bg, item); |
| 176 | + }); |
| 177 | + this.closeByNewLine = nl; |
| 178 | + console.groupEnd(); |
| 179 | + if (nl) console.log(); |
| 180 | + } else { |
| 181 | + this.print( |
| 182 | + fg, |
| 183 | + bg, |
| 184 | + strings.map((item) => { |
| 185 | + return `${this.useIcons ? `${icon} ` : ""}${item}`; |
| 186 | + }) |
| 187 | + ); |
| 188 | + } |
| 189 | + } |
| 190 | + success(...strings) { |
| 191 | + const fg = "green"; |
| 192 | + const bg = ""; |
| 193 | + const icon = "\u2713"; |
| 194 | + const groupTile = ` ${this.successesTitle}`; |
| 195 | + if (strings.length > 1) { |
| 196 | + const c = this.#getColor(fg, bg); |
| 197 | + console.group(c, (this.useIcons ? icon : "") + groupTile); |
| 198 | + const nl = this.closeByNewLine; |
| 199 | + this.closeByNewLine = false; |
| 200 | + strings.forEach((item) => { |
| 201 | + this.print(fg, bg, item); |
| 202 | + }); |
| 203 | + this.closeByNewLine = nl; |
| 204 | + console.groupEnd(); |
| 205 | + if (nl) console.log(); |
| 206 | + } else { |
| 207 | + this.print( |
| 208 | + fg, |
| 209 | + bg, |
| 210 | + strings.map((item) => { |
| 211 | + return `${this.useIcons ? `${icon} ` : ""}${item}`; |
| 212 | + }) |
| 213 | + ); |
| 214 | + } |
| 215 | + } |
| 216 | + debug(...strings) { |
| 217 | + const fg = "magenta"; |
| 218 | + const bg = ""; |
| 219 | + const icon = "\u1367"; |
| 220 | + const groupTile = ` ${this.debugsTitle}`; |
| 221 | + if (strings.length > 1) { |
| 222 | + const c = this.#getColor(fg, bg); |
| 223 | + console.group(c, (this.useIcons ? icon : "") + groupTile); |
| 224 | + const nl = this.closeByNewLine; |
| 225 | + this.closeByNewLine = false; |
| 226 | + strings.forEach((item) => { |
| 227 | + this.print(fg, bg, item); |
| 228 | + }); |
| 229 | + this.closeByNewLine = nl; |
| 230 | + console.groupEnd(); |
| 231 | + if (nl) console.log(); |
| 232 | + } else { |
| 233 | + this.print( |
| 234 | + fg, |
| 235 | + bg, |
| 236 | + strings.map((item) => { |
| 237 | + return `${this.useIcons ? `${icon} ` : ""}${item}`; |
| 238 | + }) |
| 239 | + ); |
| 240 | + } |
| 241 | + } |
| 242 | + assert(...strings) { |
| 243 | + const fg = "cyan"; |
| 244 | + const bg = ""; |
| 245 | + const icon = "\u0021"; |
| 246 | + const groupTile = ` ${this.assertsTitle}`; |
| 247 | + if (strings.length > 1) { |
| 248 | + const c = this.#getColor(fg, bg); |
| 249 | + console.group(c, (this.useIcons ? icon : "") + groupTile); |
| 250 | + const nl = this.closeByNewLine; |
| 251 | + this.closeByNewLine = false; |
| 252 | + strings.forEach((item) => { |
| 253 | + this.print(fg, bg, item); |
| 254 | + }); |
| 255 | + this.closeByNewLine = nl; |
| 256 | + console.groupEnd(); |
| 257 | + if (nl) console.log(); |
| 258 | + } else { |
| 259 | + this.print( |
| 260 | + fg, |
| 261 | + bg, |
| 262 | + strings.map((item) => { |
| 263 | + return `${this.useIcons ? `${icon} ` : ""}${item}`; |
| 264 | + }) |
| 265 | + ); |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments