lib/irb/cmd/ls.rb in irb-1.7.4 vs lib/irb/cmd/ls.rb in irb-1.8.0

- old
+ new

@@ -1,9 +1,11 @@ # frozen_string_literal: true require "reline" +require "stringio" require_relative "nop" +require_relative "../pager" require_relative "../color" module IRB # :stopdoc: @@ -31,11 +33,11 @@ o.dump("constants", obj.constants) if obj.respond_to?(:constants) dump_methods(o, klass, obj) o.dump("instance variables", obj.instance_variables) o.dump("class variables", klass.class_variables) o.dump("locals", locals) - nil + o.print_result end def dump_methods(o, klass, obj) singleton_class = begin obj.singleton_class; rescue TypeError; nil end dumped_mods = Array.new @@ -75,32 +77,37 @@ MARGIN = " " def initialize(grep: nil) @grep = grep @line_width = screen_width - MARGIN.length # right padding + @io = StringIO.new end + def print_result + Pager.page_content(@io.string) + end + def dump(name, strs) strs = strs.grep(@grep) if @grep strs = strs.sort return if strs.empty? # Attempt a single line - print "#{Color.colorize(name, [:BOLD, :BLUE])}: " + @io.print "#{Color.colorize(name, [:BOLD, :BLUE])}: " if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length) - puts strs.join(MARGIN) + @io.puts strs.join(MARGIN) return end - puts + @io.puts # Dump with the largest # of columns that fits on a line cols = strs.size until fits_on_line?(strs, cols: cols, offset: MARGIN.length) || cols == 1 cols -= 1 end widths = col_widths(strs, cols: cols) strs.each_slice(cols) do |ss| - puts ss.map.with_index { |s, i| "#{MARGIN}%-#{widths[i]}s" % s }.join + @io.puts ss.map.with_index { |s, i| "#{MARGIN}%-#{widths[i]}s" % s }.join end end private