Created
October 30, 2017 20:43
-
-
Save tlorens/1dcd9bef7965375106fa789d19e1cc5f to your computer and use it in GitHub Desktop.
Attempt at fetching byte by byte without pressing return.
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 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