diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2017-09-24 22:57:46 -0300 |
---|---|---|
committer | Abinoam P. Marques Jr <abinoam@gmail.com> | 2017-09-25 06:51:00 -0300 |
commit | 56b7182d6f57b007759b3444130c67858b155164 (patch) | |
tree | 95b8308c55d3a310b2db3fb9472f1e66f95b894e | |
parent | e4aef4f20a2cab1f471eca70bb5fa52ffadebfe7 (diff) | |
download | highline-56b7182d6f57b007759b3444130c67858b155164.tar.gz |
Fix class/module nesting
-rw-r--r-- | lib/highline/list_renderer.rb | 397 | ||||
-rw-r--r-- | test/acceptance/acceptance_test.rb | 104 |
2 files changed, 255 insertions, 246 deletions
diff --git a/lib/highline/list_renderer.rb b/lib/highline/list_renderer.rb index 8e2d677..cc53bf0 100644 --- a/lib/highline/list_renderer.rb +++ b/lib/highline/list_renderer.rb @@ -4,247 +4,254 @@ require "highline/template_renderer" require "highline/wrapper" require "highline/list" -# -# This class is a utility for quickly and easily laying out lists -# to be used by HighLine. -# -class HighLine::ListRenderer - # Items list - # @return [Array] - attr_reader :items - - # @return [Symbol] the current mode the List is being rendered - # @see #initialize for more details see mode parameter of #initialize - attr_reader :mode - - # Changes the behaviour of some modes. Example, in :inline mode - # the option is treated as the 'end separator' (defaults to " or ") - # @return option parameter that changes the behaviour of some modes. - attr_reader :option - - # @return [HighLine] context - attr_reader :highline - - # The only required parameters are _items_ and _highline_. - # @param items [Array] the Array of items to list - # @param mode [Symbol] controls how that list is formed - # @param option has different effects, depending on the _mode_. - # @param highline [HighLine] a HighLine instance to direct the output to. +class HighLine # - # Recognized modes are: + # This class is a utility for quickly and easily laying out lists + # to be used by HighLine. # - # <tt>:columns_across</tt>:: _items_ will be placed in columns, - # flowing from left to right. If given, - # _option_ is the number of columns to be - # used. When absent, columns will be - # determined based on _wrap_at_ or a - # default of 80 characters. - # <tt>:columns_down</tt>:: Identical to <tt>:columns_across</tt>, - # save flow goes down. - # <tt>:uneven_columns_across</tt>:: Like <tt>:columns_across</tt> but each - # column is sized independently. - # <tt>:uneven_columns_down</tt>:: Like <tt>:columns_down</tt> but each - # column is sized independently. - # <tt>:inline</tt>:: All _items_ are placed on a single line. - # The last two _items_ are separated by - # _option_ or a default of " or ". All - # other _items_ are separated by ", ". - # <tt>:rows</tt>:: The default mode. Each of the _items_ is - # placed on its own line. The _option_ - # parameter is ignored in this mode. - # - # Each member of the _items_ Array is passed through ERb and thus can contain - # their own expansions. Color escape expansions do not contribute to the - # final field width. - - def initialize(items, mode = :rows, option = nil, highline) - @highline = highline - @mode = mode - @option = option - @items = render_list_items(items) - end + class ListRenderer + # Items list + # @return [Array] + attr_reader :items + + # @return [Symbol] the current mode the List is being rendered + # @see #initialize for more details see mode parameter of #initialize + attr_reader :mode + + # Changes the behaviour of some modes. Example, in :inline mode + # the option is treated as the 'end separator' (defaults to " or ") + # @return option parameter that changes the behaviour of some modes. + attr_reader :option + + # @return [HighLine] context + attr_reader :highline + + # The only required parameters are _items_ and _highline_. + # @param items [Array] the Array of items to list + # @param mode [Symbol] controls how that list is formed + # @param option has different effects, depending on the _mode_. + # @param highline [HighLine] a HighLine instance to direct the output to. + # + # Recognized modes are: + # + # <tt>:columns_across</tt>:: _items_ will be placed in columns, + # flowing from left to right. If given, + # _option_ is the number of columns to be + # used. When absent, columns will be + # determined based on _wrap_at_ or a + # default of 80 characters. + # <tt>:columns_down</tt>:: Identical to <tt>:columns_across</tt>, + # save flow goes down. + # <tt>:uneven_columns_across</tt>:: Like <tt>:columns_across</tt> but each + # column is sized independently. + # <tt>:uneven_columns_down</tt>:: Like <tt>:columns_down</tt> but each + # column is sized independently. + # <tt>:inline</tt>:: All _items_ are placed on a single + # line. The last two _items_ are + # separated by _option_ or a default of + # " or ". All other _items_ are + # separated by ", ". + # <tt>:rows</tt>:: The default mode. Each of the _items_ + # is placed on its own line. The _option_ + # parameter is ignored in this mode. + # + # Each member of the _items_ Array is passed through ERb and thus can + # contain their own expansions. Color escape expansions do not contribute to + # the final field width. + + def initialize(items, mode = :rows, option = nil, highline) + @highline = highline + @mode = mode + @option = option + @items = render_list_items(items) + end - # Render the list using the appropriate mode and options. - # @return [String] rendered list as String - def render - return "" if items.empty? - - case mode - when :inline - list_inline_mode - when :columns_across - list_columns_across_mode - when :columns_down - list_columns_down_mode - when :uneven_columns_across - list_uneven_columns_mode - when :uneven_columns_down - list_uneven_columns_down_mode - else - list_default_mode + # Render the list using the appropriate mode and options. + # @return [String] rendered list as String + def render + return "" if items.empty? + + case mode + when :inline + list_inline_mode + when :columns_across + list_columns_across_mode + when :columns_down + list_columns_down_mode + when :uneven_columns_across + list_uneven_columns_mode + when :uneven_columns_down + list_uneven_columns_down_mode + else + list_default_mode + end end - end - private + private - def render_list_items(items) - items.to_ary.map do |item| - item = String(item) - template = ERB.new(item, nil, "%") - template_renderer = - HighLine::TemplateRenderer.new(template, self, highline) - template_renderer.render + def render_list_items(items) + items.to_ary.map do |item| + item = String(item) + template = ERB.new(item, nil, "%") + template_renderer = + HighLine::TemplateRenderer.new(template, self, highline) + template_renderer.render + end end - end - def list_default_mode - items.map { |i| "#{i}\n" }.join - end + def list_default_mode + items.map { |i| "#{i}\n" }.join + end - def list_inline_mode - end_separator = option || " or " + def list_inline_mode + end_separator = option || " or " - if items.size == 1 - items.first - else - items[0..-2].join(", ") + "#{end_separator}#{items.last}" + if items.size == 1 + items.first + else + items[0..-2].join(", ") + "#{end_separator}#{items.last}" + end end - end - def list_columns_across_mode - HighLine::List.new(right_padded_items, cols: col_count).to_s - end + def list_columns_across_mode + HighLine::List.new(right_padded_items, cols: col_count).to_s + end - def list_columns_down_mode - HighLine::List.new(right_padded_items, cols: col_count, col_down: true).to_s - end + def list_columns_down_mode + HighLine::List.new( + right_padded_items, + cols: col_count, + col_down: true + ).to_s + end - def list_uneven_columns_mode(list = nil) - list ||= HighLine::List.new(items) + def list_uneven_columns_mode(list = nil) + list ||= HighLine::List.new(items) - col_max = option || items.size - col_max.downto(1) do |column_count| - list.cols = column_count - widths = get_col_widths(list) + col_max = option || items.size + col_max.downto(1) do |column_count| + list.cols = column_count + widths = get_col_widths(list) - if column_count == 1 || # last guess - inside_line_size_limit?(widths) || # good guess - option # defined by user - return pad_uneven_rows(list, widths) + if column_count == 1 || # last guess + inside_line_size_limit?(widths) || # good guess + option # defined by user + return pad_uneven_rows(list, widths) + end end end - end - def list_uneven_columns_down_mode - list = HighLine::List.new(items, col_down: true) - list_uneven_columns_mode(list) - end + def list_uneven_columns_down_mode + list = HighLine::List.new(items, col_down: true) + list_uneven_columns_mode(list) + end - def pad_uneven_rows(list, widths) - right_padded_list = Array(list).map do |row| - right_pad_row(row.compact, widths) + def pad_uneven_rows(list, widths) + right_padded_list = Array(list).map do |row| + right_pad_row(row.compact, widths) + end + stringfy_list(right_padded_list) end - stringfy_list(right_padded_list) - end - def stringfy_list(list) - list.map { |row| row_to_s(row) }.join - end + def stringfy_list(list) + list.map { |row| row_to_s(row) }.join + end - def row_to_s(row) - row.compact.join(row_join_string) + "\n" - end + def row_to_s(row) + row.compact.join(row_join_string) + "\n" + end - def right_pad_row(row, widths) - row.zip(widths).map do |field, width| - right_pad_field(field, width) + def right_pad_row(row, widths) + row.zip(widths).map do |field, width| + right_pad_field(field, width) + end end - end - def right_pad_field(field, width) - field = String(field) # nil protection - pad_size = width - actual_length(field) - field + (pad_char * pad_size) - end + def right_pad_field(field, width) + field = String(field) # nil protection + pad_size = width - actual_length(field) + field + (pad_char * pad_size) + end - def get_col_widths(lines) - lines = transpose(lines) - get_segment_widths(lines) - end + def get_col_widths(lines) + lines = transpose(lines) + get_segment_widths(lines) + end - def get_row_widths(lines) - get_segment_widths(lines) - end + def get_row_widths(lines) + get_segment_widths(lines) + end - def get_segment_widths(lines) - lines.map do |col| - actual_lengths_for(col).max + def get_segment_widths(lines) + lines.map do |col| + actual_lengths_for(col).max + end end - end - def actual_lengths_for(line) - line.map do |item| - actual_length(item) + def actual_lengths_for(line) + line.map do |item| + actual_length(item) + end end - end - def transpose(lines) - lines = Array(lines) - first_line = lines.shift - first_line.zip(*lines) - end + def transpose(lines) + lines = Array(lines) + first_line = lines.shift + first_line.zip(*lines) + end - def inside_line_size_limit?(widths) - line_size = widths.reduce(0) { |sum, n| sum + n + row_join_str_size } - line_size <= line_size_limit + row_join_str_size - end + def inside_line_size_limit?(widths) + line_size = widths.reduce(0) { |sum, n| sum + n + row_join_str_size } + line_size <= line_size_limit + row_join_str_size + end - def actual_length(text) - HighLine::Wrapper.actual_length text - end + def actual_length(text) + HighLine::Wrapper.actual_length text + end - def items_max_length - @items_max_length ||= max_length(items) - end + def items_max_length + @items_max_length ||= max_length(items) + end - def max_length(items) - items.map { |item| actual_length(item) }.max - end + def max_length(items) + items.map { |item| actual_length(item) }.max + end - def line_size_limit - @line_size_limit ||= (highline.wrap_at || 80) - end + def line_size_limit + @line_size_limit ||= (highline.wrap_at || 80) + end - def row_join_string - @row_join_string ||= " " - end + def row_join_string + @row_join_string ||= " " + end - attr_writer :row_join_string + attr_writer :row_join_string - def row_join_str_size - row_join_string.size - end + def row_join_str_size + row_join_string.size + end - def col_count_calculate - (line_size_limit + row_join_str_size) / - (items_max_length + row_join_str_size) - end + def col_count_calculate + (line_size_limit + row_join_str_size) / + (items_max_length + row_join_str_size) + end - def col_count - option || col_count_calculate - end + def col_count + option || col_count_calculate + end - def right_padded_items - items.map do |item| - right_pad_field(item, items_max_length) + def right_padded_items + items.map do |item| + right_pad_field(item, items_max_length) + end end - end - def pad_char - " " - end + def pad_char + " " + end - def row_count - (items.count / col_count.to_f).ceil + def row_count + (items.count / col_count.to_f).ceil + end end end diff --git a/test/acceptance/acceptance_test.rb b/test/acceptance/acceptance_test.rb index 6d56195..b77e56d 100644 --- a/test/acceptance/acceptance_test.rb +++ b/test/acceptance/acceptance_test.rb @@ -2,68 +2,70 @@ require "highline/import" -class HighLine::AcceptanceTest - @answers ||= {} +class HighLine + class AcceptanceTest + @answers ||= {} - class << self - attr_reader :answers - end + class << self + attr_reader :answers + end - def self.check - caller_file = File.basename(caller(1..1).first.split(":")[-3]) + def self.check + caller_file = File.basename(caller(1..1).first.split(":")[-3]) - test = new - yield test - test.caller_file = caller_file - test.check - end + test = new + yield test + test.caller_file = caller_file + test.check + end - def self.answers_for_report - answers.map do |file, answer| - "#{file}: #{answer}" - end.join("\n") - end + def self.answers_for_report + answers.map do |file, answer| + "#{file}: #{answer}" + end.join("\n") + end - # A test description to be shown to user. - # It should express what the user is - # expected to check. - attr_accessor :desc + # A test description to be shown to user. + # It should express what the user is + # expected to check. + attr_accessor :desc - # A test action to be checked by the user - attr_accessor :action + # A test action to be checked by the user + attr_accessor :action - # A text asking the confirmation if - # the action worked (y) or not (n). - attr_accessor :question + # A text asking the confirmation if + # the action worked (y) or not (n). + attr_accessor :question - # Automatically filled attribute pointing - # to the file where the current test - # source is located. So we could check - # at the report what tests passed or failed. - attr_accessor :caller_file + # Automatically filled attribute pointing + # to the file where the current test + # source is located. So we could check + # at the report what tests passed or failed. + attr_accessor :caller_file - def check - # Print a header with the test description - puts "=====" - puts " #{caller_file}" - puts "=====" - puts - puts desc + def check + # Print a header with the test description + puts "=====" + puts " #{caller_file}" + puts "=====" + puts + puts desc - # Execute the proc/lambda assigned to action - puts "---" - puts - action.call - puts - puts "---" - puts + # Execute the proc/lambda assigned to action + puts "---" + puts + action.call + puts + puts "---" + puts - # Gather the user feedback about the test - print question - answer = STDIN.gets.chomp - answer = "y" if answer.empty? - HighLine::AcceptanceTest.answers[caller_file] = answer + # Gather the user feedback about the test + print question + answer = STDIN.gets.chomp + answer = "y" if answer.empty? + HighLine::AcceptanceTest.answers[caller_file] = answer - puts + puts + end end end |