Skip to content

Instantly share code, notes, and snippets.

@treeform
Created August 31, 2018 02:54
Show Gist options
  • Select an option

  • Save treeform/516726cae2b7ffbd7a0aaa757595ac65 to your computer and use it in GitHub Desktop.

Select an option

Save treeform/516726cae2b7ffbd7a0aaa757595ac65 to your computer and use it in GitHub Desktop.
import streams
when hostOS == "windows":
proc wgetch(): char {. header: "<conio.h>", importc: "getch" .}
proc wgetche(): char {. header: "<conio.h>", importc: "getche" .}
# stdout.write("\e[2J") # clear screen
const promt = "quicki >>> "
stdout.write(promt)
var line = ""
while true:
var input = wgetche()
# edit commands
if input == '\8':
if line.len > 0:
line.setLen(line.len - 1)
elif input == '\r':
stdout.write("\e[J") # clar everything after cursor
quit()
else:
line.add input
stdout.write("\e[J") # clar everything after cursor
stdout.write("\n")
for i, c in line:
stdout.write(c & " " & $ord(c) & " -- option [" & $i & "]\n")
# move up
for i in 0..line.len:
stdout.write("\e[1A")
# move left
for i in 0..(promt.len + line.len - 1):
stdout.write("\e[C")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment