Skip to content

Commit dc1518e

Browse files
st0012tompng
andauthored
Overhaul io gate structure (#666)
* 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>
1 parent 43cd4c5 commit dc1518e

13 files changed

Lines changed: 366 additions & 338 deletions

‎lib/reline.rb‎

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'reline/line_editor'
88
require 'reline/history'
99
require 'reline/terminfo'
10+
require 'reline/io'
1011
require 'reline/face'
1112
require 'rbconfig'
1213

@@ -336,7 +337,7 @@ def readline(prompt = '', add_hist = false)
336337
line_editor.auto_indent_proc = auto_indent_proc
337338
line_editor.dig_perfect_match_proc = dig_perfect_match_proc
338339
pre_input_hook&.call
339-
unless Reline::IOGate == Reline::GeneralIO
340+
unless Reline::IOGate.dumb?
340341
@dialog_proc_list.each_pair do |name_sym, d|
341342
line_editor.add_dialog_proc(name_sym, d.dialog_proc, d.context)
342343
end
@@ -473,7 +474,7 @@ def ambiguous_width
473474
end
474475

475476
private def may_req_ambiguous_char_width
476-
@ambiguous_width = 2 if io_gate == Reline::GeneralIO or !STDOUT.tty?
477+
@ambiguous_width = 2 if io_gate.dumb? or !STDOUT.tty?
477478
return if defined? @ambiguous_width
478479
io_gate.move_cursor_column(0)
479480
begin
@@ -573,31 +574,19 @@ def self.update_iogate
573574

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

584-
require 'reline/general_io'
585-
io = Reline::GeneralIO
586-
unless ENV['TERM'] == 'dumb'
587-
case RbConfig::CONFIG['host_os']
588-
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
589-
require 'reline/windows'
590-
tty = (io = Reline::Windows).msys_tty?
591-
else
592-
tty = $stdout.tty?
593-
end
594-
end
595-
Reline::IOGate = if tty
596-
require 'reline/ansi'
597-
Reline::ANSI
598-
else
599-
io
600-
end
585+
586+
Reline::IOGate = Reline::IO.decide_io_gate
587+
588+
# Deprecated
589+
Reline::GeneralIO = Reline::Dumb.new
601590

602591
Reline::Face.load_initial_configs
603592

‎lib/reline/general_io.rb‎

Lines changed: 0 additions & 111 deletions
This file was deleted.

‎lib/reline/io.rb‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
module Reline
3+
class IO
4+
RESET_COLOR = "\e[0m"
5+
6+
def self.decide_io_gate
7+
if ENV['TERM'] == 'dumb'
8+
Reline::Dumb.new
9+
else
10+
require 'reline/io/ansi'
11+
12+
case RbConfig::CONFIG['host_os']
13+
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
14+
require 'reline/io/windows'
15+
io = Reline::Windows.new
16+
if io.msys_tty?
17+
Reline::ANSI.new
18+
else
19+
io
20+
end
21+
else
22+
if $stdout.tty?
23+
Reline::ANSI.new
24+
else
25+
Reline::Dumb.new
26+
end
27+
end
28+
end
29+
end
30+
31+
def dumb?
32+
false
33+
end
34+
35+
def win?
36+
false
37+
end
38+
39+
def reset_color_sequence
40+
self.class::RESET_COLOR
41+
end
42+
end
43+
end
44+
45+
require 'reline/io/dumb'

0 commit comments

Comments
 (0)