Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/onboardDebug/vscode-node/copilotDebugCommandHandle.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as l10n from '@vscode/l10n';
7
import * as util from 'util';
8
import { SimpleRPC } from '../node/copilotDebugWorker/rpc';
9
10
export class CopilotDebugCommandHandle {
11
public static COPILOT_LABEL = 'Copilot';
12
13
public get ended() {
14
return this.rpc.ended;
15
}
16
17
constructor(private readonly rpc: SimpleRPC) { }
18
19
output(category: string, output: string): Promise<void> {
20
return this.rpc.callMethod('output', { category, output });
21
}
22
23
exit(code: number, error?: string): Promise<void> {
24
return this.rpc.callMethod('exit', { code, error });
25
}
26
27
question(message: string, defaultValue?: string, singleKey = false): Promise<string | undefined> {
28
return this.rpc.callMethod('question', { message: withLabel('blue', CopilotDebugCommandHandle.COPILOT_LABEL, message), defaultValue, singleKey });
29
}
30
31
confirm(message: string, defaultValue?: boolean): Promise<boolean | undefined> {
32
return this.rpc.callMethod('confirm', { message: withLabel('blue', CopilotDebugCommandHandle.COPILOT_LABEL, message), defaultValue });
33
}
34
35
printLabel(color: KnownColors, message: string): Promise<void> {
36
return this.output('stdout', withLabel(color, CopilotDebugCommandHandle.COPILOT_LABEL, message) + '\r\n');
37
}
38
39
printJson(data: any): Promise<void> {
40
return this.output('stdout', (util.inspect(data, { colors: true }) + '\n').replaceAll('\n', '\r\n'));
41
}
42
43
getFollowupKeys(padStart: number): Promise<'Enter' | 'Q' | 'R' | 'V' | 'S'> {
44
const keys = ['enter', 'r', 's', 'v', 'q'].map(p => `${Style.Reset}${Style.Bold}${p}${Style.Reset}${Style.Dim}`);
45
const loc = l10n.t('press {0} to re-run, {1} to regenerate, {2} to save config, {3} to view it, {4} to quit', ...keys);
46
const str = ' '.repeat(padStart) + Style.Dim + loc + Style.Reset + '\r\n';
47
return this.rpc.callMethod('question', { message: str, singleKey: true });
48
}
49
}
50
51
type KnownColors = 'red' | 'green' | 'blue' | 'cyan';
52
53
const enum Style {
54
Red = '\x1b[31m',
55
Green = '\x1b[32m',
56
Blue = '\x1b[34m',
57
Cyan = '\x1b[36m',
58
Reset = '\x1b[0m',
59
Bold = '\x1b[1m',
60
Inverse = '\x1b[7m',
61
Dim = '\x1b[2m',
62
}
63
64
// we know the user is running the program in a VS Code terminal, so we don't need
65
// to do the color support detection that we would normally need to handle.
66
67
function withLabel(color: KnownColors, label: string, message: string) {
68
const colorCode = (color: KnownColors) => {
69
switch (color) {
70
case 'red':
71
return Style.Red;
72
case 'green':
73
return Style.Green;
74
case 'blue':
75
return Style.Blue;
76
case 'cyan':
77
return Style.Cyan;
78
default:
79
return '';
80
}
81
};
82
83
return `${Style.Bold}${Style.Inverse}${colorCode(color)} ${label} ${Style.Reset} ${colorCode(color)}${message}${Style.Reset}`;
84
}
85
86