Sha256: baa9214452e97444bb19552b29605219328275a375df63b65f2983116dde9a89

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

#
# Create scrollable output via less!
#
# This command runs `less` in a subprocess, and gives you the IO to its STDIN pipe
# so that you can communicate with it.
#
# Example:
#
#   lesspipe do |less|
#     50.times { less.puts "Hi mom!" }
#   end
#
# The default less parameters are:
# * Allow colour
# * Don't wrap lines longer than the screen
# * Quit immediately (without paging) if there's less than one screen of text.
#
# You can change these options by passing a hash to `lesspipe`, like so:
#
#   lesspipe(:wrap=>false) { |less| less.puts essay.to_s }
#
# It accepts the following boolean options:
#    :color  => Allow ANSI colour codes?
#    :wrap   => Wrap long lines?
#    :always => Always page, even if there's less than one page of text?
#
def lesspipe(*args)
  if args.any? and args.last.is_a?(Hash)
    options = args.pop
  else
    options = {}
  end

  output = args.first if args.any?

  params = []
  params << "-R" unless options[:color] == false
  params << "-S" unless options[:wrap] == true
  params << "-F" unless options[:always] == true
  if options[:tail] == true
    params << "+\\>"
    $stderr.puts "Seeking to end of stream..."
  end
  params << "-X"

  IO.popen("less #{params * ' '}", "w") do |less|
    if output
      less.puts output
    else
      yield less
    end
  end

rescue Errno::EPIPE, Interrupt
  # less just quit -- eat the exception.
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
upm-0.1.6 lib/upm/lesspipe.rb
upm-0.1.5 lib/upm/lesspipe.rb
upm-0.1.3 lib/upm/lesspipe.rb
upm-0.1.2 lib/upm/lesspipe.rb
upm-0.1.0 lib/upm/lesspipe.rb