Class: ShellOpts::Formatter
- Inherits:
-
Object
- Object
- ShellOpts::Formatter
- Defined in:
- lib/shellopts/formatter.rb
Constant Summary collapse
- MARGIN_RIGHT =
Right margin
3- USAGE_STRING =
String for ‘Usage’ in error messages
"Usage"- USAGE_INDENT =
Indent to use in usage output
USAGE_STRING.size
- USAGE_MAX_WIDTH =
Width of usage (after usage string)
70- BRIEF_INDENT =
Indent to use in brief output
2- BRIEF_COL_SEP =
Number of characters between columns in brief output
2- BRIEF_COL1_MIN_WIDTH =
Maximum width of first column in brief option and command lists
6- BRIEF_COL1_MAX_WIDTH =
Maximum width of first column in brief option and command lists
40- BRIEF_COL2_MAX_WIDTH =
Minimum width of second column in brief option and command lists
50- HELP_INDENT =
Indent to use in help output
4
Class Method Summary collapse
-
.brief(command) ⇒ Object
When the user gives a -h option.
-
.command_of(obj) ⇒ Object
Short-hand to get the Grammar::Command object.
-
.command_prefix ⇒ Object
Command prefix when subject is a sub-command.
- .compute_columns(width, fields) ⇒ Object
-
.help(subject) ⇒ Object
When the user gives a –help option.
-
.puts_columns(widths, fields) ⇒ Object
# TODO def self.help_w_lambda(program) if @help_lambda # else program = Grammar::Command.command(program) setup_indent(HELP_INDENT) { program.puts_descr } end end.
- .rest ⇒ Object
-
.usage(subject) ⇒ Object
Usage string in error messages.
- .width ⇒ Object
Class Method Details
.brief(command) ⇒ Object
When the user gives a -h option
233 234 235 236 237 |
# File 'lib/shellopts/formatter.rb', line 233 def self.brief(command) command = Grammar::Command.command(command) @command_prefix = command.ancestors.map { |node| node.name + " " }.join setup_indent(BRIEF_INDENT) { command.puts_brief } end |
.command_of(obj) ⇒ Object
Short-hand to get the Grammar::Command object
251 252 253 254 |
# File 'lib/shellopts/formatter.rb', line 251 def self.command_of(obj) constrain obj, Grammar::Command, ::ShellOpts::Program obj.is_a?(Grammar::Command) ? obj : obj.__grammar__ end |
.command_prefix ⇒ Object
Command prefix when subject is a sub-command
216 |
# File 'lib/shellopts/formatter.rb', line 216 def self.command_prefix() @command_prefix end |
.compute_columns(width, fields) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/shellopts/formatter.rb', line 286 def self.compute_columns(width, fields) first_max = [ (fields.map { |first, _| first.size } + [BRIEF_COL1_MIN_WIDTH]).max, BRIEF_COL1_MAX_WIDTH ].min second_max = fields.map { |_, second| second ? second&.map(&:size).sum + second.size - 1: 0 }.max if first_max + BRIEF_COL_SEP + second_max <= width first_width = first_max second_width = second_max elsif first_max + BRIEF_COL_SEP + BRIEF_COL2_MAX_WIDTH <= width first_width = first_max second_width = width - first_width - BRIEF_COL_SEP else first_width = [width - BRIEF_COL_SEP - BRIEF_COL2_MAX_WIDTH, BRIEF_COL1_MAX_WIDTH].min second_width = BRIEF_COL2_MAX_WIDTH end [first_width, second_width] end |
.help(subject) ⇒ Object
When the user gives a –help option
244 245 246 247 248 |
# File 'lib/shellopts/formatter.rb', line 244 def self.help(subject) subject = Grammar::Command.command(subject) @command_prefix = subject.ancestors.map { |node| node.name + " " }.join setup_indent(HELP_INDENT) { subject.puts_help } end |
.puts_columns(widths, fields) ⇒ Object
# TODO
def self.help_w_lambda(program)
if @help_lambda
#
else
program = Grammar::Command.command(program)
setup_indent(HELP_INDENT) { program.puts_descr }
end
end
def self.help=(help_lambda) @help_lambda end
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/shellopts/formatter.rb', line 268 def self.puts_columns(widths, fields) l = [] first_width, second_width = *widths second_col = first_width + 2 for (first, second) in fields if first.size > first_width puts first indent(first_width + BRIEF_COL_SEP, ' ') { puts second.wrap(second_width) } if second elsif second printf "%-#{first_width + BRIEF_COL_SEP}s", first indent(first_width, bol: false) { puts second.wrap(second_width) } else puts first end end end |
.rest ⇒ Object
311 |
# File 'lib/shellopts/formatter.rb', line 311 def self.rest() width - $stdout.margin end |
.usage(subject) ⇒ Object
Usage string in error messages
219 220 221 222 223 224 225 226 |
# File 'lib/shellopts/formatter.rb', line 219 def self.usage(subject) subject = Grammar::Command.command(subject) @command_prefix = subject.ancestors.map { |node| node.name + " " }.join setup_indent(1) { print lead = "#{USAGE_STRING}: " indent(lead.size, ' ', bol: false) { subject.puts_usage } } end |
.width ⇒ Object
307 308 309 |
# File 'lib/shellopts/formatter.rb', line 307 def self.width() @width ||= TermInfo.screen_width - MARGIN_RIGHT end |