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

Overhaul io gate structure #666

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'reline/line_editor'
require 'reline/history'
require 'reline/terminfo'
require 'reline/io'
require 'reline/face'
require 'rbconfig'

Expand Down Expand Up @@ -336,7 +337,7 @@ def readline(prompt = '', add_hist = false)
line_editor.auto_indent_proc = auto_indent_proc
line_editor.dig_perfect_match_proc = dig_perfect_match_proc
pre_input_hook&.call
unless Reline::IOGate == Reline::GeneralIO
unless Reline::IOGate.dumb?
@dialog_proc_list.each_pair do |name_sym, d|
line_editor.add_dialog_proc(name_sym, d.dialog_proc, d.context)
end
Expand Down Expand Up @@ -473,7 +474,7 @@ def ambiguous_width
end

private def may_req_ambiguous_char_width
@ambiguous_width = 2 if io_gate == Reline::GeneralIO or !STDOUT.tty?
@ambiguous_width = 2 if io_gate.dumb? or !STDOUT.tty?
return if defined? @ambiguous_width
io_gate.move_cursor_column(0)
begin
Expand Down Expand Up @@ -573,31 +574,19 @@ def self.update_iogate

# Need to change IOGate when `$stdout.tty?` change from false to true by `$stdout.reopen`
# Example: rails/spring boot the application in non-tty, then run console in tty.
if ENV['TERM'] != 'dumb' && core.io_gate == Reline::GeneralIO && $stdout.tty?
require 'reline/ansi'
if ENV['TERM'] != 'dumb' && core.io_gate.dumb? && $stdout.tty?
require 'reline/io/ansi'
remove_const(:IOGate)
const_set(:IOGate, Reline::ANSI)
const_set(:IOGate, Reline::ANSI.new)
end
end
end

require 'reline/general_io'
io = Reline::GeneralIO
unless ENV['TERM'] == 'dumb'
case RbConfig::CONFIG['host_os']
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
require 'reline/windows'
tty = (io = Reline::Windows).msys_tty?
else
tty = $stdout.tty?
end
end
Reline::IOGate = if tty
require 'reline/ansi'
Reline::ANSI
else
io
end

Reline::IOGate = Reline::IO.decide_io_gate

# Deprecated
Reline::GeneralIO = Reline::Dumb.new

Reline::Face.load_initial_configs

Expand Down
111 changes: 0 additions & 111 deletions lib/reline/general_io.rb

This file was deleted.

45 changes: 45 additions & 0 deletions lib/reline/io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

module Reline
class IO
RESET_COLOR = "\e[0m"

def self.decide_io_gate
if ENV['TERM'] == 'dumb'
Reline::Dumb.new
else
require 'reline/io/ansi'

case RbConfig::CONFIG['host_os']
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
require 'reline/io/windows'
io = Reline::Windows.new
if io.msys_tty?
Reline::ANSI.new
else
io
end
else
if $stdout.tty?
Reline::ANSI.new
else
Reline::Dumb.new
end
end
end
end

def dumb?
false
end

def win?
false
end

def reset_color_sequence
self.class::RESET_COLOR
end
end
end

require 'reline/io/dumb'
Loading
Loading