Class: IRB::Pager::PageOverflowIO

Inherits:
Object
  • Object
show all
Defined in:
lib/irb/pager.rb

Overview

Writable IO that has page overflow callback

Constant Summary collapse

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

50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, overflow_callback, delay: nil) ⇒ PageOverflowIO

Returns a new instance of PageOverflowIO.



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/irb/pager.rb', line 149

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_linesObject (readonly)

Returns the value of attribute first_page_lines.



142
143
144
# File 'lib/irb/pager.rb', line 142

def first_page_lines
  @first_page_lines
end

#stringObject (readonly)

Returns the value of attribute string.



142
143
144
# File 'lib/irb/pager.rb', line 142

def string
  @string
end

Instance Method Details

#multipage?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/irb/pager.rb', line 213

def multipage?
  @multipage
end

#puts(text = '') ⇒ Object



162
163
164
165
166
# File 'lib/irb/pager.rb', line 162

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

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/irb/pager.rb', line 168

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 if !@lines.empty? && !@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