Skip to content

Commit

Permalink
windows: keyToString(): fix string conversion (for real)
Browse files Browse the repository at this point in the history
I must've been sleeping while writing 2ce69ef,
because while it "fixed" the error, the intent here obviously is to return the
actual character, not the number ':-)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Apr 30, 2023
1 parent 0564e01 commit fd55730
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions windows/ansi_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"
"unsafe"

Expand Down Expand Up @@ -196,10 +195,10 @@ func keyToString(keyEvent *winterm.KEY_EVENT_RECORD, escapeSequence []byte) stri

// <Alt>+Key generates ESC N Key
if !control && alt {
return ansiterm.KEY_ESC_N + strings.ToLower(strconv.Itoa(int(keyEvent.UnicodeChar)))
return ansiterm.KEY_ESC_N + strings.ToLower(string(rune(keyEvent.UnicodeChar)))
}

return strconv.Itoa(int(keyEvent.UnicodeChar))
return string(rune(keyEvent.UnicodeChar))
}

// formatVirtualKey converts a virtual key (e.g., up arrow) into the appropriate ANSI string.
Expand Down
22 changes: 22 additions & 0 deletions windows/ansi_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package windowsconsole

import (
"testing"

"github.com/Azure/go-ansiterm"
"github.com/Azure/go-ansiterm/winterm"
)

func TestKeyToString(t *testing.T) {
ke := &winterm.KEY_EVENT_RECORD{
KeyDown: 1,
ControlKeyState: winterm.LEFT_ALT_PRESSED,
UnicodeChar: 65, // capital A
}

const expected = ansiterm.KEY_ESC_N + "a"
out := keyToString(ke, nil)
if out != expected {
t.Errorf("expected %s, got %s", expected, out)
}
}

0 comments on commit fd55730

Please sign in to comment.