Class: Choosy::Printing::BasePrinter

Inherits:
Object
  • Object
show all
Includes:
Terminal
Defined in:
lib/choosy/printing/base_printer.rb

Direct Known Subclasses

HelpPrinter, ManpagePrinter

Constant Summary

Constants included from Terminal

Terminal::DEFAULT_COLUMN_COUNT, Terminal::DEFAULT_LINE_COUNT

Instance Attribute Summary collapse

Attributes included from Terminal

#columns, #lines

Instance Method Summary collapse

Methods included from Terminal

#color, #command_exists?, die, #die, #page, #pager, #pager?, #pipe_in, #pipe_out, #stdin?, #unformatted

Constructor Details

#initialize(options) ⇒ BasePrinter

Returns a new instance of BasePrinter.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/choosy/printing/base_printer.rb', line 10

def initialize(options)
  @formatting_options = options

  @heading_styles = options[:heading_styles] || [:bold, :blue]
  @option_styles = options[:option_styles] || [:bold]
  @indent = options[:indent] || '  '
  @offset = options[:offset] || '    '

  if options[:color] == false
    self.color.disable!
  end
  if options[:max_width] && self.columns > options[:max_width]
    self.columns = options[:max_width]
  end
end

Instance Attribute Details

#formatting_optionsObject

Returns the value of attribute formatting_options.



8
9
10
# File 'lib/choosy/printing/base_printer.rb', line 8

def formatting_options
  @formatting_options
end

#heading_stylesObject

Returns the value of attribute heading_styles.



8
9
10
# File 'lib/choosy/printing/base_printer.rb', line 8

def heading_styles
  @heading_styles
end

#indentObject

Returns the value of attribute indent.



8
9
10
# File 'lib/choosy/printing/base_printer.rb', line 8

def indent
  @indent
end

#offsetObject

Returns the value of attribute offset.



8
9
10
# File 'lib/choosy/printing/base_printer.rb', line 8

def offset
  @offset
end

#option_stylesObject

Returns the value of attribute option_styles.



8
9
10
# File 'lib/choosy/printing/base_printer.rb', line 8

def option_styles
  @option_styles
end

Instance Method Details

#command_name(command) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/choosy/printing/base_printer.rb', line 131

def command_name(command)
  if command.is_a?(Choosy::Command) && command.subcommand?
    "#{command.parent.name} #{command.name}"
  else
    "#{command.name}"
  end
end

#format!(command) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/choosy/printing/base_printer.rb', line 30

def format!(command)
  format_prologue(command)

  cmd_indent, option_indent, prefixes = retrieve_formatting_info(command)
  command.listing.each_with_index do |item, i|
    case item
    when Choosy::Option
      format_option(item, prefixes[i], option_indent)
    when Choosy::Command
      format_command(item, prefixes[i], cmd_indent)
    when Choosy::Printing::FormattingElement
      format_element(item)
    end
  end

  format_epilogue(command)
end

#format_command(command, formatted_prefix, indent) ⇒ Object



60
61
62
# File 'lib/choosy/printing/base_printer.rb', line 60

def format_command(command, formatted_prefix, indent)
  # Override
end

#format_element(item) ⇒ Object



64
65
66
# File 'lib/choosy/printing/base_printer.rb', line 64

def format_element(item)
  # Override
end

#format_epilogue(command) ⇒ Object



68
69
70
# File 'lib/choosy/printing/base_printer.rb', line 68

def format_epilogue(command)
  # Override
end

#format_option(option, formatted_prefix, indent) ⇒ Object



56
57
58
# File 'lib/choosy/printing/base_printer.rb', line 56

def format_option(option, formatted_prefix, indent)
  # Override
end

#format_prologue(command) ⇒ Object



52
53
54
# File 'lib/choosy/printing/base_printer.rb', line 52

def format_prologue(command)
  # Override
end

#line_countObject



48
49
50
# File 'lib/choosy/printing/base_printer.rb', line 48

def line_count
  # Override
end

#print!(command) ⇒ Object



26
27
28
# File 'lib/choosy/printing/base_printer.rb', line 26

def print!(command)
  page format!(command)
end

#regular_option(option, value = "") ⇒ Object



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
# File 'lib/choosy/printing/base_printer.rb', line 99

def regular_option(option, value="")
  if option.short_flag
    value << highlight_begin
    value << option.short_flag
    value << highlight_end
    if option.long_flag
      value << ', '
    end
  else
    value << '    '
  end

  if option.long_flag
    value << highlight_begin
    if option.negated?
      value << '--['
      value << option.negation
      value << '-]'
      value << option.long_flag.gsub(/^--/, '')
    else
      value << option.long_flag
    end
    value << highlight_end
  end

  if option.metaname
    value << ' '
    value << option.metaname
  end
  value
end

#usage_option(option, value = "") ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/choosy/printing/base_printer.rb', line 72

def usage_option(option, value="")
  value << "["
  if option.short_flag
    value << option.short_flag
    if option.long_flag
      value << "|"
    end
  end
  if option.long_flag
    value << option.long_flag
  end
  if option.negated?
    value << '|'
    value << option.negated
  end
  if option.metaname
    if option.arity.max > 1
      value << ' '
      value << option.metaname
    else
      value << '='
      value << option.metaname
    end
  end
  value << ']'
end

#usage_wrapped(command, indent = '', columns = 80) ⇒ Object

doesn’t indent the first line



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/choosy/printing/base_printer.rb', line 140

def usage_wrapped(command, indent='', columns=80)
  columns = (columns > 70) ? 70 : columns
  lines = []
  line = command_name(command)
  starting_width = width = line.length + indent.length
  lines << line

  wrap = lambda do |part|
    if width + part.length > columns
      line = ' ' * starting_width
      lines << line
      line << ' ' << part
      width = starting_width + part.length
    else
      line << ' ' << part
      width += part.length
    end
  end

  command.listing.each do |option|
    if option.is_a?(Choosy::Option)
      formatted = usage_option(option)
      wrap.call(formatted)
    end
  end

  case command
  when Choosy::Command
    if command.arguments
      wrap.call(command.arguments.metaname)
    end
  when Choosy::SuperCommand
    wrap.call(command.metaname)
  end

  lines
end