Module: Jets::Thor::Help

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/jets/thor/help.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#capture_stdout_for_helpObject



10
11
12
13
14
15
16
17
# File 'lib/jets/thor/help.rb', line 10

def capture_stdout_for_help
  stdout_old = $stdout
  io = StringIO.new
  $stdout = io
  yield
  $stdout = stdout_old
  io.string
end

#help(command = nil, subcommand = false) ⇒ Object



5
6
7
8
# File 'lib/jets/thor/help.rb', line 5

def help(command = nil, subcommand = false)
  help_output = capture_stdout_for_help { super }
  paginate_output(help_output)
end

#paginate_output(output) ⇒ Object

Method to paginate the output using less if necessary



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jets/thor/help.rb', line 20

def paginate_output(output)
  unless system("type less > /dev/null 2>&1")
    puts output
    return
  end

  # Paginate the output if it's taller than the terminal
  terminal_height = TTY::Screen.height
  if output.lines.count > terminal_height
    IO.popen("less -R", "w") { |less| less.puts(output) }
  else
    puts output
  end
end