mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 16:44:01 +02:00
Implement Reline.input= and Reline.output=
This commit is contained in:
parent
bb56b89900
commit
9cb821b024
3 changed files with 50 additions and 18 deletions
|
@ -26,8 +26,6 @@ module Reline
|
||||||
attr_reader :completion_append_character
|
attr_reader :completion_append_character
|
||||||
attr_accessor :completion_case_fold
|
attr_accessor :completion_case_fold
|
||||||
attr_accessor :filename_quote_characters
|
attr_accessor :filename_quote_characters
|
||||||
attr_writer :input
|
|
||||||
attr_writer :output
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@@config = Reline::Config.new
|
@@config = Reline::Config.new
|
||||||
|
@ -82,6 +80,21 @@ module Reline
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.input=(val)
|
||||||
|
raise TypeError unless val.respond_to?(:getc) or val.nil?
|
||||||
|
if val.respond_to?(:getc)
|
||||||
|
Reline::GeneralIO.input = val
|
||||||
|
remove_const('IO') if const_defined?('IO')
|
||||||
|
const_set('IO', Reline::GeneralIO)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@@output = STDOUT
|
||||||
|
def self.output=(val)
|
||||||
|
raise TypeError unless val.respond_to?(:write) or val.nil?
|
||||||
|
@@output = val
|
||||||
|
end
|
||||||
|
|
||||||
def retrieve_completion_block(line, byte_pointer)
|
def retrieve_completion_block(line, byte_pointer)
|
||||||
break_regexp = /[#{Regexp.escape(@@basic_word_break_characters)}]/
|
break_regexp = /[#{Regexp.escape(@@basic_word_break_characters)}]/
|
||||||
before_pointer = line.byteslice(0, byte_pointer)
|
before_pointer = line.byteslice(0, byte_pointer)
|
||||||
|
@ -139,6 +152,7 @@ module Reline
|
||||||
else
|
else
|
||||||
@@line_editor.multiline_off
|
@@line_editor.multiline_off
|
||||||
end
|
end
|
||||||
|
@@line_editor.output = @@output
|
||||||
@@line_editor.completion_proc = @@completion_proc
|
@@line_editor.completion_proc = @@completion_proc
|
||||||
@@line_editor.dig_perfect_match_proc = @@dig_perfect_match_proc
|
@@line_editor.dig_perfect_match_proc = @@dig_perfect_match_proc
|
||||||
@@line_editor.retrieve_completion_block = method(:retrieve_completion_block)
|
@@line_editor.retrieve_completion_block = method(:retrieve_completion_block)
|
||||||
|
|
|
@ -2,34 +2,47 @@ class Reline::ANSI
|
||||||
def self.getc
|
def self.getc
|
||||||
c = nil
|
c = nil
|
||||||
loop do
|
loop do
|
||||||
result = select([$stdin], [], [], 0.1)
|
result = select([STDIN], [], [], 0.1)
|
||||||
next if result.nil?
|
next if result.nil?
|
||||||
c = $stdin.read(1)
|
c = STDIN.read(1)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
c&.ord
|
c&.ord
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_screen_size
|
def self.get_screen_size
|
||||||
$stdin.winsize
|
STDIN.winsize
|
||||||
|
rescue Errno::ENOTTY
|
||||||
|
[24, 80]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.set_screen_size(rows, columns)
|
def self.set_screen_size(rows, columns)
|
||||||
$stdin.winsize = [rows, columns]
|
STDIN.winsize = [rows, columns]
|
||||||
|
self
|
||||||
|
rescue Errno::ENOTTY
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.cursor_pos
|
def self.cursor_pos
|
||||||
res = ''
|
begin
|
||||||
$stdin.raw do |stdin|
|
res = ''
|
||||||
$stdout << "\e[6n"
|
STDIN.raw do |stdin|
|
||||||
$stdout.flush
|
STDOUT << "\e[6n"
|
||||||
while (c = stdin.getc) != 'R'
|
STDOUT.flush
|
||||||
res << c if c
|
while (c = stdin.getc) != 'R'
|
||||||
|
res << c if c
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
res
|
||||||
|
m = res.match(/(?<row>\d+);(?<column>\d+)/)
|
||||||
|
column = m[:column].to_i - 1
|
||||||
|
row = m[:row].to_i - 1
|
||||||
|
rescue Errno::ENOTTY
|
||||||
|
buf = STDOUT.pread(STDOUT.pos, 0)
|
||||||
|
row = buf.count("\n")
|
||||||
|
column = buf.rindex("\n") ? (buf.size - buf.rindex("\n")) - 1 : 0
|
||||||
end
|
end
|
||||||
m = res.match(/(?<row>\d+);(?<column>\d+)/)
|
Reline::CursorPos.new(column, row)
|
||||||
Reline::CursorPos.new(m[:column].to_i - 1, m[:row].to_i - 1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.move_cursor_column(x)
|
def self.move_cursor_column(x)
|
||||||
|
|
|
@ -11,6 +11,7 @@ class Reline::LineEditor
|
||||||
attr_accessor :completion_proc
|
attr_accessor :completion_proc
|
||||||
attr_accessor :dig_perfect_match_proc
|
attr_accessor :dig_perfect_match_proc
|
||||||
attr_writer :retrieve_completion_block
|
attr_writer :retrieve_completion_block
|
||||||
|
attr_writer :output
|
||||||
|
|
||||||
ARGUMENTABLE = %i{
|
ARGUMENTABLE = %i{
|
||||||
ed_delete_next_char
|
ed_delete_next_char
|
||||||
|
@ -220,9 +221,9 @@ class Reline::LineEditor
|
||||||
@rest_height ||= (Reline::IO.get_screen_size.first - 1) - Reline::IO.cursor_pos.y
|
@rest_height ||= (Reline::IO.get_screen_size.first - 1) - Reline::IO.cursor_pos.y
|
||||||
@screen_size ||= Reline::IO.get_screen_size
|
@screen_size ||= Reline::IO.get_screen_size
|
||||||
if @menu_info
|
if @menu_info
|
||||||
puts
|
@output.puts
|
||||||
@menu_info.list.each do |item|
|
@menu_info.list.each do |item|
|
||||||
puts item
|
@output.puts item
|
||||||
end
|
end
|
||||||
@menu_info = nil
|
@menu_info = nil
|
||||||
end
|
end
|
||||||
|
@ -359,12 +360,16 @@ class Reline::LineEditor
|
||||||
visual_lines.each_with_index do |line, index|
|
visual_lines.each_with_index do |line, index|
|
||||||
Reline::IO.move_cursor_column(0)
|
Reline::IO.move_cursor_column(0)
|
||||||
escaped_print line
|
escaped_print line
|
||||||
|
if @first_prompt
|
||||||
|
@first_prompt = false
|
||||||
|
@pre_input_hook&.call
|
||||||
|
end
|
||||||
Reline::IO.erase_after_cursor
|
Reline::IO.erase_after_cursor
|
||||||
move_cursor_down(1) if index < (visual_lines.size - 1)
|
move_cursor_down(1) if index < (visual_lines.size - 1)
|
||||||
end
|
end
|
||||||
if with_control
|
if with_control
|
||||||
if finished?
|
if finished?
|
||||||
puts
|
@output.puts
|
||||||
else
|
else
|
||||||
move_cursor_up((visual_lines.size - 1) - @started_from)
|
move_cursor_up((visual_lines.size - 1) - @started_from)
|
||||||
Reline::IO.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
|
Reline::IO.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
|
||||||
|
@ -378,7 +383,7 @@ class Reline::LineEditor
|
||||||
end
|
end
|
||||||
|
|
||||||
private def escaped_print(str)
|
private def escaped_print(str)
|
||||||
print str.chars.map { |gr|
|
@output.print str.chars.map { |gr|
|
||||||
escaped = Reline::Unicode::EscapedPairs[gr.ord]
|
escaped = Reline::Unicode::EscapedPairs[gr.ord]
|
||||||
if escaped
|
if escaped
|
||||||
escaped
|
escaped
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue