Skip to content

Commit e46437d

Browse files
committed
Implement dynamic selection of candidates
1 parent 07e911e commit e46437d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/reline.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,22 @@ def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
183183
end
184184
@dialog_proc = ->() {
185185
# autocomplete
186-
if just_cursor_moving
186+
if just_cursor_moving and completion_journey_data.nil?
187187
# Auto complete starts only when edited
188188
return nil
189189
end
190190
pre, target, post= retrieve_completion_block(true)
191191
if target.nil? or target.empty?
192192
return nil
193193
end
194-
result = call_completion_proc_with_checking_args(pre, target, post)
194+
if completion_journey_data and completion_journey_data.list
195+
result = completion_journey_data.list.dup
196+
result.shift
197+
pointer = completion_journey_data.pointer - 1
198+
else
199+
result = call_completion_proc_with_checking_args(pre, target, post)
200+
pointer = nil
201+
end
195202
if result and result.size == 1 and result[0] == target
196203
result = nil
197204
end
@@ -203,7 +210,7 @@ def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
203210
else
204211
y = 0
205212
end
206-
[Reline::CursorPos.new(x, y), result]
213+
[Reline::CursorPos.new(x, y), result, pointer]
207214
}
208215
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
209216

lib/reline/line_editor.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ def screen_width
514514
@line_editor.instance_variable_get(:@screen_size).last
515515
end
516516

517+
def completion_journey_data
518+
@line_editor.instance_variable_get(:@completion_journey_data)
519+
end
520+
517521
def call
518522
instance_exec(&@proc_to_exec)
519523
end
@@ -533,7 +537,7 @@ def dialog_proc=(p)
533537
return
534538
end
535539
@dialog_proc_scope.set_cursor_pos(cursor_column, @first_line_started_from + @started_from)
536-
pos, result = @dialog_proc_scope.call
540+
pos, result, pointer = @dialog_proc_scope.call
537541
old_dialog_contents = @dialog_contents
538542
old_dialog_contents_width = @dialog_contents_width
539543
old_dialog_column = @dialog_column
@@ -582,7 +586,12 @@ def dialog_proc=(p)
582586
move_cursor_down(@dialog_vertical_offset)
583587
Reline::IOGate.move_cursor_column(@dialog_column)
584588
@dialog_contents.each_with_index do |item, i|
585-
@output.write "\e[46m%-#{DIALOG_WIDTH}s\e[49m" % item.slice(0, DIALOG_WIDTH)
589+
if i == pointer
590+
bg_color = '45'
591+
else
592+
bg_color = '46'
593+
end
594+
@output.write "\e[#{bg_color}m%-#{DIALOG_WIDTH}s\e[49m" % item.slice(0, DIALOG_WIDTH)
586595
Reline::IOGate.move_cursor_column(@dialog_column)
587596
move_cursor_down(1) if i < (@dialog_contents.size - 1)
588597
end

0 commit comments

Comments
 (0)