Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with UTF8 terminal #25

Open
giorgiofran opened this issue Jul 5, 2020 · 0 comments
Open

Problem with UTF8 terminal #25

giorgiofran opened this issue Jul 5, 2020 · 0 comments

Comments

@giorgiofran
Copy link

I was having problems with extended chars in a linux terminal.
When I typed "è" I got "Ã".
Running the rawkeys example I got the following output:
c3 (Ã)
a8 (¨)

Giving a look at the Key.readKey function I saw this comment:

   ///  ... , certain keys may
  /// be represented by multiple control key sequences. An example showing
  /// basic key handling can be found in the `example/command_line.dart`
  /// file in the package source code.

But I could not find the example "command_line"

So I did the following:
I copied the readKey function into an extension function and I did the following changes:
Original:

 Key readKey() {
    Key key;
    int charCode;
    int codeUnit = 0;

    rawMode = true;
    while (codeUnit <= 0) {
      codeUnit = stdin.readByteSync();
    }

    if (codeUnit >= 0x01 && codeUnit <= 0x1a) {

....

Modified:

  Key readSystemKey() {
    Key key;
    int charCode;
    var codeUnit;
    var bytes = <int>[];

    rawMode = true;
    while (key == null) {
      codeUnit = 0;
      while (codeUnit <= 0) {
        codeUnit = stdin.readByteSync();
      }

      if (codeUnit >= 0x01 && codeUnit <= 0x1a) {
...

Original end of method:

    } else {
      // assume other characters are printable
      key = Key.printable(String.fromCharCode(codeUnit));
    }
    rawMode = false;
    return key;
  }

Modified end of method:

      } else {
        // assume other characters are printable
        try {
          bytes.add(codeUnit);
          key = Key.printable(systemEncoding.decode(bytes));
          bytes.clear();
        } on FormatException catch (_) {
          if (bytes.length > 4) {
            rethrow;
          }
        }
      }
    }
    rawMode = false;
    return key;
  }

It works for my needs, but probably this logic should be added in other points of the package and tested with different OSs.
Another option would be to let the user set a specific encoding, if needed.

Let me know what do you think about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant