From b0767818e22ae9a39225000ae9c367e805609fbf Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 2 Mar 2023 15:21:34 -0700 Subject: [PATCH] can exit while reading from keyboard --- src/device/input/buffered.go | 4 ++-- src/device/input/input.go | 12 +++++++++--- src/device/input/input_exported_test.go | 7 ++++--- src/device/output/output.go | 3 ++- src/main.go | 5 +++-- testdata/keyboard_input.env | 2 ++ 6 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 testdata/keyboard_input.env diff --git a/src/device/input/buffered.go b/src/device/input/buffered.go index 7949841..c1416f1 100644 --- a/src/device/input/buffered.go +++ b/src/device/input/buffered.go @@ -16,8 +16,8 @@ type Buffered struct { expirationInterval time.Duration } -func NewBuffered(input Input) *Buffered { - ctx, can := context.WithCancel(context.Background()) +func NewBuffered(ctx context.Context, input Input) *Buffered { + ctx, can := context.WithCancel(ctx) result := &Buffered{ input: input, ctx: ctx, diff --git a/src/device/input/input.go b/src/device/input/input.go index 536c648..44e7111 100644 --- a/src/device/input/input.go +++ b/src/device/input/input.go @@ -1,20 +1,26 @@ package input -import "os" +import ( + "context" + "os" +) type Input interface { Read() []Button Close() } -func New() Input { +func New(ctx context.Context) Input { foo := randomCharFromRange('a', 'g') if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok { foo = randomCharFromWeightFile(p) } var result Input = NewRandom(foo) + if os.Getenv("INPUT_KEYBOARD") == "true" { + result = NewKeyboard() + } if os.Getenv("INPUT_BUFFERED") == "true" { - result = NewBuffered(result) + result = NewBuffered(ctx, result) } return result } diff --git a/src/device/input/input_exported_test.go b/src/device/input/input_exported_test.go index bae66ee..5264f1b 100644 --- a/src/device/input/input_exported_test.go +++ b/src/device/input/input_exported_test.go @@ -1,6 +1,7 @@ package input_test import ( + "context" "mayhem-party/src/device/input" "os" "path" @@ -8,7 +9,7 @@ import ( ) func TestNew(t *testing.T) { - r := input.New() + r := input.New(context.Background()) for i := 0; i < 15; i++ { batch := r.Read() for _, got := range batch { @@ -23,7 +24,7 @@ func TestNewBuffered(t *testing.T) { os.Unsetenv("INPUT_BUFFERED") }) - r := input.New() + r := input.New(context.Background()) for i := 0; i < 15; i++ { batch := r.Read() for j := range batch { @@ -44,7 +45,7 @@ func TestNewRandomWeightFile(t *testing.T) { os.Unsetenv("INPUT_RANDOM_WEIGHT_FILE") }) - r := input.New() + r := input.New(context.Background()) for i := 0; i < 15; i++ { batch := r.Read() for _, got := range batch { diff --git a/src/device/output/output.go b/src/device/output/output.go index 00cb1ac..acdbfd3 100644 --- a/src/device/output/output.go +++ b/src/device/output/output.go @@ -1,6 +1,7 @@ package output import ( + "context" "mayhem-party/src/device/output/key" "os" ) @@ -10,7 +11,7 @@ type Output interface { Press(...key.Key) } -func New() Output { +func New(ctx context.Context) Output { if os.Getenv("OUTPUT_KEYBOARD") == "true" { return NewKeyboard() } diff --git a/src/main.go b/src/main.go index 9aa498e..05a967f 100644 --- a/src/main.go +++ b/src/main.go @@ -10,9 +10,10 @@ import ( ) func Main(ctx context.Context) error { - reader := input.New() - writer := output.New() + reader := input.New(ctx) + writer := output.New(ctx) defer writer.Close() + defer reader.Close() interval := time.Millisecond * 50 if intervalS, ok := os.LookupEnv("MAIN_INTERVAL_DURATION"); !ok { diff --git a/testdata/keyboard_input.env b/testdata/keyboard_input.env new file mode 100644 index 0000000..069b56f --- /dev/null +++ b/testdata/keyboard_input.env @@ -0,0 +1,2 @@ +export INPUT_BUFFERED=true +export INPUT_KEYBOARD=true