Class: Tinylinks::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/tinylinks/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(color: false) ⇒ Formatter

Returns a new instance of Formatter.



5
6
7
# File 'lib/tinylinks/formatter.rb', line 5

def initialize(color: false)
  @c = Colorizer.new(enabled: color)
end

Instance Method Details

#errors(data) ⇒ Object



33
34
35
36
37
# File 'lib/tinylinks/formatter.rb', line 33

def errors(data)
  data["errors"].flat_map do |field, messages|
    messages.map { |msg| @c.red("#{field} #{msg}") }
  end.join("\n")
end


9
10
11
12
13
14
15
16
17
# File 'lib/tinylinks/formatter.rb', line 9

def link(data)
  lines = []
  lines << @c.bold("#{data["title"] || "(untitled)"} [##{data["id"]}]")
  lines << "  #{@c.cyan(data["url"])}"
  lines << "  #{@c.dim(data["description"])}" if data["description"] && !data["description"].empty?
  meta = meta_line(data)
  lines << "  #{meta}" if meta
  lines.join("\n")
end


19
20
21
22
23
24
25
# File 'lib/tinylinks/formatter.rb', line 19

def link_list(data)
  return @c.dim("No results found") if data["links"].empty?

  lines = data["links"].map { |l| link(l) }
  lines << @c.dim(pagination(data["meta"])) if data["meta"]
  lines.join("\n\n")
end

#tags(data) ⇒ Object



27
28
29
30
31
# File 'lib/tinylinks/formatter.rb', line 27

def tags(data)
  return @c.dim("No results found") if data["tags"].empty?

  data["tags"].map { |t| "#{@c.green(t["name"])} #{@c.dim("(#{t["count"]})")}" }.join("\n")
end