123456789_123456789_123456789_123456789_123456789_

Class: IRB::Pager::PageOverflowIO

Relationships & Source Files
Inherits: Object
Defined in: lib/irb/pager.rb

Overview

Writable IO that has page overflow callback

Constant Summary

  • MAX_CHAR_PER_CELL =

    Maximum size of a single cell in terminal Assumed worst case: “e[1;3;4;9;38;2;255;128;128;48;2;128;128;255mAe[0m” bold, italic, underline, crossed_out, RGB forgound, RGB background

    # File 'lib/irb/pager.rb', line 139
    50

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(width, height, overflow_callback, delay: nil) ⇒ PageOverflowIO

[ GitHub ]

  
# File 'lib/irb/pager.rb', line 141

def initialize(width, height, overflow_callback, delay: nil)
  @lines = []
  @first_page_lines = nil
  @width = width
  @height = height
  @buffer = +''
  @overflow_callback = overflow_callback
  @col = 0
  @string = +''
  @multipage = false
  @delay_until = (Time.now + delay if delay)
end

Instance Attribute Details

#first_page_lines (readonly)

[ GitHub ]

  
# File 'lib/irb/pager.rb', line 134

attr_reader :string, :first_page_lines

#multipage?Boolean (readonly)

[ GitHub ]

  
# File 'lib/irb/pager.rb', line 205

def multipage?
  @multipage
end

#string (readonly)

[ GitHub ]

  
# File 'lib/irb/pager.rb', line 134

attr_reader :string, :first_page_lines

Instance Method Details

#<<(text)

Alias for #write.

[ GitHub ]

  
# File 'lib/irb/pager.rb', line 210

alias << write

#puts(text = '')

[ GitHub ]

  
# File 'lib/irb/pager.rb', line 154

def puts(text = '')
  text = text.to_s unless text.is_a?(String)
  write(text)
  write("\n") unless text.end_with?("\n")
end

#write(text) Also known as: #print, #<<

[ GitHub ]

  
# File 'lib/irb/pager.rb', line 160

def write(text)
  text = text.to_s unless text.is_a?(String)
  @string << text
  if @multipage
    if @delay_until && Time.now > @delay_until
      @overflow_callback.call(@first_page_lines)
      @delay_until = nil
    end
    return
  end

  overflow_size = (@width * (@height - @lines.size) + @width - @col) * MAX_CHAR_PER_CELL
  if text.size >= overflow_size
    text = text[0, overflow_size]
    overflow = true
  end
  @buffer << text
  @col += Reline::Unicode.calculate_width(text, true)
  if text.include?("\n") || @col >= @width
    @buffer.lines.each do |line|
      wrapped_lines = Reline::Unicode.split_by_width(line.chomp, @width).first.compact
      wrapped_lines.pop if wrapped_lines.last == ''
      @lines.concat(wrapped_lines)
      if line.end_with?("\n")
        if @lines.empty? || @lines.last.end_with?("\n")
          @lines << "\n"
        else
          @lines[-1] += "\n"
        end
      end
    end
    @buffer.clear
    @buffer << @lines.pop unless @lines.last.end_with?("\n")
    @col = Reline::Unicode.calculate_width(@buffer, true)
  end
  if overflow || @lines.size > @height || (@lines.size == @height && @col > 0)
    @first_page_lines = @lines.take(@height)
    if !@delay_until || Time.now > @delay_until
      @overflow_callback.call(@first_page_lines)
      @delay_until = nil
    end
    @multipage = true
  end
end