Class: Megam::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/megam/core/text.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout, stderr, stdin, config) ⇒ Text

Returns a new instance of Text.



10
11
12
# File 'lib/megam/core/text.rb', line 10

def initialize(stdout, stderr, stdin, config)
    @stdout, @stderr, @stdin, @config = stdout, stderr, stdin, config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/megam/core/text.rb', line 8

def config
  @config
end

#stderrObject (readonly)

Returns the value of attribute stderr.



6
7
8
# File 'lib/megam/core/text.rb', line 6

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



7
8
9
# File 'lib/megam/core/text.rb', line 7

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/megam/core/text.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#color(string, *colors) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/megam/core/text.rb', line 64

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

#color?Boolean

Should colored output be used ?. When output is not to a terminal, colored output is never used

Returns:

  • (Boolean)


74
75
76
# File 'lib/megam/core/text.rb', line 74

def color?
    stdout.tty?
end

#err(message) ⇒ Object

Prints a msg to stderr. Used for warn, error, and fatal.



45
46
47
# File 'lib/megam/core/text.rb', line 45

def err(message)
    stderr.puts message
end

#error(message) ⇒ Object

Print an error message



55
56
57
# File 'lib/megam/core/text.rb', line 55

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

#fatal(message) ⇒ Object

Print a message describing a fatal error.



60
61
62
# File 'lib/megam/core/text.rb', line 60

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

#highlineObject



14
15
16
17
18
19
# File 'lib/megam/core/text.rb', line 14

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

#info(message) ⇒ Object

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



40
41
42
# File 'lib/megam/core/text.rb', line 40

def info(message)
    stdout.puts("#{color('INFO:', :green, :bold)} #{message}")
end

#list(*args) ⇒ Object



78
79
80
# File 'lib/megam/core/text.rb', line 78

def list(*args)
    highline.list(*args)
end

#msg(message) ⇒ Object



33
34
35
# File 'lib/megam/core/text.rb', line 33

def msg(message)
    stdout.puts message
end

#pretty_print(data) ⇒ Object



82
83
84
# File 'lib/megam/core/text.rb', line 82

def pretty_print(data)
    stdout.puts data
end

#summarize(data) ⇒ Object

Summarize the data. Defaults to text format output, which may not be very summary-like



23
24
25
# File 'lib/megam/core/text.rb', line 23

def summarize(data)
    text_format(data)
end

#text_format(data) ⇒ Object

Converts the data to a String in the text format. Uses Chef::Knife::Core::TextFormatter



29
30
31
# File 'lib/megam/core/text.rb', line 29

def text_format(data)
    Megam::TextFormatter.new(data, self).formatted_data
end

#warn(message) ⇒ Object

Print a warning message



50
51
52
# File 'lib/megam/core/text.rb', line 50

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