1
# help.rb: displaying the documentation of commands
2
# copyright (c) 2009 by Vincent Fourmond
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details (in the COPYING file).
14
require 'ctioga2/utils'
15
require 'ctioga2/commands/commands'
16
require 'ctioga2/commands/parsers/command-line'
17
require 'ctioga2/commands/doc/wordwrap'
21
Version::register_svn_info('$Revision: 132 $', '$Date: 2010-01-14 23:18:19 +0100 (Thu, 14 Jan 2010) $')
27
# Displays help about command-line options and such.
30
# How much space to leave for the options ?
31
attr_accessor :options_column_width
33
# How many columns do we have at all ?
34
attr_accessor :total_width
36
# Whether output has (moderate) terminal capabilities
39
# Whether we should send output to pager if output has
41
attr_accessor :to_pager
43
# Styles, ie a hash 'object' (option, argument...) => ANSI
50
# The default value for the #styles attribute.
58
# Creates an object to display command-line help. Available
59
# values for the options are given by the hash
60
# CommandLineHelpOptions. Their meaning is:
62
# * 'pager': disables or enables the use of a pager when
63
# sending output to a terminal
64
def initialize(options)
65
@options_column_width = 20
66
@to_pager = if options.key? 'pager'
72
@styles = DefaultStyles.dup
76
# Prints short help text suitable for a --help option about
77
# available commands, by groups (ungrouped last). It takes a
78
# list of all commands (_cmds_) and the list of _groups_ to
81
# \todo maybe the part about sending to the pager should be
82
# factorized into a neat utility class ?
83
def print_commandline_options(cmds, groups)
89
@total_width = Curses.cols
95
@total_width ||= 80 # 80 by default
97
# Disable color output if not a to a terminal
102
if @to_tty and @to_pager
103
# We pass -R as default value...
105
output = IO::popen("pager", "w")
113
output.puts unless group == groups[0]
114
name = (group && group.name) || "Ungrouped commands"
115
if group && group.blacklisted
116
name << " (blacklisted)"
118
output.puts style(name, 'title')
119
for cmd in cmds[group].sort {|a,b|
120
a.long_option <=> b.long_option
123
output.puts format_one_entry(cmd)
131
# Formats one entry of the commands
132
def format_one_entry(cmd)
133
sh, long, desc = cmd.option_strings
135
str = "#{leading_spaces}%2s%1s %-#{@options_column_width}s" %
136
[ sh, (sh ? "," : " "), long]
138
size = @total_width - total_leading_spaces.size
140
# Do the coloring: we need to parse option string first
141
if str =~ /(.*--\S+)(.*)/
144
str = "#{style(switch,'switch')}#{style(args,'arguments')}"
147
# Now, add the description.
148
desc_lines = WordWrapper.wrap(desc, size)
149
if long.size >= @options_column_width
150
str += "\n#{total_leading_spaces}"
152
str += desc_lines.join("\n#{total_leading_spaces}")
155
op_start = ' options: '
156
options = cmd.optional_arguments.
157
keys.sort.map { |x| "/#{x}"}.join(' ')
158
opts_lines = WordWrapper.wrap(options, size - op_start.size)
159
str += "\n#{total_leading_spaces}#{style(op_start,'switch')}" +
160
style(opts_lines.join("\n#{total_leading_spaces}#{' ' * op_start.size}"), 'options')
165
# Leading spaces to align a string with the other option texts
166
def total_leading_spaces
167
return "#{leading_spaces}#{" " *(@options_column_width + 4)}"
171
# Spaces before any 'short' option appears
176
# Colorizes some text with the given ANSI code.
178
# Word wrapping should be used *before*, as it will not work
180
def colorize(str, code)
181
# We split into lines, as I'm unsure color status is kept
183
return str.split("\n").map {|s|
184
"\e[#{code}m#{s}\e[0m"
188
# Changes the style of the object.
194
return colorize(str, @styles[what])