2005-04-13 19:30:15 +04:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# notifier.rb - output methods used by irb
|
2009-07-07 15:36:20 +04:00
|
|
|
# $Release Version: 0.9.6$
|
2005-04-13 19:30:15 +04:00
|
|
|
# $Revision$
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2005-04-13 19:30:15 +04:00
|
|
|
#
|
|
|
|
|
|
|
|
require "e2mmap"
|
|
|
|
require "irb/output-method"
|
|
|
|
|
|
|
|
module IRB
|
2012-12-21 09:45:50 +04:00
|
|
|
# An output formatter used internally by the lexer.
|
2005-04-13 19:30:15 +04:00
|
|
|
module Notifier
|
|
|
|
extend Exception2MessageMapper
|
2009-03-06 06:56:38 +03:00
|
|
|
def_exception :ErrUndefinedNotifier,
|
2005-04-13 19:30:15 +04:00
|
|
|
"undefined notifier level: %d is specified"
|
2009-03-06 06:56:38 +03:00
|
|
|
def_exception :ErrUnrecognizedLevel,
|
2005-04-13 19:30:15 +04:00
|
|
|
"unrecognized notifier level: %s is specified"
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Define a new Notifier output source, returning a new CompositeNotifier
|
|
|
|
# with the given +prefix+ and +output_method+.
|
|
|
|
#
|
|
|
|
# The optional +prefix+ will be appended to all objects being inspected
|
|
|
|
# during output, using the given +output_method+ as the output source. If
|
2013-11-27 16:56:08 +04:00
|
|
|
# no +output_method+ is given, StdioOutputMethod will be used, and all
|
2012-12-21 09:45:50 +04:00
|
|
|
# expressions will be sent directly to STDOUT without any additional
|
|
|
|
# formatting.
|
2005-04-13 19:30:15 +04:00
|
|
|
def def_notifier(prefix = "", output_method = StdioOutputMethod.new)
|
|
|
|
CompositeNotifier.new(prefix, output_method)
|
|
|
|
end
|
|
|
|
module_function :def_notifier
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# An abstract class, or superclass, for CompositeNotifier and
|
|
|
|
# LeveledNotifier to inherit. It provides several wrapper methods for the
|
|
|
|
# OutputMethod object used by the Notifier.
|
2010-03-20 06:30:59 +03:00
|
|
|
class AbstractNotifier
|
2012-12-21 09:45:50 +04:00
|
|
|
# Creates a new Notifier object
|
2005-04-13 19:30:15 +04:00
|
|
|
def initialize(prefix, base_notifier)
|
|
|
|
@prefix = prefix
|
|
|
|
@base_notifier = base_notifier
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# The +prefix+ for this Notifier, which is appended to all objects being
|
|
|
|
# inspected during output.
|
2005-04-13 19:30:15 +04:00
|
|
|
attr_reader :prefix
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# A wrapper method used to determine whether notifications are enabled.
|
|
|
|
#
|
|
|
|
# Defaults to +true+.
|
2005-04-13 19:30:15 +04:00
|
|
|
def notify?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# See OutputMethod#print for more detail.
|
2005-04-13 19:30:15 +04:00
|
|
|
def print(*opts)
|
|
|
|
@base_notifier.print prefix, *opts if notify?
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# See OutputMethod#printn for more detail.
|
2005-04-13 19:30:15 +04:00
|
|
|
def printn(*opts)
|
|
|
|
@base_notifier.printn prefix, *opts if notify?
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# See OutputMethod#printf for more detail.
|
2005-04-13 19:30:15 +04:00
|
|
|
def printf(format, *opts)
|
|
|
|
@base_notifier.printf(prefix + format, *opts) if notify?
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# See OutputMethod#puts for more detail.
|
2005-04-13 19:30:15 +04:00
|
|
|
def puts(*objs)
|
|
|
|
if notify?
|
2005-04-14 10:16:08 +04:00
|
|
|
@base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s})
|
2005-04-13 19:30:15 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Same as #ppx, except it uses the #prefix given during object
|
|
|
|
# initialization.
|
|
|
|
# See OutputMethod#ppx for more detail.
|
2005-04-13 19:30:15 +04:00
|
|
|
def pp(*objs)
|
|
|
|
if notify?
|
|
|
|
@base_notifier.ppx @prefix, *objs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Same as #pp, except it concatenates the given +prefix+ with the #prefix
|
|
|
|
# given during object initialization.
|
|
|
|
#
|
|
|
|
# See OutputMethod#ppx for more detail.
|
2005-04-13 19:30:15 +04:00
|
|
|
def ppx(prefix, *objs)
|
|
|
|
if notify?
|
|
|
|
@base_notifier.ppx @prefix+prefix, *objs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Execute the given block if notifications are enabled.
|
2005-04-13 19:30:15 +04:00
|
|
|
def exec_if
|
|
|
|
yield(@base_notifier) if notify?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# A class that can be used to create a group of notifier objects with the
|
|
|
|
# intent of representing a leveled notification system for irb.
|
|
|
|
#
|
|
|
|
# This class will allow you to generate other notifiers, and assign them
|
|
|
|
# the appropriate level for output.
|
|
|
|
#
|
|
|
|
# The Notifier class provides a class-method Notifier.def_notifier to
|
|
|
|
# create a new composite notifier. Using the first composite notifier
|
|
|
|
# object you create, sibling notifiers can be initialized with
|
|
|
|
# #def_notifier.
|
2010-03-20 06:30:59 +03:00
|
|
|
class CompositeNotifier<AbstractNotifier
|
2012-12-21 09:45:50 +04:00
|
|
|
# Create a new composite notifier object with the given +prefix+, and
|
|
|
|
# +base_notifier+ to use for output.
|
2005-04-13 19:30:15 +04:00
|
|
|
def initialize(prefix, base_notifier)
|
|
|
|
super
|
|
|
|
|
|
|
|
@notifiers = [D_NOMSG]
|
|
|
|
@level_notifier = D_NOMSG
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# List of notifiers in the group
|
2005-04-13 19:30:15 +04:00
|
|
|
attr_reader :notifiers
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Creates a new LeveledNotifier in the composite #notifiers group.
|
|
|
|
#
|
|
|
|
# The given +prefix+ will be assigned to the notifier, and +level+ will
|
|
|
|
# be used as the index of the #notifiers Array.
|
|
|
|
#
|
|
|
|
# This method returns the newly created instance.
|
2005-04-13 19:30:15 +04:00
|
|
|
def def_notifier(level, prefix = "")
|
|
|
|
notifier = LeveledNotifier.new(self, level, prefix)
|
|
|
|
@notifiers[level] = notifier
|
|
|
|
notifier
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Returns the leveled notifier for this object
|
2005-04-13 19:30:15 +04:00
|
|
|
attr_reader :level_notifier
|
|
|
|
alias level level_notifier
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Sets the leveled notifier for this object.
|
|
|
|
#
|
|
|
|
# When the given +value+ is an instance of AbstractNotifier,
|
|
|
|
# #level_notifier is set to the given object.
|
|
|
|
#
|
|
|
|
# When an Integer is given, #level_notifier is set to the notifier at the
|
|
|
|
# index +value+ in the #notifiers Array.
|
|
|
|
#
|
|
|
|
# If no notifier exists at the index +value+ in the #notifiers Array, an
|
|
|
|
# ErrUndefinedNotifier exception is raised.
|
|
|
|
#
|
|
|
|
# An ErrUnrecognizedLevel exception is raised if the given +value+ is not
|
|
|
|
# found in the existing #notifiers Array, or an instance of
|
|
|
|
# AbstractNotifier
|
2005-04-13 19:30:15 +04:00
|
|
|
def level_notifier=(value)
|
|
|
|
case value
|
2010-03-20 06:30:59 +03:00
|
|
|
when AbstractNotifier
|
2005-04-13 19:30:15 +04:00
|
|
|
@level_notifier = value
|
|
|
|
when Integer
|
|
|
|
l = @notifiers[value]
|
2013-11-27 13:05:57 +04:00
|
|
|
Notifier.Raise ErrUndefinedNotifier, value unless l
|
2005-04-13 19:30:15 +04:00
|
|
|
@level_notifier = l
|
|
|
|
else
|
|
|
|
Notifier.Raise ErrUnrecognizedLevel, value unless l
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias level= level_notifier=
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# A leveled notifier is comparable to the composite group from
|
|
|
|
# CompositeNotifier#notifiers.
|
2010-03-20 06:30:59 +03:00
|
|
|
class LeveledNotifier<AbstractNotifier
|
2005-04-13 19:30:15 +04:00
|
|
|
include Comparable
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Create a new leveled notifier with the given +base+, and +prefix+ to
|
|
|
|
# send to AbstractNotifier.new
|
|
|
|
#
|
|
|
|
# The given +level+ is used to compare other leveled notifiers in the
|
|
|
|
# CompositeNotifier group to determine whether or not to output
|
|
|
|
# notifications.
|
2005-04-13 19:30:15 +04:00
|
|
|
def initialize(base, level, prefix)
|
|
|
|
super(prefix, base)
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-04-13 19:30:15 +04:00
|
|
|
@level = level
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# The current level of this notifier object
|
2005-04-13 19:30:15 +04:00
|
|
|
attr_reader :level
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Compares the level of this notifier object with the given +other+
|
|
|
|
# notifier.
|
|
|
|
#
|
|
|
|
# See the Comparable module for more information.
|
2005-04-13 19:30:15 +04:00
|
|
|
def <=>(other)
|
|
|
|
@level <=> other.level
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Whether to output messages to the output method, depending on the level
|
|
|
|
# of this notifier object.
|
2005-04-13 19:30:15 +04:00
|
|
|
def notify?
|
|
|
|
@base_notifier.level >= self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# NoMsgNotifier is a LeveledNotifier that's used as the default notifier
|
|
|
|
# when creating a new CompositeNotifier.
|
|
|
|
#
|
|
|
|
# This notifier is used as the +zero+ index, or level +0+, for
|
|
|
|
# CompositeNotifier#notifiers, and will not output messages of any sort.
|
2005-04-13 19:30:15 +04:00
|
|
|
class NoMsgNotifier<LeveledNotifier
|
2012-12-21 09:45:50 +04:00
|
|
|
# Creates a new notifier that should not be used to output messages.
|
2005-04-13 19:30:15 +04:00
|
|
|
def initialize
|
|
|
|
@base_notifier = nil
|
|
|
|
@level = 0
|
|
|
|
@prefix = ""
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Ensures notifications are ignored, see AbstractNotifier#notify? for
|
|
|
|
# more information.
|
2005-04-13 19:30:15 +04:00
|
|
|
def notify?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
D_NOMSG = NoMsgNotifier.new # :nodoc:
|
2005-04-13 19:30:15 +04:00
|
|
|
end
|
|
|
|
end
|