split wrap protocol parsing into input.button

This commit is contained in:
bel
2023-03-24 20:25:15 -06:00
parent 9990273b19
commit b319ed7e6d
8 changed files with 72 additions and 27 deletions

View File

@@ -0,0 +1,38 @@
package button
import (
"mayhem-party/src/device/input/raw"
"os"
)
type Plaintext struct {
src raw.Raw
}
func NewPlaintext(src raw.Raw) Plaintext {
return Plaintext{src: src}
}
func (p Plaintext) Close() { p.src.Close() }
func (p Plaintext) Read() []Button {
releaseChar := byte('!')
if v := os.Getenv("BUTTON_PLAINTEXT_RELEASE"); v != "" {
releaseChar = byte(v[0])
}
b := p.src.Read()
buttons := make([]Button, 0, len(b))
down := true
for i := range b {
if b[i] == releaseChar {
down = false
} else {
if b[i] != '\n' {
buttons = append(buttons, Button{Char: b[i], Down: down})
}
down = true
}
}
return buttons
}