perl: Remove _nil_Q(), _true_Q(), and _false_Q().
[jackhill/mal.git] / chuck / readline.ck
1 public class Readline
2 {
3 fun static string readline(string prompt)
4 {
5 int done;
6 string input;
7 KBHit kb;
8 int char;
9 string repr;
10
11 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
12 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
13 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
14 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
15 " ", "!", "\"", "#", "$", "%", "&", "'",
16 "(", ")", "*", "+", ",", "-", ".", "/",
17 "0", "1", "2", "3", "4", "5", "6", "7",
18 "8", "9", ":", ";", "<", "=", ">", "?",
19 "@", "A", "B", "C", "D", "E", "F", "G",
20 "H", "I", "J", "K", "L", "M", "N", "O",
21 "P", "Q", "R", "S", "T", "U", "V", "W",
22 "X", "Y", "Z", "[", "\\", "]", "^", "_",
23 "`", "a", "b", "c", "d", "e", "f", "g",
24 "h", "i", "j", "k", "l", "m", "n", "o",
25 "p", "q", "r", "s", "t", "u", "v", "w",
26 "x", "y", "z", "{", "|", "}", "~", "DEL"] @=> string asciiTable[];
27
28 chout <= prompt;
29 chout.flush();
30
31 while( !done )
32 {
33 kb => now;
34
35 while( kb.more() && !done )
36 {
37 kb.getchar() => char;
38 asciiTable[char] => repr;
39
40 if( repr == "EOT" || repr == "LF" || repr == "CR" )
41 {
42 true => done;
43 }
44 else if( repr == "DEL" && Std.getenv("TERM") != "dumb")
45 {
46 if( input.length() > 0)
47 {
48 chout <= "\033[1D\033[0K";
49 chout.flush();
50 input.substring(0, input.length()-1) => input;
51 }
52 }
53 else
54 {
55 chout <= repr;
56 chout.flush();
57 repr +=> input;
58 }
59 }
60 }
61
62 chout <= "\n";
63
64 if( repr == "EOT" )
65 {
66 return null;
67 }
68
69 return input;
70 }
71 }
72