Sha256: 7ac3de7d871f92d182f2d8858715947bc8cb7a8ef932190aa6819f970f2a0b8a

Contents?: true

Size: 833 Bytes

Versions: 10

Compression:

Stored size: 833 Bytes

Contents

module Logaling::Command
  # http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
  module Pager
    def self.run
      return if windows?
      return unless STDOUT.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

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

    private
    def self.windows?
      RUBY_PLATFORM =~ /win32|mingw32/i
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
logaling-command-0.2.5 lib/logaling/command/pager.rb
logaling-command-0.2.4 lib/logaling/command/pager.rb
logaling-command-0.2.3 lib/logaling/command/pager.rb
logaling-command-0.2.2 lib/logaling/command/pager.rb
logaling-command-0.2.1 lib/logaling/command/pager.rb
logaling-command-0.2.0 lib/logaling/command/pager.rb
logaling-command-0.1.9 lib/logaling/command/pager.rb
logaling-command-0.1.8 lib/logaling/command/pager.rb
logaling-command-0.1.7 lib/logaling/command/pager.rb
logaling-command-0.1.6 lib/logaling/command/pager.rb