Class: ConsoleFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/formatters/console_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose) ⇒ ConsoleFormatter

Returns a new instance of ConsoleFormatter.



6
7
8
9
10
# File 'lib/hiptest-publisher/formatters/console_formatter.rb', line 6

def initialize(verbose)
  @verbose = verbose
  @immediate_verbose = true
  @verbose_messages = []
end

Instance Attribute Details

#verboseObject (readonly)

Returns the value of attribute verbose.



4
5
6
# File 'lib/hiptest-publisher/formatters/console_formatter.rb', line 4

def verbose
  @verbose
end

Instance Method Details

#dump_error(error, message = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/hiptest-publisher/formatters/console_formatter.rb', line 12

def dump_error(error, message = nil)
  return unless verbose
  puts message.blue if message
  line = "-" * 80
  puts line.yellow
  puts "#{error.class.name}: #{error.message}".red
  puts "#{error.backtrace.map {|l| "  #{l}\n"}.join}".yellow
  puts line.yellow
end

#show_options(options, message = nil) ⇒ Object



22
23
24
25
26
27
# File 'lib/hiptest-publisher/formatters/console_formatter.rb', line 22

def show_options(options, message = nil)
  return unless verbose
  message ||= "Running Hiptest-publisher #{hiptest_publisher_version} with:"
  puts message.yellow
  options.each { |k, v| puts " - #{k}: #{v.inspect}" }
end

#show_status_message(message, status = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hiptest-publisher/formatters/console_formatter.rb', line 38

def show_status_message(message, status=nil)
  status_icon = " "
  output = STDOUT

  if status == :success
    status_icon = "v".green
  elsif status == :warning
    status_icon = "?".yellow
  elsif status == :failure
    status_icon = "x".red
    output = STDERR
  end
  if status
    @immediate_verbose = true
    cursor_offset = ""
  else
    return unless $stdout.tty?
    rows, columns = IO.console.winsize
    return if columns == 0
    @immediate_verbose = false
    vertical_offset = (4 + message.length) / columns
    cursor_offset = "\r\e[#{vertical_offset + 1}A"
  end

  output.print "[#{status_icon}] #{message}#{cursor_offset}\n"

  if @immediate_verbose && !@verbose_messages.empty?
    @verbose_messages.each { |message| show_verbose_message(message) }
    @verbose_messages.clear
  end
end

#show_verbose_message(message) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/hiptest-publisher/formatters/console_formatter.rb', line 29

def show_verbose_message(message)
  return unless verbose
  if @immediate_verbose
    STDOUT.print "#{message}\n"
  else
    @verbose_messages << message
  end
end