Skip to content

Instantly share code, notes, and snippets.

@tlorens
Created October 30, 2017 20:43
Show Gist options
  • Save tlorens/1dcd9bef7965375106fa789d19e1cc5f to your computer and use it in GitHub Desktop.
Save tlorens/1dcd9bef7965375106fa789d19e1cc5f to your computer and use it in GitHub Desktop.
Attempt at fetching byte by byte without pressing return.
package shckio
import (
"bufio"
"log"
)
func GetCh(rw *bufio.ReadWriter) byte {
ch, _ := rw.ReadByte()
log.Println(string(ch))
return ch
}
func GetLine(rw *bufio.ReadWriter, maxLen int) string {
var tmp string
var ch byte
for ch != '\n' {
ch = GetCh(rw)
if (len(tmp) <= maxLen) {
tmp = tmp + string(ch)
}
}
return tmp
}
func Prompt(rw *bufio.ReadWriter, maxLen int, prompt string) string {
_, err := rw.WriteString(prompt)
if err != nil {
log.Println("Cannot write to connection.\n", err)
}
err = rw.Flush()
if err != nil {
log.Println("Flush failed.", err)
}
return GetLine(rw, maxLen)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment