Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt fix #22

Merged
merged 3 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
348 changes: 20 additions & 328 deletions examples/wapm-shell/components/xterm.tsx

Large diffs are not rendered by default.

61 changes: 33 additions & 28 deletions examples/wapm-shell/services/command-runner/command-runner.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { h, Component } from "preact";

import * as Comlink from "comlink";

import { Duplex, PassThrough } from "stream";
import parse_ from "shell-parse";
const parse = parse_;

import { Terminal } from "xterm";

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this imports were not used, so I removed them

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 😄

import parse from "shell-parse";
import { Terminal, IBufferLine } from "xterm";
import Process from "../process/process";

import { CommandOptions, Command } from "./command";

import { CommandOptions } from "./command";
import CommandFetcher from "./command-fetcher";
import LocalEchoController from "../local-echo/LocalEchoController";

let processWorkerBlobUrl: string | undefined = undefined;
const getBlobUrlForProcessWorker = async (xterm: Terminal) => {
Expand Down Expand Up @@ -112,6 +104,7 @@ export default class CommandRunner {
xterm: Terminal;
commandString: string;
stdoutOnCurrentLine: string;
localEcho: LocalEchoController;

commandEndCallback: Function;
commandFetcherCallback: Function;
Expand All @@ -132,6 +125,10 @@ export default class CommandRunner {
(window as any).SharedArrayBuffer && (window as any).Atomics;

this.xterm = xterm;
this.localEcho = new LocalEchoController(this.xterm, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the suffix Controller I think is a little too verbose. Also, in the webdev world, Controllers tend to refer to angularJS Controllers, which is usually associated with a DOM Component. I think this is more of a Service if we were to add a suffix. 🤔 😄 👍

historySize: 0,
maxAutocompleteEntries: 0
});
this.commandString = commandString;
this.stdoutOnCurrentLine = "";

Expand Down Expand Up @@ -171,14 +168,6 @@ export default class CommandRunner {
await this.tryToSpawnProcess(0);
}

sendStdinLine(line: string) {
// Remove our stdout prefix if we have one
const stdin = line.replace(this.stdoutOnCurrentLine, "");

const data = new TextEncoder().encode(stdin);
this.addStdinToSharedStdin(data, 0);
}

addStdinToSharedStdin(data: Uint8Array, processObjectIndex: number) {
// Pass along the stdin to the shared object

Expand Down Expand Up @@ -314,18 +303,21 @@ export default class CommandRunner {
this.pipedStdinDataForNextProcess = newPipedStdinData;
}
} else {
// console.log("processDataCallback");
// Write the output to our terminal
let dataString = new TextDecoder("utf-8").decode(data);
// console.log("processDataCallback", dataString);
this.xterm.write(dataString.replace(/\n/g, "\r\n"));

this.stdoutOnCurrentLine = dataString;
// Check if the data ended with 10 (New Line)
// If it did, then clear the stdout we have on our line
// Otherwise, record this that way we don't send it along our stdin
if (data[data.length - 1] === 10) {
this.stdoutOnCurrentLine = "";
} else {
this.stdoutOnCurrentLine += dataString;
}
// if (data[data.length - 1] === 10) {
// this.stdoutOnCurrentLine = "";
// } else {
// // let lines = dataString.split("\n");
// this.stdoutOnCurrentLine = dataString; //lines[lines.length-1];
// }
}
}

Expand Down Expand Up @@ -357,8 +349,21 @@ export default class CommandRunner {
this.commandEndCallback();
}

// TODO: Maybe Remove this? May not need it? Wait until cleanup...
processStdinReadCallback() {}
processStdinReadCallback() {
// When the stdin read begins
console.log(
`processStdinReadCallback y:${this.xterm.buffer.cursorY}, x: ${this.xterm.buffer.cursorX}, stdoutOnCurrentLine: ${this.stdoutOnCurrentLine}`
);
// let promptRead = (line as IBufferLine).translateToString(false, 0, this.xterm.buffer.cursorX);
// console.log(promptRead);
// this.xterm.write("\u001b[2K\r\u001b[2K");
// let lines = this.stdoutOnCurrentLine.split("\n");
this.localEcho.read("").then((stdin: string) => {
this.stdoutOnCurrentLine = "";
const data = new TextEncoder().encode(stdin);
this.addStdinToSharedStdin(data, 0);
});
}

kill() {
if (!this.isRunning) {
Expand Down
56 changes: 56 additions & 0 deletions examples/wapm-shell/services/local-echo/HistoryController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* The history controller provides an ring-buffer
*/
export class HistoryController {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would like to rename this to CommandHistoryService. 😄

size: number;
entries: Array<string>;
cursor: number;

constructor(size: number) {
this.size = size;
this.entries = [];
this.cursor = 0;
}

/**
* Push an entry and maintain ring buffer size
*/
push(entry: string) {
// Skip empty entries
if (entry.trim() === "") return;
// Skip duplicate entries
const lastEntry = this.entries[this.entries.length - 1];
if (entry == lastEntry) return;
// Keep track of entries
this.entries.push(entry);
if (this.entries.length > this.size) {
this.entries.pop();
}
this.cursor = this.entries.length;
}

/**
* Rewind history cursor on the last entry
*/
rewind() {
this.cursor = this.entries.length;
}

/**
* Returns the previous entry
*/
getPrevious() {
const idx = Math.max(0, this.cursor - 1);
this.cursor = idx;
return this.entries[idx];
}

/**
* Returns the next entry
*/
getNext() {
const idx = Math.min(this.entries.length, this.cursor + 1);
this.cursor = idx;
return this.entries[idx];
}
}
Loading