can exit while reading from keyboard

master
Bel LaPointe 2023-03-02 15:21:34 -07:00
parent 5c5a371f55
commit b0767818e2
6 changed files with 22 additions and 11 deletions

View File

@ -16,8 +16,8 @@ type Buffered struct {
expirationInterval time.Duration expirationInterval time.Duration
} }
func NewBuffered(input Input) *Buffered { func NewBuffered(ctx context.Context, input Input) *Buffered {
ctx, can := context.WithCancel(context.Background()) ctx, can := context.WithCancel(ctx)
result := &Buffered{ result := &Buffered{
input: input, input: input,
ctx: ctx, ctx: ctx,

View File

@ -1,20 +1,26 @@
package input package input
import "os" import (
"context"
"os"
)
type Input interface { type Input interface {
Read() []Button Read() []Button
Close() Close()
} }
func New() Input { func New(ctx context.Context) Input {
foo := randomCharFromRange('a', 'g') foo := randomCharFromRange('a', 'g')
if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok { if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok {
foo = randomCharFromWeightFile(p) foo = randomCharFromWeightFile(p)
} }
var result Input = NewRandom(foo) var result Input = NewRandom(foo)
if os.Getenv("INPUT_KEYBOARD") == "true" {
result = NewKeyboard()
}
if os.Getenv("INPUT_BUFFERED") == "true" { if os.Getenv("INPUT_BUFFERED") == "true" {
result = NewBuffered(result) result = NewBuffered(ctx, result)
} }
return result return result
} }

View File

@ -1,6 +1,7 @@
package input_test package input_test
import ( import (
"context"
"mayhem-party/src/device/input" "mayhem-party/src/device/input"
"os" "os"
"path" "path"
@ -8,7 +9,7 @@ import (
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
r := input.New() r := input.New(context.Background())
for i := 0; i < 15; i++ { for i := 0; i < 15; i++ {
batch := r.Read() batch := r.Read()
for _, got := range batch { for _, got := range batch {
@ -23,7 +24,7 @@ func TestNewBuffered(t *testing.T) {
os.Unsetenv("INPUT_BUFFERED") os.Unsetenv("INPUT_BUFFERED")
}) })
r := input.New() r := input.New(context.Background())
for i := 0; i < 15; i++ { for i := 0; i < 15; i++ {
batch := r.Read() batch := r.Read()
for j := range batch { for j := range batch {
@ -44,7 +45,7 @@ func TestNewRandomWeightFile(t *testing.T) {
os.Unsetenv("INPUT_RANDOM_WEIGHT_FILE") os.Unsetenv("INPUT_RANDOM_WEIGHT_FILE")
}) })
r := input.New() r := input.New(context.Background())
for i := 0; i < 15; i++ { for i := 0; i < 15; i++ {
batch := r.Read() batch := r.Read()
for _, got := range batch { for _, got := range batch {

View File

@ -1,6 +1,7 @@
package output package output
import ( import (
"context"
"mayhem-party/src/device/output/key" "mayhem-party/src/device/output/key"
"os" "os"
) )
@ -10,7 +11,7 @@ type Output interface {
Press(...key.Key) Press(...key.Key)
} }
func New() Output { func New(ctx context.Context) Output {
if os.Getenv("OUTPUT_KEYBOARD") == "true" { if os.Getenv("OUTPUT_KEYBOARD") == "true" {
return NewKeyboard() return NewKeyboard()
} }

View File

@ -10,9 +10,10 @@ import (
) )
func Main(ctx context.Context) error { func Main(ctx context.Context) error {
reader := input.New() reader := input.New(ctx)
writer := output.New() writer := output.New(ctx)
defer writer.Close() defer writer.Close()
defer reader.Close()
interval := time.Millisecond * 50 interval := time.Millisecond * 50
if intervalS, ok := os.LookupEnv("MAIN_INTERVAL_DURATION"); !ok { if intervalS, ok := os.LookupEnv("MAIN_INTERVAL_DURATION"); !ok {

2
testdata/keyboard_input.env vendored Normal file
View File

@ -0,0 +1,2 @@
export INPUT_BUFFERED=true
export INPUT_KEYBOARD=true