From 46825a7462e3106599c034bd77dcd3646a181539 Mon Sep 17 00:00:00 2001 From: aycabta Date: Fri, 26 Feb 2021 10:32:34 +0900 Subject: Backport lib/reline, and lib/irb for 3.0.1 3rd (#4228) * [ruby/irb] Suppress error when File::ALT_SEPARATOR is nil https://github.com/ruby/irb/commit/96accf3b95 * [ruby/irb] Suppress colorize on Windows tests https://github.com/ruby/irb/commit/5be9354cf9 * [ruby/irb] The command "irb_info" should show RUBY_PLATFORM https://github.com/ruby/irb/commit/39d1cd874f * [ruby/irb] Fix inverse separator condition https://github.com/ruby/irb/commit/33f933196f * [ruby/reline] Return 1 when char width not found This fixes ruby/reline#261. https://github.com/ruby/reline/commit/3cf1213014 * [ruby/reline] Avoid tripping over nil prompt https://github.com/ruby/reline/commit/d4d9d3e3d4 * [ruby/reline] Move script files for yamatanooroti tests https://github.com/ruby/reline/commit/03031b885d * [ruby/reline] Add a test for suppressing crash when dynamic prompt returns empty ref. https://github.com/ruby/reline/pull/262 https://github.com/ruby/reline/commit/b98bc3c329 * [ruby/reline] We still need support new and legacy behavior. Revert "Support for change in Windows-specific behavior at eol" This reverts commit cad4de6ee841b43f3f0e441626f9415c3eda0f82. https://github.com/ruby/reline/commit/646587fc2c * [ruby/reline] check ENABLE_VIRTUAL_TERMINAL_PROCESSING flag and switch eof processing https://github.com/ruby/reline/commit/3535676689 * [ruby/reline] Use UTF-8 only for width calc, rest uses original encoding I confirmed that libvterm supports only which are UTF-8, US ASCII, UK, and DEC graphics by reading source code, so can't test this patch by yamatanoorogi gem through vterm gem for now. This fixes ruby/irb#190. https://github.com/ruby/reline/commit/44596c0cc7 * [ruby/reline] fix Reline::Windows.getconsolemode buffer use double quotes to properly convert the \000 escape sequence. https://github.com/ruby/reline/commit/236dfe5683 * [ruby/irb] Version 1.3.4 https://github.com/ruby/irb/commit/ab9852ccc5 * [ruby/reline] Version 0.2.4 https://github.com/ruby/reline/commit/462f971bd3 Co-authored-by: Nobuyoshi Nakada Co-authored-by: Eamonn Webster Co-authored-by: Yoshinao Muramatu Co-authored-by: cremno --- lib/irb/cmd/info.rb | 1 + lib/irb/ext/loader.rb | 4 ++-- lib/irb/version.rb | 4 ++-- lib/irb/workspace.rb | 2 +- lib/reline/line_editor.rb | 28 +++++++++++++++++++--------- lib/reline/unicode.rb | 1 + lib/reline/version.rb | 2 +- lib/reline/windows.rb | 24 ++++++++++++++++++++++++ 8 files changed, 51 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/irb/cmd/info.rb b/lib/irb/cmd/info.rb index 53ec71d754..d122c88b77 100644 --- a/lib/irb/cmd/info.rb +++ b/lib/irb/cmd/info.rb @@ -13,6 +13,7 @@ module IRB str += "IRB version: #{IRB.version}\n" str += "InputMethod: #{IRB.CurrentContext.io.inspect}\n" str += ".irbrc path: #{IRB.rc_file}\n" if File.exist?(IRB.rc_file) + str += "RUBY_PLATFORM: #{RUBY_PLATFORM}\n" str end alias_method :to_s, :inspect diff --git a/lib/irb/ext/loader.rb b/lib/irb/ext/loader.rb index 90dcd70bd0..af028996e7 100644 --- a/lib/irb/ext/loader.rb +++ b/lib/irb/ext/loader.rb @@ -38,9 +38,9 @@ module IRB # :nodoc: else separator = if File::ALT_SEPARATOR - File::SEPARATOR - else "[#{Regexp.quote(File::SEPARATOR + File::ALT_SEPARATOR)}]" + else + File::SEPARATOR end ABSOLUTE_PATH_PATTERN = # :nodoc: case Dir.pwd diff --git a/lib/irb/version.rb b/lib/irb/version.rb index a715293b34..0a4a1bb922 100644 --- a/lib/irb/version.rb +++ b/lib/irb/version.rb @@ -11,7 +11,7 @@ # module IRB # :nodoc: - VERSION = "1.3.3" + VERSION = "1.3.4" @RELEASE_VERSION = VERSION - @LAST_UPDATE_DATE = "2021-02-07" + @LAST_UPDATE_DATE = "2021-02-25" end diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index c6c328e7b5..78d434d106 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -175,7 +175,7 @@ EOF body = (start_pos..end_pos).map do |current_pos| sprintf(fmt, pos == current_pos ? '=>' : '', current_pos + 1, lines[current_pos]) end.join("") - "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear}\n" + "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear if use_colorize}\n" end def IRB.delete_caller diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 557b5aa737..12a2bde234 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -124,6 +124,7 @@ class Reline::LineEditor @prompt_cache_time = Time.now.to_f end prompt_list.map!{ prompt } if @vi_arg or @searching_prompt + prompt_list = [prompt] if prompt_list.empty? mode_string = check_mode_string prompt_list = prompt_list.map{ |pr| mode_string + pr } if mode_string prompt = prompt_list[@line_index] @@ -343,8 +344,9 @@ class Reline::LineEditor else end_of_line_cursor = new_cursor_max end - line_to_calc.encode(Encoding::UTF_8).grapheme_clusters.each do |gc| - mbchar_width = Reline::Unicode.get_mbchar_width(gc) + line_to_calc.grapheme_clusters.each do |gc| + mbchar = gc.encode(Encoding::UTF_8) + mbchar_width = Reline::Unicode.get_mbchar_width(mbchar) now = new_cursor + mbchar_width if now > end_of_line_cursor or now > cursor break @@ -724,13 +726,17 @@ class Reline::LineEditor Reline::IOGate.move_cursor_column(0) if line.nil? if calculate_width(visual_lines[index - 1], true) == Reline::IOGate.get_screen_size.last - # Reaches the end of line. - # - # When the cursor is at the end of the line and erases characters - # after the cursor, some terminals delete the character at the - # cursor position. - move_cursor_down(1) - Reline::IOGate.move_cursor_column(0) + # reaches the end of line + if Reline::IOGate.win? and Reline::IOGate.win_legacy_console? + # A newline is automatically inserted if a character is rendered at + # eol on command prompt. + else + # When the cursor is at the end of the line and erases characters + # after the cursor, some terminals delete the character at the + # cursor position. + move_cursor_down(1) + Reline::IOGate.move_cursor_column(0) + end else Reline::IOGate.erase_after_cursor move_cursor_down(1) @@ -739,6 +745,10 @@ class Reline::LineEditor next end @output.write line + if Reline::IOGate.win? and Reline::IOGate.win_legacy_console? and calculate_width(line, true) == Reline::IOGate.get_screen_size.last + # A newline is automatically inserted if a character is rendered at eol on command prompt. + @rest_height -= 1 if @rest_height > 0 + end @output.flush if @first_prompt @first_prompt = false diff --git a/lib/reline/unicode.rb b/lib/reline/unicode.rb index 9b5ddc4622..7dbe8a12a5 100644 --- a/lib/reline/unicode.rb +++ b/lib/reline/unicode.rb @@ -108,6 +108,7 @@ class Reline::Unicode end m = mbchar.encode(Encoding::UTF_8).match(MBCharWidthRE) case + when m.nil? then 1 # TODO should be U+FFFD � REPLACEMENT CHARACTER when m[:width_2_1], m[:width_2_2] then 2 when m[:width_3] then 3 when m[:width_0] then 0 diff --git a/lib/reline/version.rb b/lib/reline/version.rb index 5b20f6f3e7..11e8145c7f 100644 --- a/lib/reline/version.rb +++ b/lib/reline/version.rb @@ -1,3 +1,3 @@ module Reline - VERSION = '0.2.3' + VERSION = '0.2.4' end diff --git a/lib/reline/windows.rb b/lib/reline/windows.rb index 4f5fcb74bc..6edc68e780 100644 --- a/lib/reline/windows.rb +++ b/lib/reline/windows.rb @@ -9,6 +9,10 @@ class Reline::Windows true end + def self.win_legacy_console? + @@legacy_console + end + RAW_KEYSTROKE_CONFIG = { [224, 72] => :ed_prev_history, # ↑ [224, 80] => :ed_next_history, # ↓ @@ -94,6 +98,26 @@ class Reline::Windows @@GetFileInformationByHandleEx = Win32API.new('kernel32', 'GetFileInformationByHandleEx', ['L', 'I', 'P', 'L'], 'I') @@FillConsoleOutputAttribute = Win32API.new('kernel32', 'FillConsoleOutputAttribute', ['L', 'L', 'L', 'L', 'P'], 'L') + @@GetConsoleMode = Win32API.new('kernel32', 'GetConsoleMode', ['L', 'P'], 'L') + @@SetConsoleMode = Win32API.new('kernel32', 'SetConsoleMode', ['L', 'L'], 'L') + ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4 + + private_class_method def self.getconsolemode + mode = "\000\000\000\000" + @@GetConsoleMode.call(@@hConsoleHandle, mode) + mode.unpack1('L') + end + + private_class_method def self.setconsolemode(mode) + @@SetConsoleMode.call(@@hConsoleHandle, mode) + end + + @@legacy_console = (getconsolemode() & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0) + #if @@legacy_console + # setconsolemode(getconsolemode() | ENABLE_VIRTUAL_TERMINAL_PROCESSING) + # @@legacy_console = (getconsolemode() & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0) + #end + @@input_buf = [] @@output_buf = [] -- cgit v1.2.3