Sha256: 6e6bf41c4852bbd34c8ef18a9fc7fb2c368bff8cf6b68d99d5f9af6a352ca798

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

# encoding: utf-8

module GithubCLI
  # This class provides pagining of terminal output.
  class Pager
    class << self

      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

      # Indicates if pager is enabled
      def enabled?
        @enabled && true
      end

      def enable
        @enabled = true
      end

      def disable
        @enabled = false
      end
    end

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

    # Pages output using configured pager.
    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
  end # System
end # GithubCLI

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
github_cli-0.4.4 lib/github_cli/pager.rb
github_cli-0.4.3 lib/github_cli/pager.rb
github_cli-0.4.2 lib/github_cli/pager.rb
github_cli-0.4.1 lib/github_cli/pager.rb
github_cli-0.4.0 lib/github_cli/pager.rb