diff options
author | Julien Dessaux | 2021-09-23 16:14:37 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-09-23 17:49:44 +0200 |
commit | 198efceb1f14c9822bf87ede6961d0f94a4760db (patch) | |
tree | 5391e03e81979b99380cf2794f8e338234a34b04 /pkg/pointer/intput-output.go | |
parent | Moved the character execution's to the pointer in order to handle the k command (diff) | |
download | gofunge98-198efceb1f14c9822bf87ede6961d0f94a4760db.tar.gz gofunge98-198efceb1f14c9822bf87ede6961d0f94a4760db.tar.bz2 gofunge98-198efceb1f14c9822bf87ede6961d0f94a4760db.zip |
Implemented commands until helloworld works \o/
Diffstat (limited to 'pkg/pointer/intput-output.go')
-rw-r--r-- | pkg/pointer/intput-output.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/pkg/pointer/intput-output.go b/pkg/pointer/intput-output.go new file mode 100644 index 0000000..cc7533e --- /dev/null +++ b/pkg/pointer/intput-output.go @@ -0,0 +1,61 @@ +package pointer + +import ( + "fmt" + "log" + "os" + + "github.com/pkg/term" +) + +var defaultInputLastChar *int = nil + +func DefaultCharacterInput() int { + if defaultInputLastChar != nil { + c := *defaultInputLastChar + defaultInputLastChar = nil + return c + } + t, err := term.Open("/dev/stdin") + if err != nil { + log.Fatalf("Could not open stdin: %+v", err) + } + defer t.Close() + defer t.Restore() + term.RawMode(t) + b := make([]byte, 1) + i, err := os.Stdin.Read(b) + if err != nil { + log.Fatalf("Error in DefaultCharacterInput { b: %c, i: %d, err: %+v }", b[0], i, err) + } + return int(b[0]) +} + +func DefaultDecimalInput() int { + var v int + for { + c := DefaultCharacterInput() + if c >= '0' && c <= '9' { + v = c - '0' + break + } + } + for { + c := DefaultCharacterInput() + if c >= '0' && c <= '9' { + v = v*10 + c - '0' + } else { + defaultInputLastChar = &c + break + } + } + return v +} + +func DefaultCharacterOutput(c int) { + fmt.Printf("%c", c) +} + +func DefaultDecimalOutput(c int) { + fmt.Printf("%d ", c) +} |