Path: blob/main/extensions/copilot/src/extension/onboardDebug/vscode-node/copilotDebugCommandHandle.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as l10n from '@vscode/l10n';6import * as util from 'util';7import { SimpleRPC } from '../node/copilotDebugWorker/rpc';89export class CopilotDebugCommandHandle {10public static COPILOT_LABEL = 'Copilot';1112public get ended() {13return this.rpc.ended;14}1516constructor(private readonly rpc: SimpleRPC) { }1718output(category: string, output: string): Promise<void> {19return this.rpc.callMethod('output', { category, output });20}2122exit(code: number, error?: string): Promise<void> {23return this.rpc.callMethod('exit', { code, error });24}2526question(message: string, defaultValue?: string, singleKey = false): Promise<string | undefined> {27return this.rpc.callMethod('question', { message: withLabel('blue', CopilotDebugCommandHandle.COPILOT_LABEL, message), defaultValue, singleKey });28}2930confirm(message: string, defaultValue?: boolean): Promise<boolean | undefined> {31return this.rpc.callMethod('confirm', { message: withLabel('blue', CopilotDebugCommandHandle.COPILOT_LABEL, message), defaultValue });32}3334printLabel(color: KnownColors, message: string): Promise<void> {35return this.output('stdout', withLabel(color, CopilotDebugCommandHandle.COPILOT_LABEL, message) + '\r\n');36}3738printJson(data: any): Promise<void> {39return this.output('stdout', (util.inspect(data, { colors: true }) + '\n').replaceAll('\n', '\r\n'));40}4142getFollowupKeys(padStart: number): Promise<'Enter' | 'Q' | 'R' | 'V' | 'S'> {43const keys = ['enter', 'r', 's', 'v', 'q'].map(p => `${Style.Reset}${Style.Bold}${p}${Style.Reset}${Style.Dim}`);44const loc = l10n.t('press {0} to re-run, {1} to regenerate, {2} to save config, {3} to view it, {4} to quit', ...keys);45const str = ' '.repeat(padStart) + Style.Dim + loc + Style.Reset + '\r\n';46return this.rpc.callMethod('question', { message: str, singleKey: true });47}48}4950type KnownColors = 'red' | 'green' | 'blue' | 'cyan';5152const enum Style {53Red = '\x1b[31m',54Green = '\x1b[32m',55Blue = '\x1b[34m',56Cyan = '\x1b[36m',57Reset = '\x1b[0m',58Bold = '\x1b[1m',59Inverse = '\x1b[7m',60Dim = '\x1b[2m',61}6263// we know the user is running the program in a VS Code terminal, so we don't need64// to do the color support detection that we would normally need to handle.6566function withLabel(color: KnownColors, label: string, message: string) {67const colorCode = (color: KnownColors) => {68switch (color) {69case 'red':70return Style.Red;71case 'green':72return Style.Green;73case 'blue':74return Style.Blue;75case 'cyan':76return Style.Cyan;77default:78return '';79}80};8182return `${Style.Bold}${Style.Inverse}${colorCode(color)} ${label} ${Style.Reset} ${colorCode(color)}${message}${Style.Reset}`;83}848586