From c4c1f11f2a2afa506f9df0047e4d77074072f17b Mon Sep 17 00:00:00 2001 From: bel Date: Wed, 1 Mar 2023 22:23:31 -0700 Subject: [PATCH] output.Output and output.Writer --- src/device/output/output.go | 6 ++++++ src/device/output/output_test.go | 8 ++++++++ src/device/output/writer.go | 24 ++++++++++++++++++++++++ src/device/output/writer_test.go | 14 ++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 src/device/output/output.go create mode 100644 src/device/output/output_test.go create mode 100644 src/device/output/writer.go create mode 100644 src/device/output/writer_test.go diff --git a/src/device/output/output.go b/src/device/output/output.go new file mode 100644 index 0000000..2093ad6 --- /dev/null +++ b/src/device/output/output.go @@ -0,0 +1,6 @@ +package output + +type Output interface { + Close() + Press(...int) +} diff --git a/src/device/output/output_test.go b/src/device/output/output_test.go new file mode 100644 index 0000000..4496275 --- /dev/null +++ b/src/device/output/output_test.go @@ -0,0 +1,8 @@ +package output + +import "testing" + +func TestOutput(t *testing.T) { + var _ Output = &Keyboard{} + var _ Output = &Writer{} +} diff --git a/src/device/output/writer.go b/src/device/output/writer.go new file mode 100644 index 0000000..7c9cf1d --- /dev/null +++ b/src/device/output/writer.go @@ -0,0 +1,24 @@ +package output + +import ( + "fmt" + "io" +) + +type Writer struct { + w io.Writer +} + +func NewWriter(w io.Writer) Writer { + return Writer{w: w} +} + +func (w Writer) Close() { + if wc, ok := w.w.(io.WriteCloser); ok { + wc.Close() + } +} + +func (w Writer) Press(keys ...int) { + fmt.Fprintf(w.w, "%+v\n", keys) +} diff --git a/src/device/output/writer_test.go b/src/device/output/writer_test.go new file mode 100644 index 0000000..9060a26 --- /dev/null +++ b/src/device/output/writer_test.go @@ -0,0 +1,14 @@ +package output_test + +import ( + "bytes" + "mayhem-party/src/device/output" + "testing" +) + +func TestWriter(t *testing.T) { + b := bytes.NewBuffer(nil) + w := output.NewWriter(b) + w.Press(1, 2, 3, 1) + t.Logf("%s", b.Bytes()) +}