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.



33
34
35
# File 'lib/github_cli/pager.rb', line 33

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

Class Method Details

.disableObject



28
29
30
# File 'lib/github_cli/pager.rb', line 28

def disable
  @enabled = false
end

.enableObject



24
25
26
# File 'lib/github_cli/pager.rb', line 24

def enable
  @enabled = true
end

.enabled?Boolean

Indicates if pager is enabled

Returns:

  • (Boolean)


20
21
22
# File 'lib/github_cli/pager.rb', line 20

def enabled?
  @enabled && true
end

.pager_command(*cmds) ⇒ Object



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

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.



38
39
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
# File 'lib/github_cli/pager.rb', line 38

def page
  return if not $stdout.tty? or System.windows?

  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