From cb630df69c56d5179873c85bef1fef99cd5fe48d Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 23 Jul 2023 20:59:12 +0200 Subject: line input sample --- samples/input.golem | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 samples/input.golem 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; + } +} -- cgit v1.2.3