Skip to content

Instantly share code, notes, and snippets.

@foldfelis
Last active July 21, 2020 09:16
Show Gist options
  • Save foldfelis/375dc13b2d3be792fdf029466d7761d0 to your computer and use it in GitHub Desktop.
Save foldfelis/375dc13b2d3be792fdf029466d7761d0 to your computer and use it in GitHub Desktop.
Read keyboard key.
using REPL
# +-------+
# | Utils |
# +-------+
read_next_byte(io::IO) = read(io, 1)[1]
function read_stream_bytes(stream::IO)
queue = UInt8[]
push!(queue, read_next_byte(stream))
stream_size = stream.buffer.size
if stream_size > 1
for i=1:stream_size-1
push!(queue, read_next_byte(stream))
end
end
return queue
end
read_stream(stream::IO) = String(read_stream_bytes(stream))
# +-----------+
# | Main loop |
# +-----------+
function run_app(
t=REPL.Terminals.TTYTerminal(
get(ENV, "TERM", Sys.iswindows() ? "" : "dumb"),
stdin, stdout, stderr
)
)
REPL.Terminals.raw!(t, true)
is_running = true
while is_running
sequence = read_stream(t.in_stream)
println(sequence)
(sequence == "\e") && (println("Shutting down"); is_running = false)
end
REPL.Terminals.raw!(t, false)
end
run_app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment