-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display
show_cmds
's output in a pager when in TTY environment
This can: - Make it easier to scroll up and down the commands list - Avoid pushing up users' previous output - Allow users to do basic search with `/<word>`
- Loading branch information
Showing
4 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module IRB | ||
# The implementation of this class is borrowed from RDoc's lib/rdoc/ri/driver.rb. | ||
# Please do NOT use this class directly outside of IRB. | ||
class Pager | ||
PAGE_COMMANDS = [ENV['RI_PAGER'], ENV['PAGER'], 'pager', 'less -r', 'more -r'].compact.uniq | ||
|
||
class << self | ||
def page | ||
if pager = setup_pager | ||
begin | ||
yield pager | ||
ensure | ||
pager.close | ||
end | ||
else | ||
yield $stdout | ||
end | ||
rescue Errno::EPIPE | ||
end | ||
|
||
private | ||
|
||
def setup_pager | ||
require 'shellwords' | ||
|
||
PAGE_COMMANDS.each do |pager| | ||
pager = Shellwords.split(pager) | ||
next if pager.empty? | ||
|
||
begin | ||
io = IO.popen(pager, 'w') | ||
rescue | ||
next | ||
end | ||
|
||
if $? && $?.pid == io.pid && $?.exited? # pager didn't work | ||
next | ||
end | ||
|
||
return io | ||
end | ||
|
||
nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters