Class: Heroku::Helpers::LogDisplayer

Inherits:
Object
  • Object
show all
Includes:
Heroku::Helpers
Defined in:
lib/heroku/helpers/log_displayer.rb

Constant Summary collapse

COLORS =
%w( cyan yellow green magenta red )
COLOR_CODES =
{
  "red"     => 31,
  "green"   => 32,
  "yellow"  => 33,
  "magenta" => 35,
  "cyan"    => 36,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Heroku::Helpers

#action, #ask, #confirm, #confirm_billing, #confirm_command, #create_git_remote, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #error, error_with_failure, error_with_failure=, extended, extended_into, #fail, #format_bytes, #format_date, #format_error, #format_with_bang, #get_terminal_environment, #git, #has_git?, #home_directory, #host_name, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #with_tty

Constructor Details

#initialize(heroku, app, opts) ⇒ LogDisplayer

Returns a new instance of LogDisplayer.



10
11
12
# File 'lib/heroku/helpers/log_displayer.rb', line 10

def initialize(heroku, app, opts)
  @heroku, @app, @opts = heroku, app, opts
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/heroku/helpers/log_displayer.rb', line 8

def app
  @app
end

#herokuObject (readonly)

Returns the value of attribute heroku.



8
9
10
# File 'lib/heroku/helpers/log_displayer.rb', line 8

def heroku
  @heroku
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/heroku/helpers/log_displayer.rb', line 8

def opts
  @opts
end

Instance Method Details

#colorize(chunk) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/heroku/helpers/log_displayer.rb', line 45

def colorize(chunk)
  lines = []
  chunk.split("\n").map do |line|
    if parsed_line = parse_log(line)
      header, identifier, body = parsed_line
      @assigned_colors[identifier] ||= COLORS[@assigned_colors.size % COLORS.size]
      lines << [
        "\e[#{COLOR_CODES[@assigned_colors[identifier]]}m",
        header,
        "\e[0m",
        body,
      ].join("")
    elsif not line.empty?
      lines << line
    end
  end
  lines.join("\n")
end

#display_logsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/heroku/helpers/log_displayer.rb', line 14

def display_logs
  @assigned_colors = {}
  @line_start = true
  @token = nil

  heroku.read_logs(app, opts) do |chunk|
    unless chunk.empty?
      if STDOUT.isatty && ENV.has_key?("TERM")
        display(colorize(chunk))
      else
        display(chunk)
      end
    end
  end
rescue Errno::EPIPE
rescue Interrupt => interrupt
  if STDOUT.isatty && ENV.has_key?("TERM")
    display("\e[0m")
  end
  raise(interrupt)
end

#parse_log(log) ⇒ Object



64
65
66
67
# File 'lib/heroku/helpers/log_displayer.rb', line 64

def parse_log(log)
  return unless parsed = log.match(/^(.*?\[(\w+)([\d\.]+)?\]:)(.*)?$/)
  [1, 2, 4].map { |i| parsed[i] }
end