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

Use IO's encoding instead of Encoding.default_external #765

Merged
merged 4 commits into from
Oct 22, 2024

Conversation

monkeyWzr
Copy link
Contributor

Fix #755. Use IO object's encoding to separate from global Encoding.default_external.

For example:

require 'reline'
::Encoding.default_external = ::Encoding::ASCII_8BIT
::Encoding.default_internal = ::Encoding::ASCII_8BIT

input = STDIN.dup
input.set_encoding(Encoding::UTF_8)
Reline.input = input

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

$stderr.puts ascii_text.encoding.to_s

and get UTF-8:

prompt > こんにちは
UTF-8

@@ -41,7 +41,7 @@ def initialize
end

def encoding
Encoding.default_external
@input.external_encoding || Encoding.default_external
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fails one of irb's test. Should be fixed by ruby/irb#1022

@monkeyWzr monkeyWzr marked this pull request as ready for review October 19, 2024 16:57
@@ -138,7 +142,7 @@ def set_pasting_state(in_pasting)

def reset(prompt = '', encoding:)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter encoding: can be also removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💦 Thanks! Removed!

@monkeyWzr
Copy link
Contributor Author

FYI, one of irb's integration test seems to randomly fail.

https://github.com/ruby/reline/actions/runs/11424348590/job/31784575039

===============================================================================
Failure: test_ctrl_c_is_handled(IRB::RenderingTest::TestVTerm)
/home/runner/.rubies/ruby-head/lib/ruby/gems/3.4.0+0/bundler/gems/yamatanooroti-3937fa311781/lib/yamatanooroti/vterm.rb:155:in 'block in Yamatanooroti::VTermTestCaseModule#assert_screen'
/home/runner/.rubies/ruby-head/lib/ruby/gems/3.4.0+0/bundler/gems/yamatanooroti-3937fa311781/lib/yamatanooroti/vterm.rb:141:in 'Yamatanooroti::VTermTestCaseModule#retryable_screen_assertion_with_proc'
/home/runner/.rubies/ruby-head/lib/ruby/gems/3.4.0+0/bundler/gems/yamatanooroti-3937fa311781/lib/yamatanooroti/vterm.rb:153:in 'Yamatanooroti::VTermTestCaseModule#assert_screen'
/home/runner/work/reline/reline/irb/test/irb/yamatanooroti/test_rendering.rb:316:in 'IRB::RenderingTest::TestVTerm#test_ctrl_c_is_handled'
<"irb(main):001>\n" + "^C\n" + "irb(main):001>\n"> expected but was
<"irb(main):001> ^C\n" + "irb(main):001>\n">

diff:
+ irb(main):001> ^C
  irb(main):001>
- ^C
- irb(main):001>
===============================================================================

@tompng tompng added the enhancement New feature or request label Oct 20, 2024
@tompng
Copy link
Member

tompng commented Oct 22, 2024

randomly fail.

I think it is not related to this change.
Maybe it is because Reline prints prompt before set_signal_handlers. IRB's test is sending ctrl-c too early (need a sleep in test), or Reline can print prompt after set_signal_handlers.
I'll check it again if it fails often.

Copy link
Member

@tompng tompng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you 👍
STDIN.gets returns string with STDIN's encoding. Reline now aligns with it.

@tompng tompng merged commit f09772a into ruby:master Oct 22, 2024
40 checks passed
matzbot pushed a commit to ruby/ruby that referenced this pull request Oct 22, 2024
(ruby/reline#765)

* use IO's encoding

* refactoring

* remove unused encoding params

* (for retriggering CI) remove unused encoding params

ruby/reline@f09772adab
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 this pull request may close these issues.

Allow overriding the Reline encoding
2 participants