Class: GithubCLI::Formatter

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

Overview

This is the main entry point for formatting output. It delegates to other objects like Formatter::Table to perform actual rendering.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, options = {}) ⇒ Formatter


10
11
12
13
14
15
# File 'lib/github_cli/formatter.rb', line 10

def initialize(response, options={})
  @response = response
  @message  = options[:message]
  @format   = options[:format]
  @quiet    = options[:quiet]
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format


8
9
10
# File 'lib/github_cli/formatter.rb', line 8

def format
  @format
end

#messageObject (readonly)

Returns the value of attribute message


8
9
10
# File 'lib/github_cli/formatter.rb', line 8

def message
  @message
end

#quietObject (readonly)

Returns the value of attribute quiet


8
9
10
# File 'lib/github_cli/formatter.rb', line 8

def quiet
  @quiet
end

#responseObject (readonly)

Returns the value of attribute response


8
9
10
# File 'lib/github_cli/formatter.rb', line 8

def response
  @response
end

Instance Method Details

#determine_output_formatterObject


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/github_cli/formatter.rb', line 25

def determine_output_formatter
  case format.to_s
  when 'table', /table:v.*/, /table:h.*/
    if response.body && !response.body.empty?
      formatter = Formatters::Table.new(response.body,
                    :transform => format.to_s.split(':').last)
      formatter.format
    end
  when 'csv'
    formatter = Formatters::CSV.new(response)
    formatter.format
  when 'json'
    'json output'
  else
    raise UnknownFormatError, format
  end
end

#render_messageObject


51
52
53
54
55
# File 'lib/github_cli/formatter.rb', line 51

def render_message
  if message
    Terminal.line message
  end
end

#render_outputObject


17
18
19
20
21
22
23
# File 'lib/github_cli/formatter.rb', line 17

def render_output
  render_status
  return if quiet
  Terminal.paged_output
  determine_output_formatter
  render_message
end

#render_statusObject

Render status code


44
45
46
47
48
49
# File 'lib/github_cli/formatter.rb', line 44

def render_status
  if response.respond_to? :status
    Terminal.line "Response Status: #{response.status}\n"
    Terminal.newline
  end
end