Skip to content

Instantly share code, notes, and snippets.

@dpeek
Created April 5, 2012 12:52
Show Gist options
  • Save dpeek/2310849 to your computer and use it in GitHub Desktop.
Save dpeek/2310849 to your computer and use it in GitHub Desktop.
History/tab completion for Neko programs
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);
}
}
@dpeek
Copy link
Author

dpeek commented Apr 5, 2012

Test with:
$ haxe -x Main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment