Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 21st, 2012  |  syntax: None  |  size: 1.68 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Main
  2. {
  3.         public static function main()
  4.         {
  5.                 print(">> ");
  6.  
  7.                 var position = -1;
  8.                 var history = [];
  9.                 var input = "";
  10.  
  11.                 while (true)
  12.                 {
  13.                         var char = neko.io.File.getChar(false);
  14.  
  15.                         switch (char)
  16.                         {
  17.                                 case 27, 91: // ignore
  18.                                 case 65, 66: // up/down
  19.                                 var index = char == 65 ? position + 1 : position - 1;
  20.                                 if (index > -1 && index < history.length)
  21.                                 {
  22.                                         position = index;
  23.  
  24.                                         for (i in 0...input.length) backspace();
  25.                                         input = history[position];
  26.                                         print(input);
  27.                                 }
  28.                                 else
  29.                                 {
  30.                                         bell();
  31.                                 }
  32.                                 case 67, 68: // down, right, left
  33.  
  34.                                 case 127: // backspace
  35.                                 if (input.length > 0)
  36.                                 {
  37.                                         backspace();
  38.                                         input = input.substr(0, -1);
  39.                                 }
  40.  
  41.                                 case 9: // tab
  42.                                 var completions = ["oranges", "apples", "pears"];
  43.                                 for (completion in completions)
  44.                                 {
  45.                                         if (StringTools.startsWith(completion, input))
  46.                                         {
  47.                                                 print(completion.substr(input.length));
  48.                                                 input = completion;
  49.                                                 break;
  50.                                         }
  51.                                 }
  52.  
  53.                                 case 13: // enter
  54.                                 print("\n");
  55.  
  56.                                 if (input == "" && history.length > 0)
  57.                                 {
  58.                                         input = history[position];
  59.                                 }
  60.                                 else
  61.                                 {
  62.                                         history = history.slice(position + 1);
  63.                                         history.unshift(input);
  64.                                 }
  65.  
  66.                                 print("[execute] " + input + "\n");
  67.                                
  68.                                 print(">> ");
  69.                                 input = "";
  70.                                 position = -1;
  71.  
  72.                                 case 3: break; // kill
  73.                                 default:
  74.                                 print(String.fromCharCode(char));
  75.                                 input += String.fromCharCode(char);
  76.                         }
  77.                 }
  78.         }
  79.  
  80.         static function print(message:String)
  81.         {
  82.                 neko.Lib.print(message);
  83.         }
  84.  
  85.         static function backspace()
  86.         {
  87.                 neko.io.File.stdout().writeByte(0x08);
  88.                 neko.io.File.stdout().writeString(" ");
  89.                 neko.io.File.stdout().writeByte(0x08);
  90.         }
  91.  
  92.         static function bell()
  93.         {
  94.                 neko.io.File.stdout().writeByte(0x07);
  95.         }
  96. }