summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-23 20:59:12 +0200
committerkdx <kikoodx@paranoici.org>2023-07-23 20:59:12 +0200
commitcb630df69c56d5179873c85bef1fef99cd5fe48d (patch)
tree3cf0af7fa12fd77a579a94fef5e75ff0504fc259
parent6d44bcf1b9cdd2ad7eb9bf9bafea4df34d122c98 (diff)
downloadgolem-cb630df69c56d5179873c85bef1fef99cd5fe48d.tar.gz
line input sample
-rw-r--r--samples/input.golem41
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;
+ }
+}