Class: Printer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = STDOUT, stderr = STDERR, stdin = STDIN) ⇒ Printer

Returns a new instance of Printer.



6
7
8
# File 'lib/printer.rb', line 6

def initialize(stdout=STDOUT, stderr=STDERR, stdin=STDIN)
  @stdout, @stderr, @stdin = stdout, stderr, stdin
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



3
4
5
# File 'lib/printer.rb', line 3

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



4
5
6
# File 'lib/printer.rb', line 4

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



2
3
4
# File 'lib/printer.rb', line 2

def stdout
  @stdout
end

Instance Method Details

#color(string, *colors) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/printer.rb', line 41

def color(string, *colors)
  if color?
    highline.color(string, *colors)
  else
    string
  end
end

#color?Boolean

Should colored output be used? Only on TTY

Returns:

  • (Boolean)


50
51
52
# File 'lib/printer.rb', line 50

def color?
  true
end

#error(message) ⇒ Object

Print an error message



32
33
34
# File 'lib/printer.rb', line 32

def error(message)
  msg("#{color('ERROR:', :red, :bold)} #{message}")
end

#fatal(message) ⇒ Object

Print a message describing a fatal error.



37
38
39
# File 'lib/printer.rb', line 37

def fatal(message)
  msg("#{color('FATAL:', :red, :bold)} #{message}")
end

#highlineObject



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

def highline
  @highline ||= begin
    require 'highline'
    HighLine.new
  end
end

#msg(message) ⇒ Object Also known as: info

Prints a message to stdout. Aliased as info for compatibility with the logger API.



19
20
21
22
# File 'lib/printer.rb', line 19

def msg(message)
  stdout.puts message
  stdout.flush
end

#warn(message) ⇒ Object

Print a warning message



27
28
29
# File 'lib/printer.rb', line 27

def warn(message)
  msg("#{color('WARNING:', :yellow, :bold)} #{message}")
end