Skip to content

Commit

Permalink
chore: finalize ZoweTerminal implementation 😋
Browse files Browse the repository at this point in the history
Signed-off-by: zFernand0 <[email protected]>
  • Loading branch information
zFernand0 committed Aug 30, 2024
1 parent 38f601c commit fdd07e2
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 128 deletions.
129 changes: 1 addition & 128 deletions packages/zowe-explorer/src/commands/ZoweCommandProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ProfileManagement } from "../management/ProfileManagement";
import { Definitions } from "../configuration/Definitions";
import { SettingsConfig } from "../configuration/SettingsConfig";
import { FilterDescriptor, FilterItem } from "../management/FilterManagement";
import { ZoweTerminal } from "../tools/ZoweTerminal";

export interface ICommandProviderDialogs {
commandSubmitted: string;
Expand All @@ -33,134 +34,6 @@ export interface ICommandProviderDialogs {
selectProfile: string;
}

class ZoweTerminal implements vscode.Pseudoterminal {
public static readonly termX = ">";
public static readonly Keys = {
EMPTY_LINE: `${this.termX} `,
CLEAR_ALL: "\x1b[2J\x1b[3J\x1b[;H",
CLEAR_LINE: `\x1b[2K\r${this.termX} `,
DEL: "\x1b[P",
ENTER: "\r",
NEW_LINE: "\r\n",
UP: "\x1b[A",
DOWN: "\x1b[B",
RIGHT: "\x1b[C",
LEFT: "\x1b[D",
BACKSPACE: "\x7f",
};

public runCommand: () => string;
public constructor(public terminalName: string) {}

protected command = "";
protected history: string[] = [];
private historyIndex = 0;

private writeEmitter = new vscode.EventEmitter<string>();
protected write(text: string) {
this.writeEmitter.fire(text);
}
protected writeLine(text: string) {
this.write(text);
this.writeEmitter.fire(ZoweTerminal.Keys.NEW_LINE);
this.writeEmitter.fire(ZoweTerminal.Keys.EMPTY_LINE);
}
protected refreshCmd() {
this.write(ZoweTerminal.Keys.CLEAR_LINE);
this.write(this.command);
}
private cursorPosition = 0;

public onDidWrite: vscode.Event<string> = this.writeEmitter.event;

private closeEmitter = new vscode.EventEmitter<void>();
public onDidClose?: vscode.Event<void> = this.closeEmitter.event;

// Start is called when the terminal is opened
public open(initialDimensions: vscode.TerminalDimensions | undefined): void {
this.writeLine(`Welcome to the ${this.terminalName} Terminal!`);
}

// Close is called when the terminal is closed
public close(): void {
this.closeEmitter.fire();
}

// Handle input from the terminal
public handleInput(data: string): void {
console.log(data, this.historyIndex, this.history);
if (data === ZoweTerminal.Keys.UP) {
this.command = this.history[this.historyIndex] ?? "";
this.historyIndex = Math.max(0, this.historyIndex - 1);
this.cursorPosition = this.command.length;
this.refreshCmd();
return;
}
if (data === ZoweTerminal.Keys.DOWN) {
if (this.historyIndex === this.history.length - 1) {
this.command = "";
} else {
this.historyIndex = Math.min(Math.max(0, this.history.length - 1), this.historyIndex + 1);
this.command = this.history[this.historyIndex] ?? "";
}
this.cursorPosition = this.command.length;
this.refreshCmd();
return;
}
if (data === ZoweTerminal.Keys.LEFT) {
this.cursorPosition = Math.max(0, this.cursorPosition - 1);
this.write(ZoweTerminal.Keys.LEFT);
return;
}
if (data === ZoweTerminal.Keys.RIGHT) {
this.cursorPosition = Math.min(Math.max(0, this.command.length - 1), this.cursorPosition + 1);
this.write(ZoweTerminal.Keys.RIGHT);
return;
}
if (data === ZoweTerminal.Keys.BACKSPACE) {
if (this.command.length === 0) {
return;
}
const tmp = this.command.split("");
tmp.splice(this.cursorPosition - 1, 1);
this.cursorPosition--;
this.command = tmp.join("");
this.write(ZoweTerminal.Keys.LEFT);
this.write(ZoweTerminal.Keys.DEL);
// this.refreshCmd();
return;
}
if (data === ZoweTerminal.Keys.ENTER) {
this.write(ZoweTerminal.Keys.NEW_LINE);
if (this.command.length === 0) {
this.write(ZoweTerminal.Keys.EMPTY_LINE);
return;
}
if (this.command === "hello") {
this.writeLine("Hello there!");
} else if (this.command === "date") {
this.writeLine(`Current date: ${new Date().toLocaleString()}`);
} else if (this.command === "exit") {
this.writeLine("Exiting...");
this.closeEmitter.fire();
} else {
this.writeLine(`Unknown command: ${this.command}`);
}

if (this.command !== this.history[this.historyIndex]) {
this.historyIndex = this.history.length;
this.history.push(this.command);
}
this.command = "";
this.cursorPosition = 0;
return;
}

this.writeEmitter.fire(data.trim());
this.command += data.trim();
this.cursorPosition++;
}
}
export abstract class ZoweCommandProvider {
// eslint-disable-next-line no-magic-numbers
private static readonly totalFilters: number = 10;
Expand Down
159 changes: 159 additions & 0 deletions packages/zowe-explorer/src/tools/ZoweTerminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import * as vscode from "vscode";

export class ZoweTerminal implements vscode.Pseudoterminal {
public static readonly mTermX = ">";
public static readonly Keys = {
EMPTY_LINE: `${this.mTermX} `,
CLEAR_ALL: "\x1b[2J\x1b[3J\x1b[;H",
CLEAR_LINE: `\x1b[2K\r${this.mTermX} `,
DEL: "\x1b[P",
ENTER: "\r",
NEW_LINE: "\r\n",
UP: "\x1b[A",
DOWN: "\x1b[B",
RIGHT: "\x1b[C",
LEFT: "\x1b[D",
BACKSPACE: "\x7f",
};

public constructor(terminalName: string, message?: string, history: string[] = []) {
this.mTerminalName = terminalName;
this.mMessage = message ?? `Welcome to the ${this.mTerminalName} Terminal!`;
this.mHistory = history;
this.historyIndex = history.length;
}

private mMessage: string;
protected mTerminalName: string = "";
protected mHistory: string[];
private historyIndex: number;

private writeEmitter = new vscode.EventEmitter<string>();
protected write(text: string) {
this.writeEmitter.fire(text);
}
protected writeLine(text: string) {
this.write(text);
this.write(ZoweTerminal.Keys.NEW_LINE);
this.write(ZoweTerminal.Keys.EMPTY_LINE);
}
protected refreshCmd() {
this.write(ZoweTerminal.Keys.CLEAR_LINE);
this.write(this.command);
}
protected clear() {
this.write(ZoweTerminal.Keys.CLEAR_ALL);
this.writeLine(this.mMessage);
}

protected command: string = "";
protected cursorPosition = 0;

public onDidWrite: vscode.Event<string> = this.writeEmitter.event;

private closeEmitter = new vscode.EventEmitter<void>();
public onDidClose?: vscode.Event<void> = this.closeEmitter.event;

// Start is called when the terminal is opened
public open(initialDimensions: vscode.TerminalDimensions | undefined): void {
this.writeLine(this.mMessage);
}

// Close is called when the terminal is closed
public close(): void {
this.closeEmitter.fire();
}

// Handle input from the terminal
public handleInput(data: string): void {
console.log(data, this.historyIndex, this.mHistory);
if (data === ZoweTerminal.Keys.UP) {
this.historyIndex = Math.max(0, this.historyIndex - 1);
this.command = this.mHistory[this.historyIndex] ?? "";
this.cursorPosition = this.command.length;
this.refreshCmd();
return;
}
if (data === ZoweTerminal.Keys.DOWN) {
if (this.historyIndex === this.mHistory.length) {
this.command = "";
} else {
this.historyIndex = Math.min(this.mHistory.length, this.historyIndex + 1);
this.command = this.mHistory[this.historyIndex] ?? "";
}
this.cursorPosition = this.command.length;
this.refreshCmd();
return;
}
if (data === ZoweTerminal.Keys.LEFT) {
this.cursorPosition = Math.max(0, this.cursorPosition - 1);
if (this.cursorPosition > 0) {
this.write(ZoweTerminal.Keys.LEFT);
}
return;
}
if (data === ZoweTerminal.Keys.RIGHT) {
this.cursorPosition = Math.min(this.command.length, this.cursorPosition + 1);
if (this.cursorPosition < this.command.length) {
this.write(ZoweTerminal.Keys.RIGHT);
}
return;
}
if (data === ZoweTerminal.Keys.BACKSPACE) {
if (this.command.length === 0) {
return;
}
this.write(ZoweTerminal.Keys.LEFT);
this.write(ZoweTerminal.Keys.DEL);

const tmp = this.command.split("");
tmp.splice(this.cursorPosition - 1, 1);
this.command = tmp.join("");

this.cursorPosition = Math.max(0, this.cursorPosition - 1);

// this.refreshCmd();
return;
}
if (data === ZoweTerminal.Keys.ENTER) {
this.write(ZoweTerminal.Keys.NEW_LINE);
if (this.command.length === 0) {
this.write(ZoweTerminal.Keys.EMPTY_LINE);
return;
}
if (this.command === "hello") {
this.writeLine("Hello there!");
} else if (this.command === ":clear") {
this.clear();
} else if (this.command === "date") {
this.writeLine(`Current date: ${new Date().toLocaleString()}`);
} else if (this.command === ":exit") {
this.writeLine("Exiting...");
this.closeEmitter.fire();
} else {
this.writeLine(`Unknown command: ${this.command}`);
}

this.mHistory.push(this.command);
this.historyIndex = this.mHistory.length;
this.cursorPosition = 0;
this.command = "";
return;
}

this.write(data);
this.command += data;
this.cursorPosition = Math.min(this.command.length, this.cursorPosition + 1);
}
}

0 comments on commit fdd07e2

Please sign in to comment.