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

Implement RIGHT and LEFT keys for cursor navigation in terminal #637

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
35 changes: 23 additions & 12 deletions frontend/src/components/Terminal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,17 @@ export default {
},

removeInput() {
const textAfterCursorLength = this.terminalInputBuffer.length - this.cursorPosition;
const spaces = " ".repeat(textAfterCursorLength);
const backspaceCount = this.terminalInputBuffer.length;
const backspaces = "\b \b".repeat(backspaceCount);
this.cursorPosition = 0;
this.terminal.write(backspaces);
this.terminal.write(spaces + backspaces);
this.terminalInputBuffer = "";
},

mainTerminalConfig() {
this.terminal.onKey(e => {
const code = e.key.charCodeAt(0);
console.debug("Encode: " + JSON.stringify(e.key));

if (e.key === "\r") {
Expand All @@ -180,29 +181,39 @@ export default {
this.$root.emitAgent(this.endpoint, "terminalInput", this.name, buffer + e.key, (err) => {
this.$root.toastError(err.msg);
});

} else if (code === 127) { // Backspace
} else if (e.key === "\u007F") { // Backspace
if (this.cursorPosition > 0) {
this.terminal.write("\b \b");
const trimmedTextBeforeCursor = this.terminalInputBuffer.slice(0, this.cursorPosition - 1);
const textAfterCursor = this.terminalInputBuffer.slice(this.cursorPosition);
const clearAfterCursor = " ".repeat(textAfterCursor.length) + "\b \b".repeat(textAfterCursor.length + 1);
this.terminalInputBuffer = trimmedTextBeforeCursor + textAfterCursor;
this.terminal.write(clearAfterCursor + textAfterCursor + "\b".repeat(textAfterCursor.length));
this.cursorPosition--;
this.terminalInputBuffer = this.terminalInputBuffer.slice(0, -1);
}
} else if (e.key === "\u001B\u005B\u0041" || e.key === "\u001B\u005B\u0042") { // UP OR DOWN
// Do nothing

} else if (e.key === "\u001B\u005B\u0043") { // RIGHT
// TODO
if (this.cursorPosition < this.terminalInputBuffer.length) {
this.terminal.write(this.terminalInputBuffer[this.cursorPosition]);
this.cursorPosition++;
}
} else if (e.key === "\u001B\u005B\u0044") { // LEFT
// TODO
if (this.cursorPosition > 0) {
this.terminal.write("\b");
this.cursorPosition--;
}
} else if (e.key === "\u0003") { // Ctrl + C
console.debug("Ctrl + C");
this.$root.emitAgent(this.endpoint, "terminalInput", this.name, e.key);
this.removeInput();
} else if (e.key === "\u0009" || e.key.startsWith("\u001B")) { // TAB or other special keys
// Do nothing
} else {
const textBeforeCursor = this.terminalInputBuffer.slice(0, this.cursorPosition);
const textAfterCursor = this.terminalInputBuffer.slice(this.cursorPosition);
this.terminalInputBuffer = textBeforeCursor + e.key + textAfterCursor;
this.terminal.write(e.key + textAfterCursor + "\b".repeat(textAfterCursor.length));
this.cursorPosition++;
this.terminalInputBuffer += e.key;
console.log(this.terminalInputBuffer);
this.terminal.write(e.key);
}
});
},
Expand Down
Loading