lib/git_trend/formatters/text_formatter.rb in git-trend-1.3.0 vs lib/git_trend/formatters/text_formatter.rb in git-trend-1.4.0
- old
+ new
@@ -1,11 +1,11 @@
require "mb_string"
module GitTrend::Formatters
class TextFormatter
- HEADER_COLUMNS = %w(no. name lang star description)
- DEFAULT_COLUMNS_SIZES = [3, 40, 10, 6, 20]
+ HEADER_COLUMNS = %w[no. name lang star description].freeze
+ DEFAULT_COLUMNS_SIZES = [3, 40, 10, 6, 20].freeze
def print(projects, options)
if projects.empty?
render_zero
return
@@ -25,10 +25,11 @@
puts "if languages is unknown, you can specify 'unkown'."
puts
end
private
+
def render_zero
puts "It looks like we don’t have any trending repositories."
puts
end
@@ -40,11 +41,11 @@
@columns_sizes.pop unless @enable_description
end
def rule_max_description_size
terminal_width, _terminal_height = detect_terminal_size
- description_width = terminal_width - @columns_sizes[0..-2].inject(&:+) - (@columns_sizes.size - 1)
+ description_width = terminal_width - @columns_sizes[0..-2].sum - (@columns_sizes.size - 1)
if description_width >= DEFAULT_COLUMNS_SIZES.last
@columns_sizes[-1] = description_width
else
@enable_description = false
end
@@ -69,10 +70,11 @@
puts (fmt % header).rstrip
puts fmt % @columns_sizes.map { |column| "-" * column }
end
+ # rubocop:disable Metrics/AbcSize
def render_body(projects)
f = @columns_sizes
fmt = "%#{f[0]}s %-#{f[1]}s %-#{f[2]}s %#{f[3]}s"
description_fmt = ""
projects.each_with_index do |project, i|
@@ -85,16 +87,18 @@
end
result = "#{fmt}#{description_fmt}" % data
puts result.rstrip
end
end
+ # rubocop:enable Metrics/AbcSize
def render_footer
puts
end
# https://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb#L61-71
+ # rubocop:disable all
def detect_terminal_size
if (ENV["COLUMNS"] =~ /^\d+$/) && (ENV["LINES"] =~ /^\d+$/)
[ENV["COLUMNS"].to_i, ENV["LINES"].to_i]
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV["TERM"])) && command_exists?("tput")
[`tput cols`.to_i, `tput lines`.to_i]
@@ -102,9 +106,10 @@
`stty size`.scan(/\d+/).map(&:to_i).reverse
end
rescue
nil
end
+ # rubocop:enable all
def command_exists?(command)
ENV["PATH"].split(File::PATH_SEPARATOR).any? { |d| File.exist? File.join(d, command) }
end
end