Class: Pry::Pager::SystemPager

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

Overview

SystemPager buffers output until we're pretty sure it's at least a page long, then invokes an external pager and starts streaming output to it. If #close is called before then, it just prints out the buffered content.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NullPager

#height, #print, #puts, #width

Constructor Details

#initializeSystemPager

Returns a new instance of SystemPager



155
156
157
158
159
160
# File 'lib/pry/pager.rb', line 155

def initialize(*)
  super
  @tracker = PageTracker.new(height, width)
  @buffer  = ""
  @pager   = nil
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pry/pager.rb', line 137

def self.available?
  if @system_pager.nil?
    @system_pager = begin
      pager_executable = default_pager.split(' ').first
      if Pry::Helpers::BaseHelpers.windows? || Pry::Helpers::BaseHelpers.windows_ansi?
        `where #{pager_executable}`
      else
        `which #{pager_executable}`
      end
      $?.success?
    rescue
      false
    end
  else
    @system_pager
  end
end

.default_pagerObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/pry/pager.rb', line 124

def self.default_pager
  pager = ENV["PAGER"] || ""

  # Default to less, and make sure less is being passed the correct options
  if pager.strip.empty? or pager =~ /^less\b/
    pager = "less -R -F -X"
  end

  pager
end

Instance Method Details

#closeObject



177
178
179
180
181
182
183
# File 'lib/pry/pager.rb', line 177

def close
  if invoked_pager?
    pager.close
  else
    @out.puts @buffer
  end
end

#invoked_pager?Boolean (private)

Returns:

  • (Boolean)


191
192
193
# File 'lib/pry/pager.rb', line 191

def invoked_pager?
  @pager
end

#pagerObject (private)



195
196
197
# File 'lib/pry/pager.rb', line 195

def pager
  @pager ||= IO.popen(self.class.default_pager, 'w')
end

#write(str) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/pry/pager.rb', line 162

def write(str)
  if invoked_pager?
    write_to_pager str
  else
    @tracker.record str
    @buffer << str

    if @tracker.page?
      write_to_pager @buffer
    end
  end
rescue Errno::EPIPE
  raise StopPaging
end

#write_to_pager(text) ⇒ Object (private)



187
188
189
# File 'lib/pry/pager.rb', line 187

def write_to_pager(text)
  pager.write @out.decolorize_maybe(text)
end