From e749045319b37e330a8d14b0c120af70cb83da2c Mon Sep 17 00:00:00 2001 From: tompng Date: Sun, 19 Jul 2026 16:31:16 +0900 Subject: [PATCH] Use Reline::ANSI when ruby is built with the msys/cygwin runtime A cygwin-built ruby (e.g. MSYS2's ruby package) routes all I/O through the Cygwin tty layer, which presents an ANSI terminal regardless of what stdin is attached to: a Cygwin pty pipe (mintty) or a Windows console (Windows Terminal, conhost). In console mode, Cygwin enables ENABLE_VIRTUAL_TERMINAL_INPUT, so arrow keys arrive as ESC [ A character events instead of VK_UP key events. Reline::Windows reads the console handle directly via ReadConsoleInputW, bypassing the tty layer, and has no CSI key bindings, so arrow keys were ignored. cygwin/msys were originally not in this branch: until 1cfa5221 the condition was `RUBY_PLATFORM =~ /mswin|mingw/`. That commit switched to the generic "is Windows" host_os regex to support JRuby (whose RUBY_PLATFORM is always "java"); including cygwin/msys was incidental, not motivated by any case where a cygwin ruby needed the console API. JRuby on Windows reports host_os "mswin32" and is unaffected. Fixes https://github.com/ruby/reline/issues/903 Co-Authored-By: Claude Fable 5 --- lib/reline/io.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/reline/io.rb b/lib/reline/io.rb index 17c3a3cd12..5e3ecf7932 100644 --- a/lib/reline/io.rb +++ b/lib/reline/io.rb @@ -10,15 +10,20 @@ def self.decide_io_gate require 'reline/io/ansi' case RbConfig::CONFIG['host_os'] - when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ + when /mswin|mingw|bccwin|wince|emc/ require 'reline/io/windows' io = Reline::Windows.new if io.msys_tty? + # stdin is a Cygwin/MSYS pty pipe (e.g. mintty): no console handle + # exists, and the pty speaks ANSI. Reline::ANSI.new else io end else + # Ruby built with the msys/cygwin runtime also reaches here. Its tty layer + # speaks ANSI in any terminal (mintty pty or Windows console), so the + # Win32 console API must not be used. https://github.com/ruby/reline/issues/903 Reline::ANSI.new end end