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.



25
26
27
# File 'lib/megam/core/text.rb', line 25

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.



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

def config
  @config
end

#stderrObject (readonly)

Returns the value of attribute stderr.



21
22
23
# File 'lib/megam/core/text.rb', line 21

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



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

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



20
21
22
# File 'lib/megam/core/text.rb', line 20

def stdout
  @stdout
end

Instance Method Details

#agree(*args, &block) ⇒ Object



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

def agree(*args, &block)
  highline.agree(*args, &block)
end

#ask(*args, &block) ⇒ Object



85
86
87
# File 'lib/megam/core/text.rb', line 85

def ask(*args, &block)
  highline.ask(*args, &block)
end

#ask_question(question, opts = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/megam/core/text.rb', line 100

def ask_question(question, opts={})
  question = question + "[#{opts[:default]}] " if opts[:default]

  if opts[:default] and config[:defaults]
    opts[:default]
  else
    stdout.print question
    a = stdin.readline.strip

    if opts[:default]
      a.empty? ? opts[:default] : a
    else
    a
    end
  end
end

#color(string, *colors) ⇒ Object



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

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

#color?Boolean

Should colored output be used? For output to a terminal, this is determined by the value of ‘config`. When output is not to a terminal, colored output is never used

Returns:

  • (Boolean)


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

def color?
  ##Chef::Config[:color] && stdout.tty? && !Chef::Platform.windows?
  :red
end

#err(message) ⇒ Object

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



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

def err(message)
  stderr.puts message
end

#error(message) ⇒ Object

Print an error message



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

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

#fatal(message) ⇒ Object

Print a message describing a fatal error.



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

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

#highlineObject



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

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

#list(*args) ⇒ Object



89
90
91
# File 'lib/megam/core/text.rb', line 89

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

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

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



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

def msg(message)
  stdout.puts message
end

#output(data) ⇒ Object

Formats data using the configured presenter and outputs the result via msg. Formatting can be customized by configuring a different presenter. See use_presenter



96
97
98
# File 'lib/megam/core/text.rb', line 96

def output(data)
  msg @presenter.format(data)
end

#pretty_print(data) ⇒ Object



117
118
119
# File 'lib/megam/core/text.rb', line 117

def pretty_print(data)
  stdout.puts data
end

#warn(message) ⇒ Object

Print a warning message



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

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