Class: Morpheus::Logging::DarkPrinter

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/morpheus/logging.rb

Overview

An IO class for printing debugging info This is used as a proxy for ::RestClient.log printing right now.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, color = nil, is_dark = true) ⇒ DarkPrinter

Returns a new instance of DarkPrinter.



134
135
136
137
138
# File 'lib/morpheus/logging.rb', line 134

def initialize(io, color=nil, is_dark=true)
  @io = io # || $stdout
  @color = color # || cyan
  @is_dark = is_dark
end

Instance Attribute Details

#colorObject

String

ansi color code for output. Default is dark



115
116
117
# File 'lib/morpheus/logging.rb', line 115

def color
  @color
end

#ioObject

IO

to write to



112
113
114
# File 'lib/morpheus/logging.rb', line 112

def io
  @io
end

Class Method Details

.<<(*messages) ⇒ Object



130
131
132
# File 'lib/morpheus/logging.rb', line 130

def self.<<(*messages)
  instance.<<(*messages)
end

.instanceObject

DarkPrinter with io STDOUT



118
119
120
# File 'lib/morpheus/logging.rb', line 118

def self.instance
  @instance ||= self.new(STDOUT, nil, true)
end


122
123
124
# File 'lib/morpheus/logging.rb', line 122

def self.print(*messages)
  instance.print(*messages)
end

.puts(*messages) ⇒ Object



126
127
128
# File 'lib/morpheus/logging.rb', line 126

def self.puts(*messages)
  instance.puts(*messages)
end

Instance Method Details

#<<(*messages) ⇒ Object



178
179
180
# File 'lib/morpheus/logging.rb', line 178

def <<(*messages)
  print(*messages)
end


156
157
158
159
160
161
162
163
164
165
# File 'lib/morpheus/logging.rb', line 156

def print(*messages)
  if @io
    messages = messages.flatten.collect {|it| scrub_message(it) }
    print_with_color do 
      messages.each do |msg|
        @io.print msg
      end
    end
  end
end


144
145
146
147
148
149
150
151
152
153
154
# File 'lib/morpheus/logging.rb', line 144

def print_with_color(&block)
  if Term::ANSIColor.coloring?
    @io.print Term::ANSIColor.reset
    @io.print @color if @color
    @io.print Term::ANSIColor.dark if @is_dark
  end
  yield
  if Term::ANSIColor.coloring?
    @io.print Term::ANSIColor.reset
  end
end

#puts(*messages) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/morpheus/logging.rb', line 167

def puts(*messages)
  if @io
    messages = messages.flatten.collect {|it| scrub_message(it) }
    print_with_color do 
      messages.each do |msg|
        @io.puts msg
      end
    end
  end
end

#scrub_message(msg) ⇒ Object



140
141
142
# File 'lib/morpheus/logging.rb', line 140

def scrub_message(msg)
  Morpheus::Logging.scrub_message(msg)
end