2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
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
|
2012-12-21 00:45:50 -05:00
|
|
|
# An output formatter used internally by the lexer.
|
2005-04-13 11:30:15 -04:00
|
|
|
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"
|
|
|
|
|
2012-12-21 00:45:50 -05: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 07:56:08 -05:00
|
|
|
# no +output_method+ is given, StdioOutputMethod will be used, and all
|
2012-12-21 00:45:50 -05:00
|
|
|
# expressions will be sent directly to STDOUT without any additional
|
|
|
|
# formatting.
|
2005-04-13 11:30:15 -04:00
|
|
|
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
|
|
|
|
2012-12-21 00:45:50 -05: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-19 23:30:59 -04:00
|
|
|
class AbstractNotifier
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new Notifier object
|
2005-04-13 11:30:15 -04:00
|
|
|
def initialize(prefix, base_notifier)
|
2014-08-08 21:36:49 -04:00
|
|
|
@prefix = prefix
|
|
|
|
@base_notifier = base_notifier
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The +prefix+ for this Notifier, which is appended to all objects being
|
|
|
|
# inspected during output.
|
2005-04-13 11:30:15 -04:00
|
|
|
attr_reader :prefix
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# A wrapper method used to determine whether notifications are enabled.
|
|
|
|
#
|
|
|
|
# Defaults to +true+.
|
2005-04-13 11:30:15 -04:00
|
|
|
def notify?
|
2014-08-08 21:36:49 -04:00
|
|
|
true
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# See OutputMethod#print for more detail.
|
2005-04-13 11:30:15 -04:00
|
|
|
def print(*opts)
|
2014-08-08 21:36:49 -04:00
|
|
|
@base_notifier.print prefix, *opts if notify?
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# See OutputMethod#printn for more detail.
|
2005-04-13 11:30:15 -04:00
|
|
|
def printn(*opts)
|
2014-08-08 21:36:49 -04:00
|
|
|
@base_notifier.printn prefix, *opts if notify?
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# See OutputMethod#printf for more detail.
|
2005-04-13 11:30:15 -04:00
|
|
|
def printf(format, *opts)
|
2014-08-08 21:36:49 -04:00
|
|
|
@base_notifier.printf(prefix + format, *opts) if notify?
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# See OutputMethod#puts for more detail.
|
2005-04-13 11:30:15 -04:00
|
|
|
def puts(*objs)
|
2014-08-08 21:36:49 -04:00
|
|
|
if notify?
|
|
|
|
@base_notifier.puts(*objs.collect{|obj| prefix + obj.to_s})
|
|
|
|
end
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Same as #ppx, except it uses the #prefix given during object
|
|
|
|
# initialization.
|
|
|
|
# See OutputMethod#ppx for more detail.
|
2005-04-13 11:30:15 -04:00
|
|
|
def pp(*objs)
|
2014-08-08 21:36:49 -04:00
|
|
|
if notify?
|
|
|
|
@base_notifier.ppx @prefix, *objs
|
|
|
|
end
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05: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 11:30:15 -04:00
|
|
|
def ppx(prefix, *objs)
|
2014-08-08 21:36:49 -04:00
|
|
|
if notify?
|
|
|
|
@base_notifier.ppx @prefix+prefix, *objs
|
|
|
|
end
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Execute the given block if notifications are enabled.
|
2005-04-13 11:30:15 -04:00
|
|
|
def exec_if
|
2014-08-08 21:36:49 -04:00
|
|
|
yield(@base_notifier) if notify?
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05: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.
|
2016-10-07 01:18:57 -04:00
|
|
|
class CompositeNotifier < AbstractNotifier
|
2012-12-21 00:45:50 -05:00
|
|
|
# Create a new composite notifier object with the given +prefix+, and
|
|
|
|
# +base_notifier+ to use for output.
|
2005-04-13 11:30:15 -04:00
|
|
|
def initialize(prefix, base_notifier)
|
2014-08-08 21:36:49 -04:00
|
|
|
super
|
2005-04-13 11:30:15 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
@notifiers = [D_NOMSG]
|
|
|
|
@level_notifier = D_NOMSG
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# List of notifiers in the group
|
2005-04-13 11:30:15 -04:00
|
|
|
attr_reader :notifiers
|
|
|
|
|
2012-12-21 00:45:50 -05: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 11:30:15 -04:00
|
|
|
def def_notifier(level, prefix = "")
|
2014-08-08 21:36:49 -04:00
|
|
|
notifier = LeveledNotifier.new(self, level, prefix)
|
|
|
|
@notifiers[level] = notifier
|
|
|
|
notifier
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the leveled notifier for this object
|
2005-04-13 11:30:15 -04:00
|
|
|
attr_reader :level_notifier
|
|
|
|
alias level level_notifier
|
|
|
|
|
2012-12-21 00:45:50 -05: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 11:30:15 -04:00
|
|
|
def level_notifier=(value)
|
2014-08-08 21:36:49 -04:00
|
|
|
case value
|
|
|
|
when AbstractNotifier
|
|
|
|
@level_notifier = value
|
|
|
|
when Integer
|
|
|
|
l = @notifiers[value]
|
|
|
|
Notifier.Raise ErrUndefinedNotifier, value unless l
|
|
|
|
@level_notifier = l
|
|
|
|
else
|
|
|
|
Notifier.Raise ErrUnrecognizedLevel, value unless l
|
|
|
|
end
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
alias level= level_notifier=
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# A leveled notifier is comparable to the composite group from
|
|
|
|
# CompositeNotifier#notifiers.
|
2016-10-07 01:18:57 -04:00
|
|
|
class LeveledNotifier < AbstractNotifier
|
2005-04-13 11:30:15 -04:00
|
|
|
include Comparable
|
|
|
|
|
2012-12-21 00:45:50 -05: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 11:30:15 -04:00
|
|
|
def initialize(base, level, prefix)
|
2014-08-08 21:36:49 -04:00
|
|
|
super(prefix, base)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
@level = level
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The current level of this notifier object
|
2005-04-13 11:30:15 -04:00
|
|
|
attr_reader :level
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Compares the level of this notifier object with the given +other+
|
|
|
|
# notifier.
|
|
|
|
#
|
|
|
|
# See the Comparable module for more information.
|
2005-04-13 11:30:15 -04:00
|
|
|
def <=>(other)
|
2014-08-08 21:36:49 -04:00
|
|
|
@level <=> other.level
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Whether to output messages to the output method, depending on the level
|
|
|
|
# of this notifier object.
|
2005-04-13 11:30:15 -04:00
|
|
|
def notify?
|
2014-08-08 21:36:49 -04:00
|
|
|
@base_notifier.level >= self
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05: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.
|
2016-10-07 01:18:57 -04:00
|
|
|
class NoMsgNotifier < LeveledNotifier
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new notifier that should not be used to output messages.
|
2005-04-13 11:30:15 -04:00
|
|
|
def initialize
|
2014-08-08 21:36:49 -04:00
|
|
|
@base_notifier = nil
|
|
|
|
@level = 0
|
|
|
|
@prefix = ""
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Ensures notifications are ignored, see AbstractNotifier#notify? for
|
|
|
|
# more information.
|
2005-04-13 11:30:15 -04:00
|
|
|
def notify?
|
2014-08-08 21:36:49 -04:00
|
|
|
false
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
D_NOMSG = NoMsgNotifier.new # :nodoc:
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|