Last active
October 21, 2019 01:55
-
-
Save danhab99/681abc438559257f73a89cd7c62571c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import ( | |
"bufio" | |
"os" | |
) | |
func chanNBRead(c chan bool, def bool) bool { | |
var e bool | |
select { | |
case e = <-c: | |
default: | |
e = true | |
} | |
return e | |
} | |
// Keypress : Starts a keypress loop | |
func Keypress() (chan byte, chan bool, chan bool) { | |
abort := make(chan bool) | |
out := make(chan byte, 1) | |
enable := make(chan bool) | |
scanner := bufio.NewScanner(os.Stdin) | |
go func() { | |
for chanNBRead(abort, false) && scanner.Scan() { | |
if scanner.Err() != nil { | |
abort <- true | |
} | |
t := scanner.Bytes() | |
for _, b := range t { | |
if chanNBRead(enable, true) { | |
out <- b | |
} | |
} | |
} | |
}() | |
e := scanner.Err() != nil | |
abort <- e | |
return out, abort, enable | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment