-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: getKey vs getKeys #48
Comments
while true:
for key in getKeys():
case key
of Key.None: discard
of Key.Escape, Key.Q: exitProc()
else:
tb.write(8, 4, ' '.repeat(31))
tb.write(2, 4, resetStyle, "Key pressed: ", fgGreen, $key)
tb.display()
sleep(20) |
Not sure if display should be in the inner while loop or not while true:
var key = getKey()
while key != Key.None:
case key
of Key.None: discard
of Key.Escape, Key.Q: exitProc()
else:
tb.write(8, 4, ' '.repeat(31))
tb.write(2, 4, resetStyle, "Key pressed: ", fgGreen, $key)
key = getKey()
tb.display()
sleep(20)
|
Yeah, so this is how I want the API to look like, |
Don't you think the double while loop looks not obvious and even ugly? Also, it is just iterator over while simulation. |
I want to have a minimal API surface. If you really want Also, you don't need a double while loop if you don't particularly care about 20ms delay between processing keypresses now and then. Or you can do 5 or 10ms sleep. Again, up to the client code how to deal with this, the minimum we need is |
The only good, in my opinion, solution to avoid the cpu usage and to avoid sleep without any delays is: while true:
let key = getKeyWithTimeout(1000)
case key
of Key.None:
buf.add '.'
of Key.Escape, Key.Q: exitProc()
else:
buf.add $key
if buf.len > 20:
buf = buf[^20..^1]
tb.write(8, 4, ' '.repeat(31))
tb.write(2, 4, resetStyle, "Key pressed: ", fgGreen, buf)
tb.display() |
Thanks for the examples @inv2004, but I've said all I wanted on the matter. I only want Thank you. |
Actually @inv2004, thought about it a bit more and |
I am working on getKeyWithTimeout When it is ready - getKeys can be rolled back The main challenge is to avoid any addition buffers and to make parser for long sequences works directly on stdin next char |
@inv2004 Please make sure that your async solution works not only in Linux terminals but also on Windows in |
Done here #47
BTW: win api looks 10 times better than the stdin escapes -- added -- |
Here is an iterator for someone who wants pretty monotonic ticks without sleep: iterator keyEachSec(): Key =
var timeout = 1000
while true:
let a = getMonoTime().ticks
let k = getKeyWithTimeout(timeout)
if k == Key.None:
timeout = 1000
else:
let b = int((getMonoTime().ticks - a) div 1000000)
timeout = timeout - (b mod 1000)
yield k |
Initial example in readme:
The text was updated successfully, but these errors were encountered: