[18425] | 1 | #
|
---|
| 2 | # output-method.rb - optput methods used by irb
|
---|
| 3 | # $Release Version: 0.9.5$
|
---|
| 4 | # $Revision: 11708 $
|
---|
| 5 | # $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
|
---|
| 6 | # by Keiju ISHITSUKA([email protected])
|
---|
| 7 | #
|
---|
| 8 | # --
|
---|
| 9 | #
|
---|
| 10 | #
|
---|
| 11 | #
|
---|
| 12 |
|
---|
| 13 | require "e2mmap"
|
---|
| 14 |
|
---|
| 15 | module IRB
|
---|
| 16 | # OutputMethod
|
---|
| 17 | # StdioOutputMethod
|
---|
| 18 |
|
---|
| 19 | class OutputMethod
|
---|
| 20 | @RCS_ID='-$Id: output-method.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
|
---|
| 21 |
|
---|
| 22 | def print(*opts)
|
---|
| 23 | IRB.fail NotImplementError, "print"
|
---|
| 24 | end
|
---|
| 25 |
|
---|
| 26 | def printn(*opts)
|
---|
| 27 | print opts.join(" "), "\n"
|
---|
| 28 | end
|
---|
| 29 |
|
---|
| 30 | # extend printf
|
---|
| 31 | def printf(format, *opts)
|
---|
| 32 | if /(%*)%I/ =~ format
|
---|
| 33 | format, opts = parse_printf_format(format, opts)
|
---|
| 34 | end
|
---|
| 35 | print sprintf(format, *opts)
|
---|
| 36 | end
|
---|
| 37 |
|
---|
| 38 | # %
|
---|
| 39 | # <¥Õ¥é¥°> [#0- +]
|
---|
| 40 | # <ºÇŸ®¥Õ¥£¡Œ¥ë¥ÉÉý> (\*|\*[1-9][0-9]*\$|[1-9][0-9]*)
|
---|
| 41 | # <ÀºÅÙ>.(\*|\*[1-9][0-9]*\$|[1-9][0-9]*|)?
|
---|
| 42 | # #<Ĺ€µœ€ÀµÊž»ú>(hh|h|l|ll|L|q|j|z|t)
|
---|
| 43 | # <ÊÑŽ¹œ€ÀµÊž»ú>[diouxXeEfgGcsb%]
|
---|
| 44 | def parse_printf_format(format, opts)
|
---|
| 45 | return format, opts if $1.size % 2 == 1
|
---|
| 46 | end
|
---|
| 47 |
|
---|
| 48 | def foo(format)
|
---|
| 49 | pos = 0
|
---|
| 50 | inspects = []
|
---|
| 51 | format.scan(/%[#0\-+ ]?(\*(?=[^0-9])|\*[1-9][0-9]*\$|[1-9][0-9]*(?=[^0-9]))?(\.(\*(?=[^0-9])|\*[1-9][0-9]*\$|[1-9][0-9]*(?=[^0-9])))?(([1-9][0-9]*\$)*)([diouxXeEfgGcsb%])/) {|f, p, pp, pos, new_pos, c|
|
---|
| 52 | puts [f, p, pp, pos, new_pos, c].join("!")
|
---|
| 53 | pos = new_pos if new_pos
|
---|
| 54 | if c == "I"
|
---|
| 55 | inspects.push pos.to_i
|
---|
| 56 | (f||"")+(p||"")+(pp||"")+(pos||"")+"s"
|
---|
| 57 | else
|
---|
| 58 | $&
|
---|
| 59 | end
|
---|
| 60 | }
|
---|
| 61 | end
|
---|
| 62 |
|
---|
| 63 | def puts(*objs)
|
---|
| 64 | for obj in objs
|
---|
| 65 | print(*obj)
|
---|
| 66 | print "\n"
|
---|
| 67 | end
|
---|
| 68 | end
|
---|
| 69 |
|
---|
| 70 | def pp(*objs)
|
---|
| 71 | puts(*objs.collect{|obj| obj.inspect})
|
---|
| 72 | end
|
---|
| 73 |
|
---|
| 74 | def ppx(prefix, *objs)
|
---|
| 75 | puts(*objs.collect{|obj| prefix+obj.inspect})
|
---|
| 76 | end
|
---|
| 77 |
|
---|
| 78 | end
|
---|
| 79 |
|
---|
| 80 | class StdioOutputMethod<OutputMethod
|
---|
| 81 | def print(*opts)
|
---|
| 82 | STDOUT.print(*opts)
|
---|
| 83 | end
|
---|
| 84 | end
|
---|
| 85 | end
|
---|