| 1 | import chalk from "chalk"; |
| 2 | import * as vscode from "vscode"; |
| 3 | |
| 4 | type Level = "info" | "error" | "warning"; |
| 5 | |
| 6 | class Output { |
| 7 | outputChannel: vscode.OutputChannel | undefined; |
| 8 | |
| 9 | activate() { |
| 10 | if (!this.outputChannel) { |
| 11 | this.outputChannel = vscode.window.createOutputChannel("Regex File"); |
| 12 | this.show(); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | show() { |
| 17 | this.outputChannel?.show(); |
| 18 | } |
| 19 | |
| 20 | clear() { |
| 21 | this.outputChannel?.clear(); |
| 22 | } |
| 23 | |
| 24 | dispose() { |
| 25 | this.outputChannel?.dispose(); |
| 26 | } |
| 27 | |
| 28 | hide() { |
| 29 | this.outputChannel?.hide(); |
| 30 | } |
| 31 | |
| 32 | log(message: string, level: Level = "info") { |
| 33 | const dateString = new Date().toLocaleTimeString(); |
| 34 | const reset = "\x1b[0m"; |
| 35 | const blue = "\x1b[34m"; |
| 36 | const green = "\x1b[32m"; |
| 37 | const red = "\x1b[31m"; |
| 38 | const yellow = "\x1b[33m"; |
| 39 | const whiteBright = "\x1b[97m"; |
| 40 | |
| 41 | let output = `[${dateString}] `; |
| 42 | if (level === "info") { |
| 43 | output += `[info] `; |
| 44 | } else if (level === "error") { |
| 45 | output += `[error] `; |
| 46 | } else if (level === "warning") { |
| 47 | output += `[warning] `; |
| 48 | } |
| 49 | output += `${message}`; |
| 50 | |
| 51 | |
| 52 | let coloredOutput = `${blue}[${dateString}] ${reset}`; |
| 53 | if (level === "info") { |
| 54 | coloredOutput += `${green}[info] ${reset}`; |
| 55 | } else if (level === "error") { |
| 56 | coloredOutput += `${red}[error] ${reset}`; |
| 57 | } else if (level === "warning") { |
| 58 | coloredOutput += `${yellow}[warning] ${reset}`; |
| 59 | } |
| 60 | coloredOutput += `${whiteBright}${message}${reset}`; |
| 61 | |
| 62 | |
| 63 | |
| 64 | this.outputChannel?.appendLine(message); |
| 65 | } |
| 66 | |
| 67 | deactivate() { |
| 68 | this.hide(); |
| 69 | this.dispose(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | export const output = new Output(); |