Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align completion menu items like Readline in noautocomplete mode #613

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,29 @@ module CompletionState
RenderedScreen = Struct.new(:base_y, :lines, :cursor_y, keyword_init: true)

CompletionJourneyData = Struct.new(:preposing, :postposing, :list, :pointer)
MenuInfo = Struct.new(:target, :list)

class MenuInfo
attr_reader :list

def initialize(list)
@list = list
end

def lines(screen_width)
return [] if @list.empty?

list = @list.sort
sizes = list.map { |item| Reline::Unicode.calculate_width(item) }
item_width = sizes.max + 2
num_cols = [screen_width / item_width, 1].max
num_rows = list.size.fdiv(num_cols).ceil
list_with_padding = list.zip(sizes).map { |item, size| item + ' ' * (item_width - size) }
aligned = (list_with_padding + [nil] * (num_rows * num_cols - list_with_padding.size)).each_slice(num_rows).to_a.transpose
aligned.map do |row|
row.join.rstrip
end
end
end

MINIMUM_SCROLLBAR_HEIGHT = 1

Expand Down Expand Up @@ -460,7 +482,7 @@ def render_differential
[[0, Reline::Unicode.calculate_width(l, true), l]]
end
if @menu_info
@menu_info.list.sort!.each do |item|
@menu_info.lines(screen_width).each do |item|
new_lines << [[0, Reline::Unicode.calculate_width(item), item]]
end
@menu_info = nil # TODO: do not change state here
Expand Down Expand Up @@ -781,8 +803,8 @@ def editing_mode
@config.editing_mode
end

private def menu(target, list)
@menu_info = MenuInfo.new(target, list)
private def menu(_target, list)
@menu_info = MenuInfo.new(list)
end

private def complete_internal_proc(list, is_menu)
Expand Down
25 changes: 25 additions & 0 deletions test/reline/test_line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,29 @@ def test_complicated
end
end
end

def test_menu_info_format
list = %w[aa b c d e f g hhh i j k]
col3 = [
'aa e i',
'b f j',
'c g k',
'd hhh'
]
col2 = [
'aa g',
'b hhh',
'c i',
'd j',
'e k',
'f'
]
assert_equal(col3, Reline::LineEditor::MenuInfo.new(list).lines(19))
assert_equal(col3, Reline::LineEditor::MenuInfo.new(list).lines(15))
assert_equal(col2, Reline::LineEditor::MenuInfo.new(list).lines(14))
assert_equal(col2, Reline::LineEditor::MenuInfo.new(list).lines(10))
assert_equal(list, Reline::LineEditor::MenuInfo.new(list).lines(9))
assert_equal(list, Reline::LineEditor::MenuInfo.new(list).lines(0))
assert_equal([], Reline::LineEditor::MenuInfo.new([]).lines(10))
end
end
Loading