From 74e54c4efa9373cf5a03dda0c6de06430faa4275 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 2 Mar 2023 15:33:52 -0700 Subject: [PATCH] support $INPUT_REMAP_FILE --- src/device/input/input.go | 5 ++- src/device/input/input_exported_test.go | 31 +++++++++++++++ src/device/input/input_test.go | 1 + src/device/input/remap.go | 52 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/device/input/remap.go diff --git a/src/device/input/input.go b/src/device/input/input.go index 44e7111..bd4834a 100644 --- a/src/device/input/input.go +++ b/src/device/input/input.go @@ -12,7 +12,7 @@ type Input interface { func New(ctx context.Context) Input { foo := randomCharFromRange('a', 'g') - if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok { + if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok && len(p) > 0 { foo = randomCharFromWeightFile(p) } var result Input = NewRandom(foo) @@ -22,5 +22,8 @@ func New(ctx context.Context) Input { if os.Getenv("INPUT_BUFFERED") == "true" { result = NewBuffered(ctx, result) } + if p, ok := os.LookupEnv("INPUT_REMAP_FILE"); ok && len(p) > 0 { + result = NewRemapFromFile(result, p) + } return result } diff --git a/src/device/input/input_exported_test.go b/src/device/input/input_exported_test.go index 5264f1b..27a1fc9 100644 --- a/src/device/input/input_exported_test.go +++ b/src/device/input/input_exported_test.go @@ -18,6 +18,37 @@ func TestNew(t *testing.T) { } } +func TestNewRemapped(t *testing.T) { + d := t.TempDir() + remap := path.Join(d, "remap") + if err := os.WriteFile(remap, []byte(`{"a":"b"}`), os.ModePerm); err != nil { + t.Fatal(err) + } + + rand := path.Join(d, "rand") + if err := os.WriteFile(rand, []byte(`{"a":1}`), os.ModePerm); err != nil { + t.Fatal(err) + } + + os.Setenv("INPUT_REMAP_FILE", remap) + os.Setenv("INPUT_RANDOM_WEIGHT_FILE", rand) + t.Cleanup(func() { + os.Unsetenv("INPUT_REMAP_FILE") + os.Unsetenv("INPUT_RANDOM_WEIGHT_FILE") + }) + + r := input.New(context.Background()) + for i := 0; i < 15; i++ { + batch := r.Read() + for j := range batch { + if batch[j].Char != 'b' { + t.Errorf("%c", batch[j].Char) + } + t.Logf("[%d][%d] %c|%v", i, j, batch[j].Char, batch[j].Down) + } + } +} + func TestNewBuffered(t *testing.T) { os.Setenv("INPUT_BUFFERED", "true") t.Cleanup(func() { diff --git a/src/device/input/input_test.go b/src/device/input/input_test.go index ff32d7f..484c464 100644 --- a/src/device/input/input_test.go +++ b/src/device/input/input_test.go @@ -6,4 +6,5 @@ func TestInput(t *testing.T) { var _ Input = &Random{} var _ Input = Keyboard{} var _ Input = &Buffered{} + var _ Input = Remap{} } diff --git a/src/device/input/remap.go b/src/device/input/remap.go new file mode 100644 index 0000000..3eb777f --- /dev/null +++ b/src/device/input/remap.go @@ -0,0 +1,52 @@ +package input + +import ( + "os" + + "github.com/go-yaml/yaml" +) + +type Remap struct { + input Input + m map[byte]byte +} + +func NewRemapFromFile(input Input, p string) Remap { + b, err := os.ReadFile(p) + if err != nil { + panic(err) + } + var m map[string]string + if err := yaml.Unmarshal(b, &m); err != nil { + panic(err) + } + remap := map[byte]byte{} + for k, v := range m { + if len(k) != 1 || len(v) != 1 { + panic("remap must be char:char") + } + remap[k[0]] = v[0] + } + return NewRemap(input, remap) +} + +func NewRemap(input Input, m map[byte]byte) Remap { + return Remap{ + input: input, + m: m, + } +} + +func (re Remap) Close() { + re.input.Close() +} + +func (re Remap) Read() []Button { + result := re.input.Read() + for i := range result { + if v, ok := re.m[result[i].Char]; ok { + result[i].Char = v + } + } + return result +}