Module: Arfi::Commands::FunctionsRendering
- Included in:
- Functions
- Defined in:
- lib/arfi/commands/functions_rendering.rb,
sig/lib/arfi/commands/functions_rendering.rbs
Overview
Table rendering helpers for Functions.
Instance Method Summary collapse
-
#all_mode_rows(arr, chosen) ⇒ Array<Hash<Symbol, Object>>
Build display rows for --all mode (shows all candidates including overridden ones).
-
#calculate_widths(cols, table) ⇒ Hash<String, Integer>
Calculate the maximum display width for each column.
-
#default_mode_row(chosen, arr) ⇒ Array<Hash<Symbol, Object>>
Build a single display row for default mode (only the chosen candidate).
-
#print_table(rows) ⇒ void
Print the function list as a formatted ASCII table.
-
#render_list(rows) ⇒ void
Render the resolved function list in the selected format (paths, json, or table).
-
#render_table_rows(table, cols, widths) ⇒ void
Render each table row with proper column alignment.
-
#resolve_key_group(arr) ⇒ Array<Hash<Symbol, Object>>
Resolve a group of same-key candidates to the chosen one with shadowed info.
-
#stringify_rows(rows) ⇒ Array<Hash<Symbol, Object>>
Convert all row values to strings for display, handling special columns.
-
#table_columns ⇒ Array<String>
Determine which columns to display based on the --all flag.
Instance Method Details
#all_mode_rows(arr, chosen) ⇒ Array<Hash<Symbol, Object>>
Build display rows for --all mode (shows all candidates including overridden ones).
115 116 117 118 119 120 121 122 123 |
# File 'lib/arfi/commands/functions_rendering.rb', line 115 def all_mode_rows(arr, chosen) chosen_path = chosen[:path] arr.sort_by { |c| [-c[:priority], c[:schema], c[:function]] }.map do |c| c.merge( chosen: (c == chosen), shadowed_by: (c == chosen ? nil : rel(chosen_path)) ) end end |
#calculate_widths(cols, table) ⇒ Hash<String, Integer>
Calculate the maximum display width for each column.
72 73 74 75 76 77 78 |
# File 'lib/arfi/commands/functions_rendering.rb', line 72 def calculate_widths(cols, table) widths = {} # steep:ignore cols.each do |c| widths[c] = ([c.length] + table.map { |r| r[c.to_sym].to_s.length }).max end widths end |
#default_mode_row(chosen, arr) ⇒ Array<Hash<Symbol, Object>>
Build a single display row for default mode (only the chosen candidate).
131 132 133 134 |
# File 'lib/arfi/commands/functions_rendering.rb', line 131 def default_mode_row(chosen, arr) shadowed = (arr - [chosen]) [chosen.merge(chosen: true, shadowed: shadowed.map { rel(_1[:path]) })] end |
#print_table(rows) ⇒ void
This method returns an undefined value.
Print the function list as a formatted ASCII table.
30 31 32 33 34 35 36 37 |
# File 'lib/arfi/commands/functions_rendering.rb', line 30 def print_table(rows) cols = table_columns table = stringify_rows(rows) widths = calculate_widths(cols, table) puts cols.map { |c| c.ljust(widths[c]) }.join(' ') puts cols.map { |c| '-' * widths[c] }.join(' ') render_table_rows(table, cols, widths) end |
#render_list(rows) ⇒ void
This method returns an undefined value.
Render the resolved function list in the selected format (paths, json, or table).
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/arfi/commands/functions_rendering.rb', line 14 def render_list(rows) case [:format].to_s when 'paths' rows.each { puts rel(_1[:path]) } when 'json' puts JSON.pretty_generate(rows.map { |r| r.merge(path: rel(r[:path])) }) else print_table(rows) end end |
#render_table_rows(table, cols, widths) ⇒ void
This method returns an undefined value.
Render each table row with proper column alignment.
87 88 89 90 91 |
# File 'lib/arfi/commands/functions_rendering.rb', line 87 def render_table_rows(table, cols, widths) table.each do |r| puts cols.map { |c| r[c.to_sym].to_s.ljust(widths[c]) }.join(' ') end end |
#resolve_key_group(arr) ⇒ Array<Hash<Symbol, Object>>
Resolve a group of same-key candidates to the chosen one with shadowed info.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/arfi/commands/functions_rendering.rb', line 98 def resolve_key_group(arr) chosen = arr.max_by { |c| c[:priority] } return [] unless chosen if [:all] all_mode_rows(arr, chosen) else default_mode_row(chosen, arr) end end |
#stringify_rows(rows) ⇒ Array<Hash<Symbol, Object>>
Convert all row values to strings for display, handling special columns.
56 57 58 59 60 61 62 63 64 |
# File 'lib/arfi/commands/functions_rendering.rb', line 56 def stringify_rows(rows) rows.map do |r| r = r.dup r[:path] = rel(r[:path]) r[:chosen] = r[:chosen] ? 'yes' : 'no' if r.key?(:chosen) r[:shadowed] = (r[:shadowed] || []).join(', ') if r.key?(:shadowed) r end end |
#table_columns ⇒ Array<String>
Determine which columns to display based on the --all flag.
43 44 45 46 47 48 49 |
# File 'lib/arfi/commands/functions_rendering.rb', line 43 def table_columns if [:all] %w[chosen schema function source origin priority path shadowed_by] else %w[schema function source origin priority path shadowed] end end |