split wrap protocol parsing into input.button
This commit is contained in:
38
src/device/input/button/plaintext.go
Normal file
38
src/device/input/button/plaintext.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user