Skip to content

Commit

Permalink
Add Linux pager support for cartridges list and help
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Apr 8, 2013
1 parent 14bacd4 commit 1b67e63
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rhc/command_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def run_help(args=[], options=nil)
say "See '#{program :name} help' for a list of valid commands."
1
else
RHC::Helpers.pager
command = command(cmd)
help_bindings = CommandHelpBindings.new command, commands, self
say help_formatter.render_command help_bindings
Expand Down
2 changes: 2 additions & 0 deletions lib/rhc/commands/cartridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Cartridge < Base
def list
carts = rest_client.cartridges.sort_by{ |c| "#{c.type == 'standalone' && 1}_#{c.tags.include?('experimental') ? 1 : 0}_#{(c.display_name || c.name).downcase}" }

pager

if options.verbose
carts.each do |c|
paragraph do
Expand Down
2 changes: 1 addition & 1 deletion lib/rhc/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def color(*args)
$terminal.color(*args)
end

[:indent, :paragraph, :section, :header, :table, :table_args].each do |sym|
[:pager, :indent, :paragraph, :section, :header, :table, :table_args].each do |sym|
define_method(sym) do |*args, &block|
$terminal.send(sym, *args, &block)
end
Expand Down
28 changes: 28 additions & 0 deletions lib/rhc/highline_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ def paragraph(&block)
section(:top => 1, :bottom => 1, &block)
end

def pager
#:nocov:
return if RUBY_PLATFORM =~ /win32/
return unless @output.tty?

read, write = IO.pipe

unless Kernel.fork # Child process
STDOUT.reopen(write)
STDERR.reopen(write) if STDERR.tty?
read.close
write.close
return
end

# Parent process, become pager
STDIN.reopen(read)
read.close
write.close

ENV['LESS'] = 'FSRX' # Don't page if the input is short enough

Kernel.select [STDIN] # Wait until we have input before we start the pager
pager = ENV['PAGER'] || 'less'
exec pager rescue exec "/bin/sh", "-c", pager
#:nocov:
end

private
def separate_blocks
if (@margin ||= 0) > 0 && !@last_line_open
Expand Down

0 comments on commit 1b67e63

Please sign in to comment.