Class: Pry::Pager::PageTracker

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

Overview

PageTracker tracks output to determine whether it's likely to take up a whole page. This doesn't need to be super precise, but we can use it for SimplePager and to avoid invoking the system pager unnecessarily.

One simplifying assumption is that we don't need #page? to return true on the basis of an incomplete line. Long lines should be counted as multiple lines, but we don't have to transition from false to true until we see a newline.

Instance Method Summary collapse

Constructor Details

#initialize(rows, cols) ⇒ PageTracker

Returns a new instance of PageTracker



209
210
211
212
# File 'lib/pry/pager.rb', line 209

def initialize(rows, cols)
  @rows, @cols = rows, cols
  reset
end

Instance Method Details

#line_length(line) ⇒ Object (private)

Approximation of the printable length of a given line, without the newline and without ANSI color codes.



238
239
240
# File 'lib/pry/pager.rb', line 238

def line_length(line)
  line.chomp.gsub(/\e\[[\d;]*m/, '').length
end

#page?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/pry/pager.rb', line 225

def page?
  @row >= @rows
end

#record(str) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/pry/pager.rb', line 214

def record(str)
  str.lines.each do |line|
    if line.end_with? "\n"
      @row += ((@col + line_length(line) - 1) / @cols) + 1
      @col  = 0
    else
      @col += line_length(line)
    end
  end
end

#resetObject



229
230
231
232
# File 'lib/pry/pager.rb', line 229

def reset
  @row = 0
  @col = 0
end