2005-04-13 11:30:15 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# notifier.rb - output methods used by irb
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2005-04-13 11:30:15 -04:00
|
|
|
# $Revision$
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-04-13 11:30:15 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
require "e2mmap"
|
|
|
|
require "irb/output-method"
|
|
|
|
|
|
|
|
module IRB
|
|
|
|
module Notifier
|
|
|
|
extend Exception2MessageMapper
|
2009-03-05 22:56:38 -05:00
|
|
|
def_exception :ErrUndefinedNotifier,
|
2005-04-13 11:30:15 -04:00
|
|
|
"undefined notifier level: %d is specified"
|
2009-03-05 22:56:38 -05:00
|
|
|
def_exception :ErrUnrecognizedLevel,
|
2005-04-13 11:30:15 -04:00
|
|
|
"unrecognized notifier level: %s is specified"
|
|
|
|
|
|
|
|
def def_notifier(prefix = "", output_method = StdioOutputMethod.new)
|
|
|
|
CompositeNotifier.new(prefix, output_method)
|
|
|
|
end
|
|
|
|
module_function :def_notifier
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2010-03-19 23:30:59 -04:00
|
|
|
class AbstractNotifier
|
2005-04-13 11:30:15 -04:00
|
|
|
def initialize(prefix, base_notifier)
|
|
|
|
@prefix = prefix
|
|
|
|
@base_notifier = base_notifier
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :prefix
|
|
|
|
|
|
|
|
def notify?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def print(*opts)
|
|
|
|
@base_notifier.print prefix, *opts if notify?
|
|
|
|
end
|
|
|
|
|
|
|
|
def printn(*opts)
|
|
|
|
@base_notifier.printn prefix, *opts if notify?
|
|
|
|
end
|
|
|
|
|
|
|
|
def printf(format, *opts)
|
|
|
|
@base_notifier.printf(prefix + format, *opts) if notify?
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts(*objs)
|
|
|
|
if notify?
|
2005-04-14 02:16:08 -04:00
|
|
|
@base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s})
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pp(*objs)
|
|
|
|
if notify?
|
|
|
|
@base_notifier.ppx @prefix, *objs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ppx(prefix, *objs)
|
|
|
|
if notify?
|
|
|
|
@base_notifier.ppx @prefix+prefix, *objs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_if
|
|
|
|
yield(@base_notifier) if notify?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-19 23:30:59 -04:00
|
|
|
class CompositeNotifier<AbstractNotifier
|
2005-04-13 11:30:15 -04:00
|
|
|
def initialize(prefix, base_notifier)
|
|
|
|
super
|
|
|
|
|
|
|
|
@notifiers = [D_NOMSG]
|
|
|
|
@level_notifier = D_NOMSG
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :notifiers
|
|
|
|
|
|
|
|
def def_notifier(level, prefix = "")
|
|
|
|
notifier = LeveledNotifier.new(self, level, prefix)
|
|
|
|
@notifiers[level] = notifier
|
|
|
|
notifier
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :level_notifier
|
|
|
|
alias level level_notifier
|
|
|
|
|
|
|
|
def level_notifier=(value)
|
|
|
|
case value
|
2010-03-19 23:30:59 -04:00
|
|
|
when AbstractNotifier
|
2005-04-13 11:30:15 -04:00
|
|
|
@level_notifier = value
|
|
|
|
when Integer
|
|
|
|
l = @notifiers[value]
|
|
|
|
Notifier.Raise ErrUndefinedNotifer, value unless l
|
|
|
|
@level_notifier = l
|
|
|
|
else
|
|
|
|
Notifier.Raise ErrUnrecognizedLevel, value unless l
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias level= level_notifier=
|
|
|
|
end
|
|
|
|
|
2010-03-19 23:30:59 -04:00
|
|
|
class LeveledNotifier<AbstractNotifier
|
2005-04-13 11:30:15 -04:00
|
|
|
include Comparable
|
|
|
|
|
|
|
|
def initialize(base, level, prefix)
|
|
|
|
super(prefix, base)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:30:15 -04:00
|
|
|
@level = level
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :level
|
|
|
|
|
|
|
|
def <=>(other)
|
|
|
|
@level <=> other.level
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:30:15 -04:00
|
|
|
def notify?
|
|
|
|
@base_notifier.level >= self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NoMsgNotifier<LeveledNotifier
|
|
|
|
def initialize
|
|
|
|
@base_notifier = nil
|
|
|
|
@level = 0
|
|
|
|
@prefix = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
D_NOMSG = NoMsgNotifier.new
|
|
|
|
end
|
|
|
|
end
|