1#
2#   output-method.rb - output methods used by irb
3#   	$Release Version: 0.9.6$
4#   	$Revision: 38604 $
5#   	by Keiju ISHITSUKA(keiju@ruby-lang.org)
6#
7# --
8#
9#
10#
11
12require "e2mmap"
13
14module IRB
15  # An abstract output class for IO in irb. This is mainly used internally by
16  # IRB::Notifier. You can define your own output method to use with Irb.new,
17  # or Context.new
18  class OutputMethod
19    extend Exception2MessageMapper
20    def_exception :NotImplementedError, "Need to define `%s'"
21
22
23    # Open this method to implement your own output method, raises a
24    # NotImplementedError if you don't define #print in your own class.
25    def print(*opts)
26      OutputMethod.Raise NotImplementedError, "print"
27    end
28
29    # Prints the given +opts+, with a newline delimiter.
30    def printn(*opts)
31      print opts.join(" "), "\n"
32    end
33
34    # Extends IO#printf to format the given +opts+ for Kernel#sprintf using
35    # #parse_printf_format
36    def printf(format, *opts)
37      if /(%*)%I/ =~ format
38	format, opts = parse_printf_format(format, opts)
39      end
40      print sprintf(format, *opts)
41    end
42
43    # Returns an array of the given +format+ and +opts+ to be used by
44    # Kernel#sprintf, if there was a successful Regexp match in the given
45    # +format+ from #printf
46    #
47    #     %
48    #     <flag>  [#0- +]
49    #     <minimum field width> (\*|\*[1-9][0-9]*\$|[1-9][0-9]*)
50    #     <precision>.(\*|\*[1-9][0-9]*\$|[1-9][0-9]*|)?
51    #     #<length modifier>(hh|h|l|ll|L|q|j|z|t)
52    #     <conversion specifier>[diouxXeEfgGcsb%]
53    def parse_printf_format(format, opts)
54      return format, opts if $1.size % 2 == 1
55    end
56
57    # Calls #print on each element in the given +objs+, followed by a newline
58    # character.
59    def puts(*objs)
60      for obj in objs
61	print(*obj)
62	print "\n"
63      end
64    end
65
66    # Prints the given +objs+ calling Object#inspect on each.
67    #
68    # See #puts for more detail.
69    def pp(*objs)
70      puts(*objs.collect{|obj| obj.inspect})
71    end
72
73    # Prints the given +objs+ calling Object#inspect on each and appending the
74    # given +prefix+.
75    #
76    # See #puts for more detail.
77    def ppx(prefix, *objs)
78      puts(*objs.collect{|obj| prefix+obj.inspect})
79    end
80
81  end
82
83  # A standard output printer
84  class StdioOutputMethod<OutputMethod
85    # Prints the given +opts+ to standard output, see IO#print for more
86    # information.
87    def print(*opts)
88      STDOUT.print(*opts)
89    end
90  end
91end
92