Method: Cape::XTerm.format

Defined in:
lib/cape/xterm.rb

.format(string, *formats) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Applies the specified formats to string.

Parameters:

  • string (String)

    the string to format

  • formats (Array of Symbol)

    the formats to apply

Returns:

  • (String)

    the formatted string



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/cape/xterm.rb', line 286

def self.format(string, *formats)
  formatting_codes = formats.collect do |f|
    unless FORMATS.key?(f)
      raise ::ArgumentError, "Unrecognized format #{f.inspect}"
    end

    FORMATS[f]
  end

  return string if formatting_codes.empty? || string.nil?

  "\e[#{formatting_codes.join ';'}m#{string}\e[0m"
end