test button.plaintext parser
parent
0e46f6e122
commit
7182ab387f
|
|
@ -0,0 +1,11 @@
|
||||||
|
package button_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mayhem-party/src/device/input/button"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParser(t *testing.T) {
|
||||||
|
var _ button.Parser = button.Plaintext{}
|
||||||
|
var _ button.Parser = button.V1{}
|
||||||
|
}
|
||||||
|
|
@ -7,25 +7,28 @@ import (
|
||||||
|
|
||||||
type Plaintext struct {
|
type Plaintext struct {
|
||||||
src raw.Raw
|
src raw.Raw
|
||||||
|
release byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlaintext(src raw.Raw) Plaintext {
|
func NewPlaintext(src raw.Raw) Plaintext {
|
||||||
return Plaintext{src: src}
|
releaseChar := byte('!')
|
||||||
|
if v := os.Getenv("BUTTON_PLAINTEXT_RELEASE"); v != "" {
|
||||||
|
releaseChar = byte(v[0])
|
||||||
|
}
|
||||||
|
return Plaintext{
|
||||||
|
src: src,
|
||||||
|
release: releaseChar,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Plaintext) Close() { p.src.Close() }
|
func (p Plaintext) Close() { p.src.Close() }
|
||||||
|
|
||||||
func (p Plaintext) Read() []Button {
|
func (p Plaintext) Read() []Button {
|
||||||
releaseChar := byte('!')
|
|
||||||
if v := os.Getenv("BUTTON_PLAINTEXT_RELEASE"); v != "" {
|
|
||||||
releaseChar = byte(v[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
b := p.src.Read()
|
b := p.src.Read()
|
||||||
buttons := make([]Button, 0, len(b))
|
buttons := make([]Button, 0, len(b))
|
||||||
down := true
|
down := true
|
||||||
for i := range b {
|
for i := range b {
|
||||||
if b[i] == releaseChar {
|
if b[i] == p.release {
|
||||||
down = false
|
down = false
|
||||||
} else {
|
} else {
|
||||||
if b[i] != '\n' {
|
if b[i] != '\n' {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
package button_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mayhem-party/src/device/input/button"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPlaintext(t *testing.T) {
|
||||||
|
src := constSrc("c!b")
|
||||||
|
p := button.NewPlaintext(src)
|
||||||
|
got := p.Read()
|
||||||
|
if len(got) != 2 {
|
||||||
|
t.Fatal(len(got))
|
||||||
|
}
|
||||||
|
if got[0] != (button.Button{Char: 'c', Down: true}) {
|
||||||
|
t.Error(got[0])
|
||||||
|
}
|
||||||
|
if got[1] != (button.Button{Char: 'b', Down: false}) {
|
||||||
|
t.Error(got[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type constSrc string
|
||||||
|
|
||||||
|
func (c constSrc) Close() {}
|
||||||
|
|
||||||
|
func (c constSrc) Read() []byte {
|
||||||
|
return []byte(c)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package button
|
||||||
|
|
||||||
|
import "mayhem-party/src/device/input/raw"
|
||||||
|
|
||||||
|
type V1 struct {
|
||||||
|
src raw.Raw
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewV1(src raw.Raw) V1 {
|
||||||
|
return V1{
|
||||||
|
src: src,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v1 V1) Close() { v1.src.Close() }
|
||||||
|
|
||||||
|
func (v1 V1) Read() []Button {
|
||||||
|
panic(nil)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue