Module: GithubCLI::Manpage

Extended by:
Manpage
Included in:
Manpage
Defined in:
lib/github_cli/manpage.rb

Instance Method Summary collapse

Instance Method Details

#available_commandObject


95
96
97
98
99
100
101
102
103
# File 'lib/github_cli/manpage.rb', line 95

def available_command
  if groff?
    groff
  elsif man?
    'man'
  else
    nil
  end
end

#base_rootObject


11
12
13
# File 'lib/github_cli/manpage.rb', line 11

def base_root
  File.dirname(__FILE__)
end

#groffObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The groff command with extra arguments to turn roff into terminal output


58
59
60
# File 'lib/github_cli/manpage.rb', line 58

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

#groff?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if groff is installed on the system

Returns:

  • (Boolean)

64
65
66
# File 'lib/github_cli/manpage.rb', line 64

def groff?
  TTY::Which.exist?('groff')
end

#man?Boolean

Check if man is installed on the system

Returns:

  • (Boolean)

69
70
71
# File 'lib/github_cli/manpage.rb', line 69

def man?
  TTY::Which.exist?('man')
end

#man_dir(path = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Full path to manual directory

Returns:

  • (String)

20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/github_cli/manpage.rb', line 20

def man_dir(path = nil)
  if @man_dir.nil? || path
    man_path = path || File.expand_path('../man', __FILE__)

    if File.directory?(man_path)
      @man_dir = man_path
    else
      fail "Manuals directory `#{man_path}` does not exist"
    end
  end
  @man_dir
end

#manpage?(name, section = nil) ⇒ Boolean

Check if manual exists for a command

Parameters:

  • command (String)

    the command name

Returns:

  • (Boolean)

48
49
50
51
52
# File 'lib/github_cli/manpage.rb', line 48

def manpage?(name, section = nil)
  return false if name.nil?

  manpages(name, section).any?
end

#manpages(name = nil, section = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Paths to manpages included in this library


36
37
38
39
40
# File 'lib/github_cli/manpage.rb', line 36

def manpages(name = nil, section = nil)
  Dir.entries(man_dir).select do |file|
    file =~ /(.+)#{name}\.#{section || '\d'}$/
  end
end

#read(name, section = nil) ⇒ Object

Read manual page


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/github_cli/manpage.rb', line 76

def read(name, section = nil)
  return if name.nil?
  paths = manpages(name)
  return if paths.empty?

  if paths.size == 1
    manpath = paths[0]
  elsif paths.size > 1
    prompt = TTY::Prompt.new
    manpath = prompt.select("Choose manual to view?", paths)
  end

  if manpath
    run(manpath)
  else
    abort("No manuals found for #{name}")
  end
end

#run(manpath) ⇒ Object


105
106
107
108
109
110
111
112
# File 'lib/github_cli/manpage.rb', line 105

def run(manpath)
  fullpath = File.join(man_dir, manpath)
  if !(cmd = available_command).nil?
    system("#{cmd} #{fullpath} | #{Pager.pager_command}")
  else
    system("#{Pager.pager_command} #{fullpath}.txt")
  end
end