Module: I3::Manpage

Extended by:
Manpage
Included in:
Manpage
Defined in:
lib/i3-ipc/manpage.rb

Instance Method Summary collapse

Instance Method Details

#display(name) ⇒ Object

Prints a manpage, all pretty and paged.



6
7
8
# File 'lib/i3-ipc/manpage.rb', line 6

def display(name)
  puts manpage(name)
end

#groff?Boolean

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/i3-ipc/manpage.rb', line 42

def groff?
  system("which groff > /dev/null")
end

#groff_commandObject

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.



49
50
51
# File 'lib/i3-ipc/manpage.rb', line 49

def groff_command
  "groff -Wall -mtty-char -mandoc -Tascii"
end

#manpage(name) ⇒ Object

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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/i3-ipc/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_stdoutObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/i3-ipc/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) ⇒ Object

All calls to puts are paged, git-style.



54
55
56
57
# File 'lib/i3-ipc/manpage.rb', line 54

def puts(*args)
  page_stdout
  super
end

#raw_manpage(name) ⇒ Object

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.



32
33
34
35
36
37
38
# File 'lib/i3-ipc/manpage.rb', line 32

def raw_manpage(name)
  if File.exists? file = File.dirname(__FILE__) + "/../../man/#{name}.1"
    File.read(file)
  else
    DATA.read
  end
end