Skip to content

Commit

Permalink
Merge pull request #42 from thaJeztah/fix_windows_conversion_again
Browse files Browse the repository at this point in the history
windows: keyToString(): fix string conversion (for real)
  • Loading branch information
thaJeztah committed May 1, 2023
2 parents 0564e01 + abe3d01 commit 2ff7073
Show file tree
Hide file tree
Showing 2 changed files with 26 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
24 changes: 24 additions & 0 deletions windows/ansi_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build windows
// +build windows

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{
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 2ff7073

Please sign in to comment.