Gist::Manpage

Public Instance Methods

display(name) click to toggle source

Prints a manpage, all pretty and paged.

# File lib/gist/manpage.rb, line 6
def display(name)
  puts manpage(name)
end
groff?() click to toggle source

Returns true if groff is installed and in our path, false if not.

# File lib/gist/manpage.rb, line 42
def groff?
  system("which groff > /dev/null")
end
groff_command() click to toggle source

The groff command complete with crazy arguments we need to run in order to turn our raw roff (manpage markup) into something readable on the terminal.

# File lib/gist/manpage.rb, line 49
def groff_command
  "groff -Wall -mtty-char -mandoc -Tascii"
end
manpage(name) click to toggle source

Returns the terminal-formatted manpage, ready to be printed to the screen.

# File lib/gist/manpage.rb, line 12
def manpage(name)
  return "** Can't find groff(1)" unless groff?

  require 'open3'
  out = nil
  Open3.popen3(groff_command) do |stdin, stdout, _|
    stdin.puts raw_manpage(name)
    stdin.close
    out = stdout.read.strip
  end
  out
end
page_stdout() click to toggle source

nex-3.com/posts/73-git-style-automatic-paging-in-ruby

# File lib/gist/manpage.rb, line 60
def page_stdout
  return unless $stdout.tty?

  read, write = IO.pipe

  if Kernel.fork
    # Parent process, become pager
    $stdin.reopen(read)
    read.close
    write.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 = ENV['PAGER'] || 'less -isr'
    pager = 'cat' if pager.empty?

    exec pager rescue exec "/bin/sh", "-c", pager
  else
    # Child process
    $stdout.reopen(write)
    $stderr.reopen(write) if $stderr.tty?
    read.close
    write.close
  end
end
puts(*args) click to toggle source

All calls to `puts` are paged, git-style.

# File lib/gist/manpage.rb, line 54
def puts(*args)
  page_stdout
  super
end
raw_manpage(name) click to toggle source

Returns the raw manpage. If we're not running in standalone mode, it's a file sitting at the root under the `man` directory.

If we are running in standalone mode the manpage will be included after the __END__ of the file so we can grab it using DATA.

# File lib/gist/manpage.rb, line 32
def raw_manpage(name)
  if File.exists? file = File.dirname(__FILE__) + "/../../man/#{name}.1"
    File.read(file)
  else
    DATA.read.split("__CACERT__").first
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.