Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 79 additions and 4 deletions.
  1. +79 −4 javascripts/tty.js
View
83 javascripts/tty.js
@@ -17,6 +17,9 @@
TTY.prompt.html(escapeHTML(TTY.promptStr));
TTY.readerFn = readerFn;
TTY.inputBuffer = "";
+ TTY.history = [];
+ TTY.historyIdx = -1;
+ TTY.commandCache = "";
blinkCursor(800);
@@ -63,6 +66,7 @@
switch(e.which) {
case 3: // C-c
TTY.command.text(TTY.command.text() + "^C");
+ TTY.commandCache = "";
drawNewLine();
break;
case 12: // C-l
@@ -73,9 +77,7 @@
consumeLine();
break;
case 21: // C-u
- TTY.command.html("");
- TTY.cursor.html(" ");
- TTY.afterCursor.html("");
+ clearLine();
break;
case 32: // Space
TTY.command.html(TTY.command.html() + " ");
@@ -91,11 +93,26 @@
typingEvent("#buffer", "keydown", function(e) {
if (e.metaKey) return;
+ console.log(e.which);
switch (e.which) {
+ // History back
+ case 38:
+ if (! (e.ctrlKey || e.altKey))
+ historyBack();
+ break;
+
+ // History forward
+ case 40:
+ if (! (e.ctrlKey || e.altKey))
+ historyForward();
+ break;
+
// Backward delete
case 72: // H
- if (! e.ctrlKey) break;
+ if (e.ctrlKey)
+ deleteBack();
+ break;
case 8: // Backspace
if (e.ctrlKey || e.altKey)
deleteBackWord();
@@ -160,6 +177,12 @@
case 34: // End
moveToEnd();
break;
+
+ // Swap chars
+ case 84: // T
+ if (e.ctrlKey)
+ swapChars();
+ break;
}
});
}
@@ -265,6 +288,55 @@
TTY.afterCursor.html('');
}
+ function swapChars() {
+ debugger;
+ if (TTY.command.text()) {
+ moveRight();
+ } else if (TTY.afterCursor.text()) {
+ moveRight();
+ moveRight();
+ }
+ var command = TTY.command.text();
+ var formerChar = command[command.length-2];
+ var latterChar = command[command.length-1];
+
+ var newCommand = command.substr(0, command.length-2);
+ if (latterChar) newCommand += latterChar;
+ if (formerChar) newCommand += formerChar;
+ TTY.command.html(escapeHTML(newCommand));
+ }
+
+ function cacheLine() {
+ moveToEnd();
+ TTY.commandCache = TTY.command.text();
+ }
+
+ function clearLine() {
+ TTY.command.html("");
+ TTY.cursor.html(" ");
+ TTY.afterCursor.html("");
+ }
+
+ function historyBack() {
+ var prevCommand = TTY.history[TTY.historyIdx+1];
+ if (prevCommand) {
+ if (TTY.historyIdx == -1) cacheLine();
+ clearLine();
+ TTY.command.html(escapeHTML(prevCommand));
+ TTY.historyIdx++;
+ }
+ }
+
+ function historyForward() {
+ var nextCommand = TTY.history[TTY.historyIdx-1];
+ if (nextCommand) {
+ clearLine();
+ TTY.command.html(escapeHTML(nextCommand));
+ TTY.historyIdx--;
+ } else
+ TTY.command.html(escapeHTML(TTY.commandCache));
+ }
+
function consumeLine() {
var output = TTY.readerFn(TTY.inputBuffer + TTY.command.text());
if (output === false) {
@@ -273,7 +345,10 @@
for (i = TTY.promptStr.length-2; i > 0; i--) newPrompt += " ";
drawNewLine(newPrompt+"->");
} else {
+ TTY.history.unshift(TTY.inputBuffer + TTY.command.text());
+ TTY.historyIdx = -1;
TTY.inputBuffer = "";
+ TTY.commandCache = "";
printOutput(output);
drawNewLine();
}

No commit comments for this range

Something went wrong with that request. Please try again.