Class: AngryMob::UI

Inherits:
Object show all
Defined in:
lib/angry_mob/ui.rb

Constant Summary collapse

CLEAR =

Embed in a String to clear all previous ANSI sequences.

"\e[0m"
BOLD =

The start of an ANSI bold sequence.

"\e[1m"
BLACK =

Set the terminal’s foreground ANSI color to black.

"\e[30m"
RED =

Set the terminal’s foreground ANSI color to red.

"\e[31m"
GREEN =

Set the terminal’s foreground ANSI color to green.

"\e[32m"
YELLOW =

Set the terminal’s foreground ANSI color to yellow.

"\e[33m"
BLUE =

Set the terminal’s foreground ANSI color to blue.

"\e[34m"
MAGENTA =

Set the terminal’s foreground ANSI color to magenta.

"\e[35m"
CYAN =

Set the terminal’s foreground ANSI color to cyan.

"\e[36m"
WHITE =

Set the terminal’s foreground ANSI color to white.

"\e[37m"
GRAY =
"#{BOLD}#{BLACK}"
BRIGHT_WHITE =
"#{BOLD}#{WHITE}"
ON_BLACK =

Set the terminal’s background ANSI color to black.

"\e[40m"
ON_RED =

Set the terminal’s background ANSI color to red.

"\e[41m"
ON_GREEN =

Set the terminal’s background ANSI color to green.

"\e[42m"
ON_YELLOW =

Set the terminal’s background ANSI color to yellow.

"\e[43m"
ON_BLUE =

Set the terminal’s background ANSI color to blue.

"\e[44m"
ON_MAGENTA =

Set the terminal’s background ANSI color to magenta.

"\e[45m"
ON_CYAN =

Set the terminal’s background ANSI color to cyan.

"\e[46m"
ON_WHITE =

Set the terminal’s background ANSI color to white.

"\e[47m"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, min_level = 0, stack = []) ⇒ UI

Returns a new instance of UI.



49
50
51
52
53
54
# File 'lib/angry_mob/ui.rb', line 49

def initialize(options={}, min_level=0, stack=[])
  @options = options
  @stack = stack
  @min_level = @level = min_level
  @colour = CLEAR
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



47
48
49
# File 'lib/angry_mob/ui.rb', line 47

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message.



47
48
49
# File 'lib/angry_mob/ui.rb', line 47

def message
  @message
end

#resultObject (readonly)

Returns the value of attribute result.



47
48
49
# File 'lib/angry_mob/ui.rb', line 47

def result
  @result
end

#stackObject (readonly)

Returns the value of attribute stack.



47
48
49
# File 'lib/angry_mob/ui.rb', line 47

def stack
  @stack
end

Class Method Details

.silence(&block) ⇒ Object



107
108
109
110
111
112
# File 'lib/angry_mob/ui.rb', line 107

def self.silence(&block)
  old_silence,@silence = @silence,true
  yield
ensure
  @silence = old_silence
end

.silence?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/angry_mob/ui.rb', line 113

def self.silence?
  @silence
end

Instance Method Details

#benchmark?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/angry_mob/ui.rb', line 156

def benchmark?
  @benchmark ||= !(FalseClass === @options[:benchmark])
end

#colourise(string, colour, bold = false) ⇒ Object



62
63
64
65
66
# File 'lib/angry_mob/ui.rb', line 62

def colourise(string, colour, bold=false)
  color = self.class.const_get(colour.to_s.upcase) if colour.is_a?(Symbol)
  bold  = bold ? BOLD : ""
  "#{bold}#{color}#{string}#{CLEAR}"
end

#currentObject

def self.stack; @stack ||= [] end def stack; self.class.stack end



58
59
60
# File 'lib/angry_mob/ui.rb', line 58

def current
  stack.last || self
end

#debug(message) ⇒ Object



152
153
154
# File 'lib/angry_mob/ui.rb', line 152

def debug(message)
  say spaces+message, :gray if debug?
end

#debug?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/angry_mob/ui.rb', line 149

def debug?
  @debug ||= !(FalseClass === @options[:debug])
end

#error(message) ⇒ Object Also known as: bad



144
145
146
# File 'lib/angry_mob/ui.rb', line 144

def error(message)
  say spaces+message, :red
end

#exception!(ex) ⇒ Object



222
223
224
225
226
227
228
229
# File 'lib/angry_mob/ui.rb', line 222

def exception!(ex)
  msg = "[#{ex.class}] - #{ex.message}"
  say spaces+msg, :red
  # backtrace

  @result = ex
  @message = msg
end

#good(message) ⇒ Object



127
128
129
# File 'lib/angry_mob/ui.rb', line 127

def good(message)
  say spaces+message, :green
end

#indentObject



98
99
100
# File 'lib/angry_mob/ui.rb', line 98

def indent
  @level += 1
end

#info(message) ⇒ Object



123
124
125
# File 'lib/angry_mob/ui.rb', line 123

def info(message)
  say spaces+message, :bright_white
end

#isay(message, colour = nil, force_new_line = (message.to_s !~ /( |\t)$/)) ⇒ Object



86
87
88
# File 'lib/angry_mob/ui.rb', line 86

def isay(message, colour=nil, force_new_line=(message.to_s !~ /( |\t)$/))
  say spaces+message, colour, force_new_line
end

#log(message) ⇒ Object



136
137
138
# File 'lib/angry_mob/ui.rb', line 136

def log(message)
  say spaces+indent_string(message, @level+1), :white
end

#newlineObject



90
91
92
# File 'lib/angry_mob/ui.rb', line 90

def newline
  $stdout.puts "\n" unless self.class.silence?
end

#ok!(message = nil) ⇒ Object Also known as: changed!



231
232
233
234
# File 'lib/angry_mob/ui.rb', line 231

def ok!(message=nil)
  @result = :ok
  @message = message
end

#outdentObject



102
103
104
105
# File 'lib/angry_mob/ui.rb', line 102

def outdent
  @level -= 1
  @level = @min_level if @level < @min_level
end

#point(message) ⇒ Object



140
141
142
# File 'lib/angry_mob/ui.rb', line 140

def point(message)
  say spaces+"#{message}", :blue
end

#push(message, opts = {}, &block) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/angry_mob/ui.rb', line 164

def push(message,opts={},&block)
  start_time = Time.now
  start! message
  subui = self.class.new(@options, @level+1,stack)

  stack.push subui

  begin
    yield
  rescue Object
    if opts[:bubble]
      raise $!
    else
      subui.exception!($!)
    end
  end
ensure
  stack.pop

  say spaces+'}', :yellow, false

  colour = nil

  case subui.result
  when :ok
    colour = :green
    say "#{message}", :green, false
    say " (#{subui.message})", :green, false if subui.message

  when :skip
    colour = :blue
    say "#{message}", :blue, false
    say " (#{subui.message})", :blue, false if subui.message

  when Exception
    colour = :red
    say "#{message}", :red, false
    say " (#{subui.message})", :red, false if subui.message
    newline
    raise subui.result

  else
    colour = :gray
    say " #{message}", :gray, false
  end

  say(" (#{Time.now-start_time}s)", colour, false) if benchmark?

  newline
  newline
end

#recolourize(colour) ⇒ Object



68
69
70
# File 'lib/angry_mob/ui.rb', line 68

def recolourize(colour)
  $stdout.print("\e[s\e[1G#{RED}\e[u")
end

#say(message = "", colour = nil, force_new_line = (message.to_s !~ /( |\t)$/)) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/angry_mob/ui.rb', line 72

def say(message="", colour=nil, force_new_line=(message.to_s !~ /( |\t)$/))
  return if self.class.silence?

  message  = message.to_s
  message  = colourise(message, colour) if colour

  if force_new_line
    $stdout.puts(message)
  else
    $stdout.print(message)
    $stdout.flush
  end
end

#silence(&block) ⇒ Object



116
117
118
# File 'lib/angry_mob/ui.rb', line 116

def silence(&block)
  self.class.silence(&block)
end

#skipped!(message) ⇒ Object



237
238
239
240
# File 'lib/angry_mob/ui.rb', line 237

def skipped!(message)
  @result = :skip
  @message = message
end

#spacesObject



94
95
96
# File 'lib/angry_mob/ui.rb', line 94

def spaces
  "  " * @level
end

#start!(banner) ⇒ Object



216
217
218
219
220
# File 'lib/angry_mob/ui.rb', line 216

def start!(banner)
  say spaces+">> ", :blue
  say banner, :white, false
  say " {", :yellow
end

#task(message) ⇒ Object



131
132
133
134
# File 'lib/angry_mob/ui.rb', line 131

def task(message)
  isay ">> ", :blue
  say message
end

#warn(message) ⇒ Object



160
161
162
# File 'lib/angry_mob/ui.rb', line 160

def warn(message)
  say spaces+message, :yellow
end