Class: CTioga2::Commands::Documentation::CommandLineHelp

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/commands/doc/help.rb

Overview

Displays help about command-line options and such.

Constant Summary collapse

DefaultStyles =

The default value for the #styles attribute.

{
  'switch' => "01",
  'title' => "01;04",
  'arguments' => '32',
  'options' => '34'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CommandLineHelp

Creates an object to display command-line help. Available values for the options are given by the hash CommandLineHelpOptions. Their meaning is:

  • ‘pager’: disables or enables the use of a pager when sending output to a terminal



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ctioga2/commands/doc/help.rb', line 62

def initialize(options)
  @options_column_width = 20
  @to_pager = if options.key? 'pager'
                options['pager']
              else
                true
              end

  @styles = DefaultStyles.dup
  @color = true
end

Instance Attribute Details

#colorObject

Color output ?



46
47
48
# File 'lib/ctioga2/commands/doc/help.rb', line 46

def color
  @color
end

#options_column_widthObject

How much space to leave for the options ?



29
30
31
# File 'lib/ctioga2/commands/doc/help.rb', line 29

def options_column_width
  @options_column_width
end

#stylesObject

Styles, ie a hash ‘object’ (option, argument…) => ANSI color code.



43
44
45
# File 'lib/ctioga2/commands/doc/help.rb', line 43

def styles
  @styles
end

#to_pagerObject

Whether we should send output to pager if output has terminal support.



39
40
41
# File 'lib/ctioga2/commands/doc/help.rb', line 39

def to_pager
  @to_pager
end

#to_ttyObject

Whether output has (moderate) terminal capabilities



35
36
37
# File 'lib/ctioga2/commands/doc/help.rb', line 35

def to_tty
  @to_tty
end

#total_widthObject

How many columns do we have at all ?



32
33
34
# File 'lib/ctioga2/commands/doc/help.rb', line 32

def total_width
  @total_width
end

Instance Method Details

Prints short help text suitable for a –help option about available commands, by groups (ungrouped last). It takes a list of all commands (cmds) and the list of groups to display.

todo maybe the part about sending to the pager should be factorized into a neat utility class ?



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ctioga2/commands/doc/help.rb', line 81

def print_commandline_options(cmds, groups)
  @to_tty = false
  if STDOUT.tty? 
    @to_tty = true
    begin
      require 'curses'
      Curses.init_screen
      @total_width = Curses.cols
      Curses.close_screen
    rescue LoadError => e
      # We'll be missing the exact term size
    end
  end
  @total_width ||= 80   # 80 by default

  # Disable color output if not a to a terminal
  if ! @to_tty
    @color = false
  end

  should_close = false
  output = $stdout

  if @to_tty and @to_pager
    # We pass -R as default value...
    ENV['LESS'] = 'R'
    pager = 'pager'
    for w in [ENV['PAGER'], 'less', 'pager' ]
      if Utils::which(w)
        pager = w
        break
      end
    end
    begin
      output = IO::popen(pager, "w")
      should_close = true
    rescue
    end
  end
  
  for group in groups
    output.puts unless group == groups[0]
    name = (group && group.name) || "Ungrouped commands"
    if group && group.blacklisted 
      name << " (blacklisted)"
    end
    output.puts style(name, 'title')
    for cmd in cmds[group].sort {|a,b|
        a.long_option <=> b.long_option
      }

      output.puts format_one_entry(cmd)
    end
  end
  output.close if should_close
end