Skip to content

Commit

Permalink
Overhaul io gate structure (#666)
Browse files Browse the repository at this point in the history
* Overhaul IO gate structure

1. Move IO related classes to `lib/reline/io/` directory.
2. Rename `GeneralIO` to `Dumb`.
3. Use IO classes as instances instead of classes.

* Update lib/reline/io/ansi.rb

Co-authored-by: tomoya ishida <tomoyapenguin@gmail.com>

---------

Co-authored-by: tomoya ishida <tomoyapenguin@gmail.com>
  • Loading branch information
st0012 and tompng authored Jun 1, 2024
1 parent 43cd4c5 commit dc1518e
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 338 deletions.
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

0 comments on commit dc1518e

Please sign in to comment.