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.


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

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

Class Method Details

.available?Boolean

Returns:

  • (Boolean)

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/pry/pager.rb', line 142

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

.default_pagerObject


130
131
132
133
134
135
136
137
138
# File 'lib/pry/pager.rb', line 130

def self.default_pager
  pager = Pry::Env['PAGER'] || ''

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

  pager
end

Instance Method Details

#closeObject


181
182
183
184
185
186
187
# File 'lib/pry/pager.rb', line 181

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

#invoked_pager?Boolean (private)

Returns:

  • (Boolean)

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

def invoked_pager?
  @pager
end

#pagerObject (private)


199
200
201
# File 'lib/pry/pager.rb', line 199

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

#write(str) ⇒ Object


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pry/pager.rb', line 168

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

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

#write_to_pager(text) ⇒ Object (private)


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

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