Class: GithubCLI::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/pager.rb

Overview

This class provides pagining of terminal output.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Pager

Returns a new instance of Pager


35
36
37
# File 'lib/github_cli/pager.rb', line 35

def initialize(options={})
  @pager_command = options[:pager_command] if options[:pager_command]
end

Class Method Details

.disableObject


30
31
32
# File 'lib/github_cli/pager.rb', line 30

def disable
  @enabled = false
end

.enableObject


26
27
28
# File 'lib/github_cli/pager.rb', line 26

def enable
  @enabled = true
end

.enabled?Boolean

Indicates if pager is enabled

Returns:

  • (Boolean)

22
23
24
# File 'lib/github_cli/pager.rb', line 22

def enabled?
  @enabled && true
end

.pager_command(*cmds) ⇒ Object


10
11
12
13
14
15
16
17
18
19
# File 'lib/github_cli/pager.rb', line 10

def pager_command(*cmds)
  @pager_command = (!@pager_command.nil? && cmds.empty?) || begin
    commands = [
      ENV['GIT_PAGER'], `git config --get-all core.pager`.split.first,
      ENV['PAGER'], 'less -isr', 'more', 'cat', 'pager'
    ]
    commands = cmds + commands
    commands.compact.uniq.find { |cmd| System.command? cmd }
  end
end

Instance Method Details

#pageObject

Pages output using configured pager.


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/github_cli/pager.rb', line 40

def page
  return if not $stdout.tty?

  read_io, write_io = IO.pipe

  if Kernel.fork
    $stdin.reopen(read_io)
    read_io.close
    write_io.close

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

    # Wait until we have input before we start the pager
    Kernel.select [$stdin]

    pager = Pager.pager_command

    Kernel.exec pager rescue Kernel.exec "/bin/sh", "-c", pager
  else
    # Child process
    $stdout.reopen(write_io)
    $stderr.reopen(write_io) if $stderr.tty?
    write_io.close
    read_io.close
  end
end