From 5bade22868233400348cc83f773acc674974b404 Mon Sep 17 00:00:00 2001 From: Oleksandr Andriienko Date: Tue, 7 Apr 2020 11:33:22 +0300 Subject: [PATCH] Display exec error or exit information in the UI cloud shell. (#89) Signed-off-by: Oleksandr Andriienko --- cloud-shell/src/const.ts | 15 +++++++++++++++ cloud-shell/src/index.ts | 15 +++++++++++++-- cloud-shell/src/terminal-protocol.ts | 13 +++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 cloud-shell/src/const.ts diff --git a/cloud-shell/src/const.ts b/cloud-shell/src/const.ts new file mode 100644 index 000000000..89ec529d2 --- /dev/null +++ b/cloud-shell/src/const.ts @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2020 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ + +export namespace ANSIControlSequences { + export const RESET_COLOR = '\u001b[0m' + export const RED_COLOR = '\u001b[31m' + export const GREEN_COLOR = '\u001b[32m' + } diff --git a/cloud-shell/src/index.ts b/cloud-shell/src/index.ts index c8ae63b60..964742359 100644 --- a/cloud-shell/src/index.ts +++ b/cloud-shell/src/index.ts @@ -11,7 +11,9 @@ import { CloudShellTerminal, TerminalHandler } from "./terminal"; import { JsonRpcConnection } from "./json-rpc-connection"; import { GenericNotificationHandler } from "vscode-jsonrpc"; -import { MachineExec } from "./terminal-protocol"; +import { MachineExec, EXIT_METHOD, ERROR_METHOD, ExecExitEvent, ExecErrorEvent } from "./terminal-protocol"; +import { NotificationType } from 'vscode-ws-jsonrpc'; +import { ANSIControlSequences as CS } from './const'; const terminalElem = document.getElementById('terminal-container'); @@ -33,7 +35,7 @@ rpcConnecton.create().then(connection => { const exec: MachineExec = { tty: true, cols: terminal.cols, - rows: terminal.rows, + rows: terminal.rows }; connection.sendRequest<{}>('create', exec).then((value: {}) => { @@ -67,6 +69,15 @@ rpcConnecton.create().then(connection => { } }); }); + const exitNotification = new NotificationType(EXIT_METHOD); + connection.onNotification(exitNotification, (event: ExecExitEvent) => { + terminal.sendLine(CS.GREEN_COLOR + "Process completed." + CS.RESET_COLOR) + }); + + const errorNotification = new NotificationType(ERROR_METHOD); + connection.onNotification(errorNotification, (event: ExecErrorEvent) => { + terminal.sendLine(CS.RED_COLOR + 'Failed to create terminal. Error: ' + event.stack + CS.RESET_COLOR) + }); }).catch(err => { console.log('Fatal. Unable to connect to container.', err); }) diff --git a/cloud-shell/src/terminal-protocol.ts b/cloud-shell/src/terminal-protocol.ts index 44ff144ce..eb11f9e46 100644 --- a/cloud-shell/src/terminal-protocol.ts +++ b/cloud-shell/src/terminal-protocol.ts @@ -8,6 +8,9 @@ * Red Hat, Inc. - initial API and implementation */ +export const EXIT_METHOD: string = 'onExecExit'; +export const ERROR_METHOD: string = 'onExecError'; + export interface MachineIdentifier { machineName: string, workspaceId: string @@ -22,6 +25,16 @@ export interface MachineExec { id?: number } +export interface ExecExitEvent { + id: number; + code: number; +} + +export interface ExecErrorEvent { + id: number; + stack: string; +} + export interface IdParam { id: number }