Class: Skytap::Templates::Help

Inherits:
Base
  • Object
show all
Defined in:
lib/skytap/templates.rb

Constant Summary collapse

TEMPLATE =
File.expand_path(File.join(File.dirname(__FILE__), 'help_templates', 'help.erb'))

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#indent, #render

Class Method Details

.render(command) ⇒ Object



52
53
54
# File 'lib/skytap/templates.rb', line 52

def self.render(command)
  new('command' => command).render
end

Instance Method Details

#breaking_word_wrap(text, *args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/skytap/templates.rb', line 100

def breaking_word_wrap(text, *args)
  options = args.extract_options!
  unless args.blank?
    options[:line_width] = args[0] || 80
  end
  options.reverse_merge!(:line_width => 80)
  text = text.split(" ").collect do |word|
    word.length > options[:line_width] ? word.gsub(/(.{1,#{options[:line_width]}})/, "\\1 ") : word
  end * " "
    text.split("\n").collect do |line|
      line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
    end * "\n"
end

#choices(option, col_width) ⇒ Object



159
160
161
162
163
# File 'lib/skytap/templates.rb', line 159

def choices(option, col_width)
  if option.choices.present?
    hard_wrap("Options: #{option.choices.join(', ')}", col_width)
  end
end

#command_exists?(command) ⇒ Boolean

Determines if a shell command exists by searching for it in ENV.

Returns:

  • (Boolean)


91
92
93
# File 'lib/skytap/templates.rb', line 91

def command_exists?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
end

#default(option, col_width) ⇒ Object



153
154
155
156
157
# File 'lib/skytap/templates.rb', line 153

def default(option, col_width)
  if option.default.present? && option.show_default?
    hard_wrap("Default: #{option.default}", col_width)
  end
end

#description(option, col_width) ⇒ Object



147
148
149
150
151
# File 'lib/skytap/templates.rb', line 147

def description(option, col_width)
  if option.desc.present?
    hard_wrap(option.desc, col_width) << "\n"
  end
end

#description_cell(option, col_width) ⇒ Object



141
142
143
144
145
# File 'lib/skytap/templates.rb', line 141

def description_cell(option, col_width)
  [description(option, col_width),
    default(option, col_width),
    choices(option, col_width)].compact.join("\n")
end

#detect_terminal_sizeObject

TODO:NLA These two methods are from github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb#L61-71, which is under the MIT license. Figure out how to cite it properly.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/skytap/templates.rb', line 77

def detect_terminal_size
  if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
    [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
  elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
    [`tput cols`.to_i, `tput lines`.to_i]
  elsif STDIN.tty? && command_exists?('stty')
    `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
  else
    nil
  end
rescue
  nil
end

#error_msgObject



64
65
66
67
68
# File 'lib/skytap/templates.rb', line 64

def error_msg
  if command.error
    "Error: ".color(:red).bright << command.error
  end
end

#global_options_tableObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/skytap/templates.rb', line 114

def global_options_table
  min_width, max_width = 70, 120
  indentation = 5
  tty_cols = [detect_terminal_size.try(:first) || 80, min_width].max
  width = [tty_cols, max_width].min - indentation
  option_col_width = CommandLine.global_options.values.collect(&:signature).collect(&:length).max
  padding = 7

  table = Terminal::Table.new do |t|
    t.style = {:width => width}
    t.headings = ['Option'.underline, 'Description'.underline]
    desc_col_width = width - option_col_width - padding
    t.rows = CommandLine.global_options.values.inject([]) do |acc, opt|
      acc << [
        opt.signature,
        description_cell(opt, desc_col_width)
      ]

      # Add separator unless we're at the very end.
      acc << :separator unless acc.length == 2 * CommandLine.global_options.values.length - 1
      acc
    end
  end
  indent(table.to_s, indentation)
end

#hard_wrap(text, line_width) ⇒ Object



95
96
97
# File 'lib/skytap/templates.rb', line 95

def hard_wrap(text, line_width)
  breaking_word_wrap(text, :line_width => line_width)
end

#header(name) ⇒ Object



60
61
62
# File 'lib/skytap/templates.rb', line 60

def header(name)
  name.bright
end

#param_description(param, col_width) ⇒ Object



201
202
203
204
205
# File 'lib/skytap/templates.rb', line 201

def param_description(param, col_width)
  if param['description'].present?
    hard_wrap(param['description'], col_width) << "\n"
  end
end

#param_description_cell(param, col_width) ⇒ Object

TODO:NLA Lots of duplication here (see methods in class Help, above.



196
197
198
199
# File 'lib/skytap/templates.rb', line 196

def param_description_cell(param, col_width)
  [param_description(param, col_width),
    param_examples(param, col_width)].compact.join("\n")
end

#param_examples(param, col_width) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/skytap/templates.rb', line 207

def param_examples(param, col_width)
  if param['examples'].present?
    param['examples'].collect do |example|
      hard_wrap("Example: #{example}", col_width)
    end
  end
end

#parameters_tableObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/skytap/templates.rb', line 165

def parameters_table
  min_width, max_width = 70, 120
  indentation = 5
  tty_cols = [detect_terminal_size.try(:first) || 80, min_width].max
  width = [tty_cols, max_width].min - indentation
  left_col_width = command.parameters.collect{|name, _| name}.collect(&:length).max || 0
  padding = 7

  table = Terminal::Table.new do |t|
    t.style = {:width => width}
    t.headings = ['Parameter'.underline << "\n#{'*'.color(:cyan)} = required", 'Description'.underline]
    desc_col_width = width - left_col_width - padding

    t.rows = command.parameters.inject([]) do |acc, hash|
      name = hash.keys.first
      info = hash[name]

      acc << [
        info['required'] ? ('*'.color(:cyan) << name) : (' ' << name),
        param_description_cell(info, desc_col_width)
      ]

      # Add separator unless we're at the very end.
      acc << :separator unless acc.length == 2 * command.parameters.length - 1
      acc
    end
  end
  indent(table.to_s, indentation)
end

#subcommandsObject



70
71
72
# File 'lib/skytap/templates.rb', line 70

def subcommands
  @subcommands ||= command.class.subcommand_banners
end

#template_strObject



56
57
58
# File 'lib/skytap/templates.rb', line 56

def template_str
  File.open(TEMPLATE, &:read)
end