Class: Megam::Text
- Inherits:
-
Object
- Object
- Megam::Text
- Defined in:
- lib/megam/core/text.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #agree(*args, &block) ⇒ Object
- #ask(*args, &block) ⇒ Object
- #ask_question(question, opts = {}) ⇒ Object
- #color(string, *colors) ⇒ Object
-
#color? ⇒ Boolean
Should colored output be used? For output to a terminal, this is determined by the value of ‘config`.
-
#err(message) ⇒ Object
Prints a msg to stderr.
-
#error(message) ⇒ Object
Print an error message.
-
#fatal(message) ⇒ Object
Print a message describing a fatal error.
- #highline ⇒ Object
-
#initialize(stdout, stderr, stdin, config) ⇒ Text
constructor
A new instance of Text.
- #list(*args) ⇒ Object
-
#msg(message) ⇒ Object
(also: #info)
Prints a message to stdout.
-
#output(data) ⇒ Object
Formats
data
using the configured presenter and outputs the result viamsg
. - #pretty_print(data) ⇒ Object
-
#warn(message) ⇒ Object
Print a warning message.
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
23 24 25 |
# File 'lib/megam/core/text.rb', line 23 def config @config end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
21 22 23 |
# File 'lib/megam/core/text.rb', line 21 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
22 23 24 |
# File 'lib/megam/core/text.rb', line 22 def stdin @stdin end |
#stdout ⇒ Object (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
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() stderr.puts end |
#error(message) ⇒ Object
Print an error message
56 57 58 |
# File 'lib/megam/core/text.rb', line 56 def error() err("#{color('ERROR:', :red, :bold)} #{}") end |
#fatal(message) ⇒ Object
Print a message describing a fatal error.
61 62 63 |
# File 'lib/megam/core/text.rb', line 61 def fatal() err("#{color('FATAL:', :red, :bold)} #{}") end |
#highline ⇒ Object
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() stdout.puts 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() err("#{color('WARNING:', :yellow, :bold)} #{}") end |