can exit while reading from keyboard
parent
5c5a371f55
commit
b0767818e2
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
export INPUT_BUFFERED=true
|
||||
export INPUT_KEYBOARD=true
|
||||
Loading…
Reference in New Issue