diff options
author | aycabta <aycabta@gmail.com> | 2021-08-17 19:21:27 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2021-08-29 20:30:32 +0900 |
commit | fb0fc201963c5e70e62b72e0ac9e27dc39e0f5ec (patch) | |
tree | 5740227abbada228f1549ed0fc900fc333319724 /lib/reline.rb | |
parent | e66200780b5a07c070295231408873c8d3dd7c5e (diff) |
[ruby/reline] Implement dialog with autocomplete callback
https://github.com/ruby/reline/commit/1401d6165e
Diffstat (limited to 'lib/reline.rb')
-rw-r--r-- | lib/reline.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/reline.rb b/lib/reline.rb index d68627dca5..fade2da9d6 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -34,6 +34,7 @@ module Reline auto_indent_proc pre_input_hook dig_perfect_match_proc + dialog_proc ).each(&method(:attr_reader)) attr_accessor :config @@ -130,6 +131,11 @@ module Reline @dig_perfect_match_proc = p end + def dialog_proc=(p) + raise ArgumentError unless p.respond_to?(:call) or p.nil? + @dialog_proc = p + end + def input=(val) raise TypeError unless val.respond_to?(:getc) or val.nil? if val.respond_to?(:getc) @@ -175,6 +181,23 @@ module Reline unless confirm_multiline_termination raise ArgumentError.new('#readmultiline needs block to confirm multiline termination') end + @dialog_proc = ->() { + # autocomplete + if just_cursor_moving + # Auto complete starts only when endited + return nil + end + pre, target, post= retrieve_completion_block(true) + if target.nil? or target.empty? + result = nil + else + result = call_completion_proc_with_checking_args(pre, target, post) + if result and result.size == 1 and result[0] == target + result = nil + end + end + [Reline::CursorPos.new(cursor_pos.x - Reline::Unicode.calculate_width(target), nil), result] + } inner_readline(prompt, add_hist, true, &confirm_multiline_termination) whole_buffer = line_editor.whole_buffer.dup @@ -230,6 +253,7 @@ module Reline line_editor.auto_indent_proc = auto_indent_proc line_editor.dig_perfect_match_proc = dig_perfect_match_proc line_editor.pre_input_hook = pre_input_hook + line_editor.dialog_proc = dialog_proc unless config.test_mode config.read |