diff options
-rw-r--r-- | samples/input.golem | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/samples/input.golem b/samples/input.golem new file mode 100644 index 0000000..6cdbb1d --- /dev/null +++ b/samples/input.golem @@ -0,0 +1,41 @@ +main() => input(">>> ") |> print(); + +define INPUT_BUF_SIZE = 256; +global input_buf[INPUT_BUF_SIZE] = 0; +input(prompt) { + local i = 0; + local c; + print(prompt); + loop { + if (i >= INPUT_BUF_SIZE) + return input_buf; + red &c; + if (c == 0x7f) { + // backspace + if (i > 0) { + i = i - 1; + [input_buf + i] = 0; + wrt '\r'; + print(prompt); + print(input_buf); + wrt ' '; + } + } else + [input_buf + i++] = c; + wrt '\r'; + print(prompt); + print(input_buf); + if (c == '\n') + return input_buf; + } + return input_buf; +} + +print(s) { + loop { + if ([s] == 0) + return; + wrt [s]; + s = s + 1; + } +} |