class GitTrend::Formatters::TextFormatter

Constants

DEFAULT_COLUMNS_SIZES
HEADER_COLUMNS

Public Instance Methods

print(projects, options) click to toggle source
print_languages(languages) click to toggle source

Private Instance Methods

command_exists?(command) click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 108
def command_exists?(command)
  ENV["PATH"].split(File::PATH_SEPARATOR).any? { |d| File.exist? File.join(d, command) }
end
detect_terminal_size() click to toggle source

github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb#L61-71

# File lib/git_trend/formatters/text_formatter.rb, line 96
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]
  elsif STDIN.tty? && command_exists?("stty")
    `stty size`.scan(/\d+/).map(&:to_i).reverse
  end
rescue
  nil
end
max_size_of(projects, attr) click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 59
def max_size_of(projects, attr)
  projects.max_by { |project| project.send(attr).size }.send(attr).size
end
render_body(projects) click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 74
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|
    data = [i + 1, [project.name, project.lang, project.star_count.to_s]].flatten
    if @enable_description
      description = project.description.mb_truncate(f.last)
      data << description
      mb_char_size = description.display_width - description.size
      description_fmt = " %-#{f.last - mb_char_size}s"
    end
    result = "#{fmt}#{description_fmt}" % data
    puts result.rstrip
  end
end
render_header() click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 63
def render_header
  header = HEADER_COLUMNS.map(&:capitalize)
  header.pop unless @enable_description
  f = @columns_sizes
  fmt = "%#{f[0]}s %-#{f[1]}s %-#{f[2]}s %#{f[3]}s"
  fmt << " %-#{f[4]}s" if @enable_description

  puts (fmt % header).rstrip
  puts fmt % @columns_sizes.map { |column| "-" * column }
end
render_zero() click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 30
def render_zero
  puts "It looks like we don’t have any trending repositories."
  puts
end
rule_columns_sizes(projects) click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 35
def rule_columns_sizes(projects)
  @columns_sizes = DEFAULT_COLUMNS_SIZES.dup
  rule_max_column_size(projects, :name)
  rule_max_column_size(projects, :lang)
  rule_max_description_size if @enable_description
  @columns_sizes.pop unless @enable_description
end
rule_max_column_size(projects, attr) click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 53
def rule_max_column_size(projects, attr)
  index = HEADER_COLUMNS.index(attr.to_s)
  max_size = max_size_of(projects, attr)
  @columns_sizes[index] = max_size if max_size > @columns_sizes[index]
end
rule_max_description_size() click to toggle source
# File lib/git_trend/formatters/text_formatter.rb, line 43
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)
  if description_width >= DEFAULT_COLUMNS_SIZES.last
    @columns_sizes[-1] = description_width
  else
    @enable_description = false
  end
end