# # notifier.rb - output methods used by irb # $Release Version: 0.9.6$ # $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # # -- # # # require "e2mmap" require "irb/output-method" module IRB # An output formatter used internally by the lexer. module Notifier extend Exception2MessageMapper def_exception :ErrUndefinedNotifier, "undefined notifier level: %d is specified" def_exception :ErrUnrecognizedLevel, "unrecognized notifier level: %s is specified" # 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 # no +output_method+ is given, StdioOutputMethod will be used, and all # expressions will be sent directly to STDOUT without any additional # formatting. def def_notifier(prefix = "", output_method = StdioOutputMethod.new) CompositeNotifier.new(prefix, output_method) end module_function :def_notifier # An abstract class, or superclass, for CompositeNotifier and # LeveledNotifier to inherit. It provides several wrapper methods for the # OutputMethod object used by the Notifier. class AbstractNotifier # Creates a new Notifier object def initialize(prefix, base_notifier) @prefix = prefix @base_notifier = base_notifier end # The +prefix+ for this Notifier, which is appended to all objects being # inspected during output. attr_reader :prefix # A wrapper method used to determine whether notifications are enabled. # # Defaults to +true+. def notify? true end # See OutputMethod#print for more detail. def print(*opts) @base_notifier.print prefix, *opts if notify? end # See OutputMethod#printn for more detail. def printn(*opts) @base_notifier.printn prefix, *opts if notify? end # See OutputMethod#printf for more detail. def printf(format, *opts) @base_notifier.printf(prefix + format, *opts) if notify? end # See OutputMethod#puts for more detail. def puts(*objs) if notify? @base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s}) end end # Same as #ppx, except it uses the #prefix given during object # initialization. # See OutputMethod#ppx for more detail. def pp(*objs) if notify? @base_notifier.ppx @prefix, *objs end end # Same as #pp, except it concatenates the given +prefix+ with the #prefix # given during object initialization. # # See OutputMethod#ppx for more detail. def ppx(prefix, *objs) if notify? @base_notifier.ppx @prefix+prefix, *objs end end # Execute the given block if notifications are enabled. def exec_if yield(@base_notifier) if notify? end end # 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. class CompositeNotifier(other) @level <=> other.level end # Whether to output messages to the output method, depending on the level # of this notifier object. def notify? @base_notifier.level >= self end end # 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. class NoMsgNotifier