File: cli.rb

package info (click to toggle)
ruby-foreman 0.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 584 kB
  • sloc: ruby: 2,020; sh: 88; makefile: 8
file content (105 lines) | stat: -rw-r--r-- 2,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require "foreman/engine"

class Foreman::Engine::CLI < Foreman::Engine

  module Color

    ANSI = {
      :reset          => 0,
      :black          => 30,
      :red            => 31,
      :green          => 32,
      :yellow         => 33,
      :blue           => 34,
      :magenta        => 35,
      :cyan           => 36,
      :white          => 37,
      :bright_black   => 30,
      :bright_red     => 31,
      :bright_green   => 32,
      :bright_yellow  => 33,
      :bright_blue    => 34,
      :bright_magenta => 35,
      :bright_cyan    => 36,
      :bright_white   => 37,
    }

    def self.enable(io, force=false)
      io.extend(self)
      @@color_force = force
    end

    def color?
      return true if @@color_force
      return false if Foreman.windows?
      return false unless self.respond_to?(:isatty)
      self.isatty && ENV["TERM"]
    end

    def color(name)
      return "" unless color?
      return "" unless ansi = ANSI[name.to_sym]
      "\e[#{ansi}m"
    end

  end

  FOREMAN_COLORS = %w( cyan yellow green magenta red blue bright_cyan bright_yellow
                       bright_green bright_magenta bright_red bright_blue )

  def startup
    @colors = map_colors
    proctitle "foreman: main" unless Foreman.windows?
    Color.enable($stdout, options[:color])
  end

  def output(name, data)
    data.to_s.lines.map(&:chomp).each do |message|
      output  = ""
      output += $stdout.color(@colors[name.split(".").first].to_sym)
      output += "#{Time.now.strftime("%H:%M:%S")} " if options[:timestamp]
      output += "#{pad_process_name(name)} | "
      output += $stdout.color(:reset)
      output += message
      $stdout.puts output
      $stdout.flush
    end
  rescue Errno::EPIPE
    terminate_gracefully
  end

  def shutdown
  end

private

  def name_padding
    @name_padding ||= begin
      index_padding = @names.values.map { |n| formation[n] }.max.to_s.length + 1
      name_padding  = @names.values.map { |n| n.length + index_padding }.sort.last
      [ 6, name_padding.to_i ].max
    end
  end

  def pad_process_name(name)
    name.ljust(name_padding, " ")
  end

  def map_colors
    colors = Hash.new("white")
    @names.values.each_with_index do |name, index|
      colors[name] = FOREMAN_COLORS[index % FOREMAN_COLORS.length]
    end
    colors["system"] = "bright_white"
    colors
  end

  def proctitle(title)
    $0 = title
  end

  def termtitle(title)
    printf("\033]0;#{title}\007") unless Foreman.windows?
  end

end