Module: Rabbit::Utils

Included in:
Action, Element::Base, Format, Logger, Parser::RD::Ext::InlineVerbatim, Renderer::Display
Defined in:
lib/rabbit/utils.rb

Class Method Summary collapse

Class Method Details

.arg_list(arity) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rabbit/utils.rb', line 98

def arg_list(arity)
  args = []
  if arity == -1
    args << "*args"
  else
    arity.times do |i|
      args << "arg#{i}"
    end
  end
  args
end

.collect_classes_under_module(mod) ⇒ Object


74
75
76
# File 'lib/rabbit/utils.rb', line 74

def collect_classes_under_module(mod)
  collect_under_module(mod, Class)
end

.collect_modules_under_module(mod) ⇒ Object


78
79
80
# File 'lib/rabbit/utils.rb', line 78

def collect_modules_under_module(mod)
  collect_under_module(mod, Module)
end

.collect_under_module(mod, klass) ⇒ Object


66
67
68
69
70
71
72
# File 'lib/rabbit/utils.rb', line 66

def collect_under_module(mod, klass)
  mod.constants.collect do |x|
    mod.const_get(x)
  end.find_all do |x|
    x.is_a?(klass)
  end
end

.combination(elements) ⇒ Object


255
256
257
258
259
260
261
262
263
264
# File 'lib/rabbit/utils.rb', line 255

def combination(elements)
  return [] if elements.empty?
  first, *rest = elements
  results = combination(rest)
  if results.empty?
    [[], [first]]
  else
    results + results.collect {|result| [first, *result]}
  end
end

.compute_bottom_y(base_y, base_height, target_height, max_y) ⇒ Object


225
226
227
228
# File 'lib/rabbit/utils.rb', line 225

def compute_bottom_y(base_y, base_height, target_height, max_y)
  bottom = base_y + base_height - target_height
  [[bottom, max_y - target_height].min, 0].max
end

.compute_left_x(base_x) ⇒ Object


212
213
214
# File 'lib/rabbit/utils.rb', line 212

def compute_left_x(base_x)
  [base_x, 0].max
end

.compute_right_x(base_x, base_width, target_width, max_x) ⇒ Object


216
217
218
219
# File 'lib/rabbit/utils.rb', line 216

def compute_right_x(base_x, base_width, target_width, max_x)
  right = base_x + base_width - target_width
  [[right, max_x - target_width].min, 0].max
end

.compute_top_y(base_y) ⇒ Object


221
222
223
# File 'lib/rabbit/utils.rb', line 221

def compute_top_y(base_y)
  [base_y, 0].max
end

.corresponding_class_under_module(mod) ⇒ Object


90
91
92
# File 'lib/rabbit/utils.rb', line 90

def corresponding_class_under_module(mod)
  corresponding_objects(collect_classes_under_module(mod))
end

.corresponding_module_under_module(mod) ⇒ Object


94
95
96
# File 'lib/rabbit/utils.rb', line 94

def corresponding_module_under_module(mod)
  corresponding_objects(collect_modules_under_module(mod))
end

.corresponding_objects(objects) ⇒ Object


82
83
84
85
86
87
88
# File 'lib/rabbit/utils.rb', line 82

def corresponding_objects(objects)
  objects.find_all do |object|
    object.respond_to?(:priority)
  end.sort_by do |object|
    object.priority
  end.last
end

.ensure_time(object) ⇒ Object


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rabbit/utils.rb', line 297

def ensure_time(object)
  return nil if object.nil?
  return object if object.is_a?(Numeric)

  if /\A\s*\z/m =~ object
    nil
  else
    if /\A\s*(\d*\.?\d*)\s*(h|m|s)?\s*\z/i =~ object
      time = $1.to_f
      unit = $2
      if unit
        case unit.downcase
        when "m"
          time *= 60
        when "h"
          time *= 3600
        end
      end
      time.to_i
    else
      nil
    end
  end
end

.events_pending_available?Boolean

Returns:

  • (Boolean)

133
134
135
# File 'lib/rabbit/utils.rb', line 133

def events_pending_available?
  !windows? #or (Gtk::BINDING_VERSION <=> [0, 14, 1]) > 0
end

.extract_four_way(params) ⇒ Object


266
267
268
# File 'lib/rabbit/utils.rb', line 266

def extract_four_way(params)
  [params[:top], params[:right], params[:bottom], params[:left]]
end

.find_path_in_load_path(*name) ⇒ Object


110
111
112
113
114
115
116
117
118
119
# File 'lib/rabbit/utils.rb', line 110

def find_path_in_load_path(*name)
  found_path = $LOAD_PATH.find do |path|
    File.readable?(File.join(path, *name))
  end
  if found_path
    File.join(found_path, *name)
  else
    nil
  end
end

.init_by_constants_as_default_value(obj) ⇒ Object


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rabbit/utils.rb', line 143

def init_by_constants_as_default_value(obj)
  klass = obj.class
  klass.constants.each do |name|
    const = klass.const_get(name)
    unless const.kind_of?(Class)
      var_name = name.downcase
      obj.instance_variable_set("@#{var_name}", const.dup)
      klass.module_eval {attr_accessor var_name}
    end
  end
end

.move_to(base, target) ⇒ Object


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rabbit/utils.rb', line 194

def move_to(base, target)
  window = base.window
  screen = window.screen
  num = screen.get_monitor(window)
  monitor = screen.get_monitor_geometry(num)
  window_x, window_y = window.origin
  window_width  = window.width
  window_height = window.height
  target_width, target_height = target.size

  args = [window_x, window_y, window_width, window_height]
  args.concat([target_width, target_height])
  args.concat([screen.width, screen.height])
  x, y = yield(*args)

  target.move(x, y)
end

.move_to_bottom_left(base, target) ⇒ Object


242
243
244
245
246
# File 'lib/rabbit/utils.rb', line 242

def move_to_bottom_left(base, target)
  move_to(base, target) do |bx, by, bw, bh, tw, th, sw, sh|
    [compute_left_x(bx), compute_bottom_y(by, bh, th, sh)]
  end
end

.move_to_bottom_right(base, target) ⇒ Object


248
249
250
251
252
253
# File 'lib/rabbit/utils.rb', line 248

def move_to_bottom_right(base, target)
  move_to(base, target) do |bx, by, bw, bh, tw, th, sw, sh|
    [compute_right_x(bx, bw, tw, sw),
     compute_bottom_y(by, bh, th, sh)]
  end
end

.move_to_top_left(base, target) ⇒ Object


230
231
232
233
234
# File 'lib/rabbit/utils.rb', line 230

def move_to_top_left(base, target)
  move_to(base, target) do |bx, by, bw, bh, tw, th, sw, sh|
    [compute_left_x(bx), compute_top_y(by)]
  end
end

.move_to_top_right(base, target) ⇒ Object


236
237
238
239
240
# File 'lib/rabbit/utils.rb', line 236

def move_to_top_right(base, target)
  move_to(base, target) do |bx, by, bw, bh, tw, th, sw, sh|
    [compute_right_x(bx, bw, tw, sw), compute_top_y(by)]
  end
end

.parse_four_way(*values) ⇒ Object


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rabbit/utils.rb', line 270

def parse_four_way(*values)
  if values.size == 1 and
      (values.first.is_a?(Array) or values.first.is_a?(Hash))
    values = values.first
  end
  if values.is_a?(Hash)
    extract_four_way(values)
  else
    case values.size
    when 1
      left = right = top = bottom = Integer(values.first)
    when 2
      top, left = values.collect {|x| Integer(x)}
      bottom = top
      right = left
    when 3
      top, left, bottom = values.collect {|x| Integer(x)}
      right = left
    when 4
      top, right, bottom, left = values.collect {|x| Integer(x)}
    else
      raise ArgumentError
    end
    [top, right, bottom, left]
  end
end

.process_pending_eventsObject


125
126
127
128
129
130
131
# File 'lib/rabbit/utils.rb', line 125

def process_pending_events
  if events_pending_available?
    while Gtk.events_pending?
      Gtk.main_iteration
    end
  end
end

.process_pending_events_procObject


137
138
139
140
141
# File 'lib/rabbit/utils.rb', line 137

def process_pending_events_proc
  Proc.new do
    process_pending_events
  end
end

.quartz?Boolean

Returns:

  • (Boolean)

186
187
188
189
190
191
192
# File 'lib/rabbit/utils.rb', line 186

def quartz?
  if Gdk.respond_to?(:windowing_quartz?)
    Gdk.windowing_quartz?
  else
    !windows? and !Gdk.windowing_x11?
  end
end

.require_files_under_directory_in_load_path(dir, silent = true) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rabbit/utils.rb', line 36

def require_files_under_directory_in_load_path(dir, silent=true)
  normalize = Proc.new do |base_path, path|
    path.sub(/\A#{Regexp.escape(base_path)}\/?/, '').sub(/\.[^.]+$/, '')
  end

  $LOAD_PATH.each do |path|
    source_glob = ::File.join(path, dir, '*')
    Dir.glob(source_glob) do |source|
      next if File.directory?(source)
      begin
        before = Time.now
        normalized_path = normalize[path, source]
        require_safe normalized_path
        unless silent
          STDERR.puts([Time.now - before, normalized_path].inspect)
        end
      rescue LoadError, RuntimeError # Should be changed to Gtk::InitError?
        if $!.is_a?(RuntimeError)
          raise if /\ACannot open display:\s*\d*\z/ !~ $!.message
        end
        unless silent
          STDERR.puts(path)
          STDERR.puts($!.message)
          STDERR.puts([email protected])
        end
      end
    end
  end
end

.require_safe(path) ⇒ Object


29
30
31
32
33
34
# File 'lib/rabbit/utils.rb', line 29

def require_safe(path)
  require path
rescue LoadError
  $".reject! {|x| /\A#{Regexp.escape(path)}/ =~ x}
  raise
end

.split_number_to_minute_and_second(number) ⇒ Object


322
323
324
325
326
327
328
329
# File 'lib/rabbit/utils.rb', line 322

def split_number_to_minute_and_second(number)
  if number >= 0
    sign = " "
  else
    sign = "-"
  end
  [sign, *number.abs.divmod(60)]
end

.stringify_hash_key(hash) ⇒ Object


340
341
342
343
344
345
346
# File 'lib/rabbit/utils.rb', line 340

def stringify_hash_key(hash)
  stringified_hash = {}
  hash.each do |key, value|
    stringified_hash[key.to_s] = value
  end
  stringified_hash
end

.support_console_input?Boolean

Returns:

  • (Boolean)

160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rabbit/utils.rb', line 160

def support_console_input?
  if windows?
    begin
      File.open("conin$", "w") {}
      true
    rescue SystemCallError
      false
    end
  else
    $stdin.tty? or ENV["TERM"]
  end
end

.support_console_output?Boolean

Returns:

  • (Boolean)

173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rabbit/utils.rb', line 173

def support_console_output?
  if windows?
    begin
      File.open("conout$", "w") {}
      true
    rescue SystemCallError
      false
    end
  else
    $stdout.tty? or ENV["TERM"]
  end
end

.syntax_highlighting_debug?Boolean

Returns:

  • (Boolean)

348
349
350
# File 'lib/rabbit/utils.rb', line 348

def syntax_highlighting_debug?
  ENV["RABBIT_SYNTAX_HIGHLIGHTING_DEBUG"] == "yes"
end

.time(message = nil) ⇒ Object


331
332
333
334
335
336
337
338
# File 'lib/rabbit/utils.rb', line 331

def time(message=nil)
  before = Time.now
  yield
ensure
  output = Time.now - before
  output = [message, output] if message
  p output
end

.to_class_name(name) ⇒ Object


23
24
25
26
27
# File 'lib/rabbit/utils.rb', line 23

def to_class_name(name)
  name.gsub(/(?:\A|_|\-)([a-z])/) do |x|
    $1.upcase
  end
end

.unescape_title(title) ⇒ Object


121
122
123
# File 'lib/rabbit/utils.rb', line 121

def unescape_title(title)
  REXML::Text.unnormalize(title).gsub(/\r|\n/, ' ')
end

.windows?Boolean

Returns:

  • (Boolean)

155
156
157
158
# File 'lib/rabbit/utils.rb', line 155

def windows?
  # Gdk.windowing_win32? # what about this?
  /cygwin|mingw|mswin32|bccwin32/.match(RUBY_PLATFORM) ? true : false
end