Class: RubyJard::Pager::LessPager

Inherits:
Pry::Pager::NullPager
  • Object
show all
Defined in:
lib/ruby_jard/pager.rb

Overview

Pager using GNU Less

Instance Method Summary collapse

Constructor Details

#initialize(pry_instance, force_open: false, pager_start_at_the_end: false, prompt: nil) ⇒ LessPager

Returns a new instance of LessPager.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_jard/pager.rb', line 41

def initialize(pry_instance, force_open: false, pager_start_at_the_end: false, prompt: nil)
  super(pry_instance.output)
  @pry_instance = pry_instance
  @buffer = ''

  @pager_start_at_the_end = pager_start_at_the_end
  @prompt = prompt

  # There are two cases:
  # - If the real pager (less) is triggered, it works on a real tty (fetched
  # from /dev/tty), in which, the same as RubyJard::Console.output
  # - Otherwise, it writes directly into pry's REPL output.
  # That's why there should be two output here
  @tty_output = RubyJard::Console.redirected? ? RubyJard::Console.output : pry_instance.output
  @window_width, @window_height = RubyJard::Console.screen_size(RubyJard::Console.output)
  @tracker = JardPageTracker.new(@window_height, @window_width)
  @pager = force_open ? open_pager : nil
end

Instance Method Details

#closeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_jard/pager.rb', line 75

def close
  if invoked_pager?
    @pager.close
    @pry_instance.exec_hook :after_pager, self

    list_prompt
  elsif @tracker.row > @window_height / 2
    @out.write @buffer

    list_prompt
  else
    @out.write @buffer
  end
end

#invoked_pager?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/ruby_jard/pager.rb', line 90

def invoked_pager?
  @pager
end

#list_promptObject



112
113
114
115
# File 'lib/ruby_jard/pager.rb', line 112

def list_prompt
  prompt = @pry_instance.prompt.wait_proc.call
  @out.puts "#{prompt}Tips: You can use `list` command to show back debugger screens"
end

#open_pagerObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby_jard/pager.rb', line 94

def open_pager
  @pry_instance.exec_hook :before_pager, self
  less_command = ['less', '-R', '-X']
  less_command << "--prompt \"#{@prompt}\"" if @prompt
  less_command << '+G' if @pager_start_at_the_end

  IO.popen(
    less_command.join(' '), 'w',
    out: @tty_output, err: @tty_output
  )
end

#write(str) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby_jard/pager.rb', line 60

def write(str)
  if invoked_pager?
    write_into_pager str
  else
    @tracker.record str
    @buffer += str
    if @tracker.page?
      @pager = open_pager
      write_into_pager(@buffer)
    end
  end
rescue Errno::EPIPE
  raise Pry::Pager::StopPaging
end

#write_into_pager(str) ⇒ Object



106
107
108
109
110
# File 'lib/ruby_jard/pager.rb', line 106

def write_into_pager(str)
  return unless invoked_pager?

  @pager.write str.encode('UTF-8', undef: :replace)
end