Class: Coral::Util::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/coral_core/util/interface.rb

Constant Summary collapse

COLORS =

{
  :clear => "\e[0m",
  :red => "\e[31m",
  :green => "\e[32m",
  :yellow => "\e[33m"
}
COLOR_MAP =
{
  :warn => COLORS[:yellow],
  :error => COLORS[:red],
  :success => COLORS[:green]
}
@@logger =

Properties

Log4r::Logger.new("coral::interface")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Interface


Constructor



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coral_core/util/interface.rb', line 30

def initialize(options = {})
  class_name = self.class.to_s.downcase
  
  if options.is_a?(String)
    options = { :resource => options, :logger => options }
  end
  config = Config.ensure(options)
  
  if config.get(:logger, false)
    if config[:logger].is_a?(String)
      @logger = Log4r::Logger.new(config[:logger])
    else
      @logger = config[:logger]
    end
  else
    @logger = Log4r::Logger.new(class_name)
  end
  
  @resource = config.get(:resource, '')
  @color = config.get(:color, true)
  
  @printer = config.get(:printer, :puts)
  
  @input = config.get(:input, $stdin)
  @output = config.get(:output, $stdout)
  @error = config.get(:error, $stderr)
  
  @delegate = config.get(:ui_delegate, nil)
end

Instance Attribute Details

#colorObject


Accessors / Modifiers



69
70
71
# File 'lib/coral_core/util/interface.rb', line 69

def color
  @color
end

#delegateObject


Accessors / Modifiers



69
70
71
# File 'lib/coral_core/util/interface.rb', line 69

def delegate
  @delegate
end

#error(message, *args) ⇒ Object




69
70
71
# File 'lib/coral_core/util/interface.rb', line 69

def error
  @error
end

#inputObject


Accessors / Modifiers



69
70
71
# File 'lib/coral_core/util/interface.rb', line 69

def input
  @input
end

#loggerObject


Accessors / Modifiers



69
70
71
# File 'lib/coral_core/util/interface.rb', line 69

def logger
  @logger
end

#outputObject


Accessors / Modifiers



69
70
71
# File 'lib/coral_core/util/interface.rb', line 69

def output
  @output
end

#resourceObject


Accessors / Modifiers



69
70
71
# File 'lib/coral_core/util/interface.rb', line 69

def resource
  @resource
end

Class Method Details

.loggerObject




73
74
75
# File 'lib/coral_core/util/interface.rb', line 73

def self.logger
  return @@logger
end

Instance Method Details

#ask(message, options = {}) ⇒ Object




94
95
96
97
98
99
100
101
102
# File 'lib/coral_core/util/interface.rb', line 94

def ask(message, options = {})
  return @delegate.ask(message, options) if check_delegate('ask')

  options[:new_line] = false if ! options.has_key?(:new_line)
  options[:prefix] = false if ! options.has_key?(:prefix)

  say(:info, message, options)
  return @input.gets.chomp
end

#check_delegate(method) ⇒ Object




185
186
187
# File 'lib/coral_core/util/interface.rb', line 185

def check_delegate(method)
  return ( @delegate && @delegate.respond_to?(method.to_s) )
end

#format_message(type, message, options = {}) ⇒ Object


Utilities



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/coral_core/util/interface.rb', line 143

def format_message(type, message, options = {})
  return @delegate.format_message(type, message, options) if check_delegate('format_message')
  
  if @resource && ! @resource.empty? && options[:prefix]
    prefix = "[#{@resource}]"
  end
  message = "#{prefix} #{message}".strip
  
  if @color
    if options.has_key?(:color)
      color = COLORS[options[:color]]
      message = "#{color}#{message}#{COLORS[:clear]}"
    else
      message = "#{COLOR_MAP[type]}#{message}#{COLORS[:clear]}" if COLOR_MAP[type]
    end
  end
  return message
end

#info(message, *args) ⇒ Object




106
107
108
109
110
111
# File 'lib/coral_core/util/interface.rb', line 106

def info(message, *args)
  @logger.info("info: #{message}")
  
  return @delegate.info(message, *args) if check_delegate('info')
  say(:info, message, *args)
end

#inspectObject




62
63
64
# File 'lib/coral_core/util/interface.rb', line 62

def inspect
  "#<#{self.class}: #{@resource}>"
end

#safe_puts(message = nil, options = {}) ⇒ Object




164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/coral_core/util/interface.rb', line 164

def safe_puts(message = nil, options = {})
  return @delegate.safe_puts(message, options) if check_delegate('safe_puts')
  
  #dbg(message, 'message')
  #dbg(options, 'options')
  
  message ||= ""
  options = {
    :channel => @output,
    :printer => @printer,
  }.merge(options)

  begin
    options[:channel].send(options[:printer], message)
  rescue Errno::EPIPE
    return
  end
end

#say(type, message, options = {}) ⇒ Object


UI functionality



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coral_core/util/interface.rb', line 80

def say(type, message, options = {})
  return @delegate.say(type, message, options) if check_delegate('say')

  defaults = { :new_line => true, :prefix => true }
  options = defaults.merge(options)
  printer = options[:new_line] ? :puts : :print
  channel = type == :error || options[:channel] == :error ? @error : @output

  safe_puts(format_message(type, message, options),
            :channel => channel, :printer => printer)
end

#success(message, *args) ⇒ Object




133
134
135
136
137
138
# File 'lib/coral_core/util/interface.rb', line 133

def success(message, *args)
  @logger.info("success: #{message}")
  
  return @delegate.success(message, *args) if check_delegate('success')
  say(:success, message, *args)
end

#warn(message, *args) ⇒ Object




115
116
117
118
119
120
# File 'lib/coral_core/util/interface.rb', line 115

def warn(message, *args)
  @logger.info("warn: #{message}")
  
  return @delegate.warn(message, *args) if check_delegate('warn')
  say(:warn, message, *args)
end