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

Instance Method Details

#all_mode_rows(arr, chosen) ⇒ Array<Hash<Symbol, Object>>

Build display rows for --all mode (shows all candidates including overridden ones).

Parameters:

  • arr (Array<Arfi::Commands::candidate>)

    All candidates for one function key

  • chosen (Arfi::Commands::candidate)

    The selected (highest-priority) candidate

Returns:

  • (Array<Hash<Symbol, Object>>)

    Display rows



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.

Parameters:

  • cols (Array<String>)

    Column names

  • table (Array<Hash<Symbol, Object>>)

    Stringified table rows

Returns:

  • (Hash<String, Integer>)

    Column widths keyed by column name



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).

Parameters:

  • chosen (Arfi::Commands::candidate)

    The selected (highest-priority) candidate

  • arr (Array<Arfi::Commands::candidate>)

    All candidates for one function key

Returns:

  • (Array<Hash<Symbol, Object>>)

    Single display row



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

This method returns an undefined value.

Print the function list as a formatted ASCII table.

Parameters:

  • rows (Array<Hash<Symbol, Object>>)

    Resolved function rows



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).

Parameters:

  • rows (Array<Hash<Symbol, Object>>)

    Resolved function rows



14
15
16
17
18
19
20
21
22
23
# File 'lib/arfi/commands/functions_rendering.rb', line 14

def render_list(rows)
  case options[: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.

Parameters:

  • table (Array<Hash<Symbol, Object>>)

    Stringified table rows

  • cols (Array<String>)

    Column names

  • widths (Hash<String, Integer>)

    Calculated column widths



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.

Parameters:

  • arr (Array<Arfi::Commands::candidate>)

    Candidates for one function key

Returns:

  • (Array<Hash<Symbol, Object>>)

    Resolved row(s) for display



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 options[: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.

Parameters:

  • rows (Array<Hash<Symbol, Object>>)

    Resolved function rows

Returns:

  • (Array<Hash<Symbol, Object>>)

    Rows with string values



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_columnsArray<String>

Determine which columns to display based on the --all flag.

Returns:

  • (Array<String>)

    List of column names



43
44
45
46
47
48
49
# File 'lib/arfi/commands/functions_rendering.rb', line 43

def table_columns
  if options[:all]
    %w[chosen schema function source origin priority path shadowed_by]
  else
    %w[schema function source origin priority path shadowed]
  end
end