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.



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

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.



5
6
7
# File 'lib/choosy/printing/base_printer.rb', line 5

def formatting_options
  @formatting_options
end

#heading_stylesObject

Returns the value of attribute heading_styles.



5
6
7
# File 'lib/choosy/printing/base_printer.rb', line 5

def heading_styles
  @heading_styles
end

#indentObject

Returns the value of attribute indent.



5
6
7
# File 'lib/choosy/printing/base_printer.rb', line 5

def indent
  @indent
end

#offsetObject

Returns the value of attribute offset.



5
6
7
# File 'lib/choosy/printing/base_printer.rb', line 5

def offset
  @offset
end

#option_stylesObject

Returns the value of attribute option_styles.



5
6
7
# File 'lib/choosy/printing/base_printer.rb', line 5

def option_styles
  @option_styles
end

Instance Method Details

#command_name(command) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/choosy/printing/base_printer.rb', line 128

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



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

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



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

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

#format_element(item) ⇒ Object



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

def format_element(item)
  # Override
end

#format_epilogue(command) ⇒ Object



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

def format_epilogue(command)
  # Override
end

#format_option(option, formatted_prefix, indent) ⇒ Object



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

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

#format_prologue(command) ⇒ Object



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

def format_prologue(command)
  # Override
end

#line_countObject



45
46
47
# File 'lib/choosy/printing/base_printer.rb', line 45

def line_count
  # Override
end

#print!(command) ⇒ Object



23
24
25
# File 'lib/choosy/printing/base_printer.rb', line 23

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

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



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

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



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

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



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

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