Class: Foobara::CommandConnectors::ShCliConnector::Serializers::CliTabularSerializer

Inherits:
Foobara::CommandConnectors::Serializer show all
Defined in:
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb

Overview

TODO: refactor much of this logic to Util?

Constant Summary collapse

PAD_SIZE =
1

Instance Attribute Summary

Attributes inherited from Value::Processor

#created_in_namespace, #declaration_data, #parent_declaration_data

Instance Method Summary collapse

Methods inherited from Foobara::CommandConnectors::Serializer

#initialize, #request, serializer_from_symbol, #transform

Methods inherited from Value::Transformer

create, error_classes, foobara_manifest, #process_value, subclass, #transform

Methods inherited from Value::Processor

#always_applicable?, #applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, error_classes, #error_context, #error_message, #error_path, foobara_manifest, #foobara_manifest, #initialize, #inspect, instance, #method_missing, #name, new_with_agnostic_args, #possible_errors, #priority, #process_outcome, #process_outcome!, #process_value, #process_value!, processor_name, requires_declaration_data?, requires_parent_declaration_data?, #respond_to_missing?, #runner, symbol

Methods included from IsManifestable

#foobara_domain, #foobara_manifest, #foobara_organization, #scoped_clear_caches

Methods included from Foobara::Concern

foobara_class_methods_module_for, foobara_concern?, included

Constructor Details

This class inherits a constructor from Foobara::CommandConnectors::Serializer

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::Value::Processor

Instance Method Details

#adjust_to_preserve_final_column_width(widths) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 75

def adjust_to_preserve_final_column_width(widths)
  adjusted_final_width = final_width(widths)

  too_small_by = min_final_column_width - adjusted_final_width

  if too_small_by > 0
    reduce_each_by = too_small_by.to_f / (widths.size - 1)
    reduce_each_by = reduce_each_by.ceil

    widths = widths.map do |width|
      width - reduce_each_by
    end

    adjusted_final_width = final_width(widths)
  end

  widths[-1] = adjusted_final_width

  widths
end

#cellify(table, widths) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 150

def cellify(table, widths)
  cellified_table = []

  row_regexes = widths.map do |width|
    /\S.{0,#{width}}\S(?=\s|$)|\S+/
  end

  table.each do |row|
    row = Util.array(row)

    cellified_table << cellified_row = []

    row.each.with_index do |column, index|
      cellified_row << cell = []

      next unless column

      width = widths[index]

      column.scan(row_regexes[index]).each do |rows_worth_of_text|
        if rows_worth_of_text.size > width
          parts = rows_worth_of_text.scan(/.{1,#{width - 1}}/)

          parts_size = parts.size

          parts.each.with_index do |rows_worth, inner_index|
            rows_worth << "-" unless inner_index == parts_size - 1
            cell << rows_worth
          end
        else
          cell << rows_worth_of_text
        end
      end
    end
  end

  cellified_table
end

#column_widths(table) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 56

def column_widths(table)
  max_row_length = table.map(&:size).max

  widths = [0] * max_row_length

  table.each do |row|
    row.each.with_index do |column, index|
      next unless column

      column_width = column.size
      if column_width > widths[index]
        widths[index] = column_width
      end
    end
  end

  widths
end

#final_width(widths) ⇒ Object



96
97
98
99
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 96

def final_width(widths)
  all_but_last_width = widths[0..-2].sum
  terminal_width - all_but_last_width
end

#indentObject



118
119
120
121
122
123
124
125
126
127
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 118

def indent
  @indent ||= if declaration_data.is_a?(::Hash)
                declaration_data[:indent]
              elsif declaration_data.respond_to?(:indent)
                # TODO: test this
                # :nocov:
                declaration_data.indent
                # :nocov:
              end || 0
end

#min_final_column_widthObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 129

def min_final_column_width
  @min_final_column_width ||= begin
    config = declaration_data

    width = if config.is_a?(::Hash)
              config[:min_final_column_width]
            elsif config.respond_to?(:min_final_column_width)
              # TODO: test this
              # :nocov:
              config.min_final_column_width
              # :nocov:
            end

    width || 10
  end
end

#pad(string, width) ⇒ Object



146
147
148
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 146

def pad(string, width)
  string.ljust(width + PAD_SIZE)
end

#serialize(table) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 11

def serialize(table)
  return "" if table.empty?

  io = StringIO.new

  widths = column_widths(table)
  widths = adjust_to_preserve_final_column_width(widths)

  cellified_table = cellify(table, widths)

  cellified_table.each do |row|
    max_lines = row.map(&:size).max

    lines = []

    row.each.with_index do |cell, index|
      is_last_column = index == row.size - 1
      width = widths[index]

      cell.each.with_index do |line, line_index|
        lines[line_index] ||= ""
        line = pad(line, width) unless is_last_column
        lines[line_index] << line
      end

      unless is_last_column
        (max_lines - cell.size).times do |i|
          line = lines[max_lines - i - 1] ||= ""
          line << pad("", width)
        end
      end
    end

    lines.each do |line|
      if indent > 0
        line = (" " * indent) + line
      end

      io.puts line.rstrip
    end
  end

  io.string
end

#terminal_widthObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb', line 101

def terminal_width
  @terminal_width ||= begin
    config = declaration_data

    width = if config.is_a?(::Hash)
              config[:terminal_width]
            elsif config.respond_to?(:terminal_width)
              # TODO: test this
              # :nocov:
              config.terminal_width
              # :nocov:
            end

    (width || IO.console_size[1]) - indent
  end
end