2005-04-13 11:30:15 -04:00
|
|
|
#
|
2010-03-19 23:30:59 -04:00
|
|
|
# output-method.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"
|
|
|
|
|
|
|
|
module IRB
|
2012-12-21 00:45:50 -05:00
|
|
|
# An abstract output class for IO in irb. This is mainly used internally by
|
|
|
|
# IRB::Notifier. You can define your own output method to use with Irb.new,
|
|
|
|
# or Context.new
|
2005-04-13 11:30:15 -04:00
|
|
|
class OutputMethod
|
2012-12-25 08:34:34 -05:00
|
|
|
extend Exception2MessageMapper
|
|
|
|
def_exception :NotImplementedError, "Need to define `%s'"
|
|
|
|
|
2005-04-13 11:30:15 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Open this method to implement your own output method, raises a
|
|
|
|
# NotImplementedError if you don't define #print in your own class.
|
2005-04-13 11:30:15 -04:00
|
|
|
def print(*opts)
|
2012-12-25 08:34:34 -05:00
|
|
|
OutputMethod.Raise NotImplementedError, "print"
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Prints the given +opts+, with a newline delimiter.
|
2005-04-13 11:30:15 -04:00
|
|
|
def printn(*opts)
|
|
|
|
print opts.join(" "), "\n"
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Extends IO#printf to format the given +opts+ for Kernel#sprintf using
|
|
|
|
# #parse_printf_format
|
2005-04-13 11:30:15 -04:00
|
|
|
def printf(format, *opts)
|
|
|
|
if /(%*)%I/ =~ format
|
|
|
|
format, opts = parse_printf_format(format, opts)
|
|
|
|
end
|
|
|
|
print sprintf(format, *opts)
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns an array of the given +format+ and +opts+ to be used by
|
|
|
|
# Kernel#sprintf, if there was a successful Regexp match in the given
|
|
|
|
# +format+ from #printf
|
|
|
|
#
|
|
|
|
# %
|
|
|
|
# <flag> [#0- +]
|
|
|
|
# <minimum field width> (\*|\*[1-9][0-9]*\$|[1-9][0-9]*)
|
|
|
|
# <precision>.(\*|\*[1-9][0-9]*\$|[1-9][0-9]*|)?
|
|
|
|
# #<length modifier>(hh|h|l|ll|L|q|j|z|t)
|
|
|
|
# <conversion specifier>[diouxXeEfgGcsb%]
|
2005-04-13 11:30:15 -04:00
|
|
|
def parse_printf_format(format, opts)
|
|
|
|
return format, opts if $1.size % 2 == 1
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Calls #print on each element in the given +objs+, followed by a newline
|
|
|
|
# character.
|
2005-04-13 11:30:15 -04:00
|
|
|
def puts(*objs)
|
|
|
|
for obj in objs
|
|
|
|
print(*obj)
|
|
|
|
print "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Prints the given +objs+ calling Object#inspect on each.
|
|
|
|
#
|
|
|
|
# See #puts for more detail.
|
2005-04-13 11:30:15 -04:00
|
|
|
def pp(*objs)
|
2005-04-14 02:16:08 -04:00
|
|
|
puts(*objs.collect{|obj| obj.inspect})
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Prints the given +objs+ calling Object#inspect on each and appending the
|
|
|
|
# given +prefix+.
|
|
|
|
#
|
|
|
|
# See #puts for more detail.
|
2005-04-13 11:30:15 -04:00
|
|
|
def ppx(prefix, *objs)
|
2005-04-14 02:16:08 -04:00
|
|
|
puts(*objs.collect{|obj| prefix+obj.inspect})
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# A standard output printer
|
2005-04-13 11:30:15 -04:00
|
|
|
class StdioOutputMethod<OutputMethod
|
2012-12-21 00:45:50 -05:00
|
|
|
# Prints the given +opts+ to standard output, see IO#print for more
|
|
|
|
# information.
|
2005-04-13 11:30:15 -04:00
|
|
|
def print(*opts)
|
2005-04-14 02:16:08 -04:00
|
|
|
STDOUT.print(*opts)
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|