Skip to content

Commit

Permalink
终端颜色
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jun 17, 2024
1 parent c55ba10 commit 4c6e912
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/console/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ConsoleServer } from './server';
import { registerMethod } from './client';
import * as debug from '../debug';
import * as vscode from 'vscode';
import * as terminal from './terminal';

function setPort(port: number) {
if (!env.scriptUri) {
Expand All @@ -21,7 +22,25 @@ function registerAllMethods() {
}

registerMethod('print', async (client, params: PrintParams) => {
client.print(params.message);
let level = params.message.match(/^\[\s*(.*?)\]/)?.[1]?.toLowerCase();
switch (level) {
case 'fatal':
case 'error':
client.print(terminal.COLOR.RED + params.message);
break;
case 'warn':
client.print(terminal.COLOR.YELLOW + params.message);
break;
case 'info':
client.print(terminal.COLOR.CYAN + params.message);
break;
case 'debug':
client.print(terminal.COLOR.GREEN + params.message);
break;
default:
client.print(params.message);
break;
}
});

interface CreateTreeViewParams {
Expand Down
8 changes: 5 additions & 3 deletions src/console/terminal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import * as iconv from "iconv-lite";

const CSI = {
export const COLOR = {
RESET_COLOR: '\x1b[0m',
BLACK: '\x1b[30m',
RED: '\x1b[31m',
Expand All @@ -19,7 +19,9 @@ const CSI = {
BG_MAGENTA: '\x1b[45m',
BG_CYAN: '\x1b[46m',
BG_WHITE: '\x1b[47m',
};

const CSI = {
CURSOR_UP: '\x1b[A',
CURSOR_DOWN: '\x1b[B',
CURSOR_FORWARD: '\x1b[C',
Expand Down Expand Up @@ -284,7 +286,7 @@ class Pseudoterminal implements vscode.Pseudoterminal {
}

private async newStart() {
this.write(`${CSI.GREEN}>${CSI.RESET_COLOR}`);
this.write(`${COLOR.GREEN}>${COLOR.RESET_COLOR}`);
this.undoStack.splice(0);
this.undoIndex = 0;
this.historyIndex = 0;
Expand Down Expand Up @@ -354,7 +356,7 @@ class Pseudoterminal implements vscode.Pseudoterminal {
// 恢复用户的输入
let [newRow, newCol] = await this.requestCursorPos();
this.headPos[0] = newRow;
this.write(`${CSI.GREEN}>${CSI.RESET_COLOR}`);
this.write(`${COLOR.GREEN}>${COLOR.RESET_COLOR}`);
this.refreshLineWithoutUndo(this.inputedData, this.curOffset);
}

Expand Down

0 comments on commit 4c6e912

Please sign in to comment.