Created
April 5, 2012 12:52
-
-
Save dpeek/2310849 to your computer and use it in GitHub Desktop.
History/tab completion for Neko programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main | |
{ | |
public static function main() | |
{ | |
print(">> "); | |
var position = -1; | |
var history = []; | |
var input = ""; | |
while (true) | |
{ | |
var char = neko.io.File.getChar(false); | |
switch (char) | |
{ | |
case 27, 91: // ignore | |
case 65, 66: // up/down | |
var index = char == 65 ? position + 1 : position - 1; | |
if (index > -1 && index < history.length) | |
{ | |
position = index; | |
for (i in 0...input.length) backspace(); | |
input = history[position]; | |
print(input); | |
} | |
else | |
{ | |
bell(); | |
} | |
case 67, 68: // down, right, left | |
case 127: // backspace | |
if (input.length > 0) | |
{ | |
backspace(); | |
input = input.substr(0, -1); | |
} | |
case 9: // tab | |
var completions = ["oranges", "apples", "pears"]; | |
for (completion in completions) | |
{ | |
if (StringTools.startsWith(completion, input)) | |
{ | |
print(completion.substr(input.length)); | |
input = completion; | |
break; | |
} | |
} | |
case 13: // enter | |
print("\n"); | |
if (input == "" && history.length > 0) | |
{ | |
input = history[position]; | |
} | |
else | |
{ | |
history = history.slice(position + 1); | |
history.unshift(input); | |
} | |
print("[execute] " + input + "\n"); | |
print(">> "); | |
input = ""; | |
position = -1; | |
case 3: break; // kill | |
default: | |
print(String.fromCharCode(char)); | |
input += String.fromCharCode(char); | |
} | |
} | |
} | |
static function print(message:String) | |
{ | |
neko.Lib.print(message); | |
} | |
static function backspace() | |
{ | |
neko.io.File.stdout().writeByte(0x08); | |
neko.io.File.stdout().writeString(" "); | |
neko.io.File.stdout().writeByte(0x08); | |
} | |
static function bell() | |
{ | |
neko.io.File.stdout().writeByte(0x07); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test with:
$ haxe -x Main