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

Allow overriding the Reline encoding #755

Closed
sjanusz-r7 opened this issue Oct 4, 2024 · 3 comments · Fixed by #765
Closed

Allow overriding the Reline encoding #755

sjanusz-r7 opened this issue Oct 4, 2024 · 3 comments · Fixed by #765
Labels
enhancement New feature or request

Comments

@sjanusz-r7
Copy link

Description

Currently the Reline library uses the Ruby's default encoding, but this is not configurable:

def encoding
Encoding.default_external
end

It would be great to configure this value, i.e. in the scenario of having the encoding set to ASCII-8BIT globally - but wanting Reline to use UTF-8 encoding:

  ::Encoding.default_external = ::Encoding::ASCII_8BIT
  ::Encoding.default_internal = ::Encoding::ASCII_8BIT

  # Workaround to fix encoding crash:
  # class ::Reline::ANSI
  #   def encoding
  #     ::Encoding::UTF_8
  #   end
  # end

  ascii_text = Reline.readline('prompt > ', false)

  $stderr.puts ascii_text.encoding.to_s

Example

Pasting in the characters こんにちは

prompt > こんにちは

Current Behaviour

Crash: gems/reline-0.5.10/lib/reline/unicode.rb:112:in 'encode': "\\xE3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)

Desired Behaviour

Being able to configure the value on Reline:

  def encoding
    @encoding || Encoding.default_external
  end

Terminal Emulator

OSX

Version

0.5.10

@tompng
Copy link
Member

tompng commented Oct 4, 2024

The crash is fixed in #751

prompt > ???????????????
ASCII-8BIT

When Encoding.default_external is set to ascii-8bit, Reline thinks terminal emulator's encoding is ascii-8bit. Any bytes (not restricted to utf-8) could come to STDIN and all Reline should encode output to ascii-8bit before printing them to STDOUT, but "\x7f".."\xff" are not printable character.
If the assumption (terminal emulator's encoding is ascii-8bit) is wrong, I think changing Encoding.default_external is the right way.

If your request is to have an ascii-8bit mode like this, please open another issue.

ruby -Eascii-8bit
Pasting in the characters あ
prompt > \xE3\x81\x82
Delete key will delete single byte
prompt > \xE3\x81

@tompng tompng closed this as completed Oct 4, 2024
@sjanusz-r7
Copy link
Author

Hello! Thanks for getting back to us.

For context, for our project we set the default_encoding to be ASCII-8BIT as we deal with a lot of binary data blobs by default - and rarely UTF-8. However we want to configure Reline separately to use UTF-8.

For our scenario it would be great if Reline would either use the IO object's encoding or would allow for separate configuration to UTF-8, maybe something like:

Reline.encoding = Encoding::UTF_8

If the assumption (terminal emulator's encoding is ascii-8bit) is wrong, I think changing Encoding.default_external is the right way.

Hopefully the above context clarifies why we can't globally change Encoding.default_external to UTF-8 to use Reline, as that would break our other network protocol handling in our project

@tompng
Copy link
Member

tompng commented Oct 7, 2024

Thank you for the detailed scenario

Reline would either use the IO object's encoding or ...

The idea using IO object's encoding looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

2 participants