Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/blackjack/strings/string.rb

Overview

Class that modifies the String class and equips it with a method to set terminal properties of a string

Instance Method Summary collapse

Instance Method Details

#set_attributes(attributes, previous_attributes = []) ⇒ string

Sets the terminal attributes of the string

Parameters:

  • attributes (int Array)

    the attributes of the string to be set

  • previous_attributes (int Array) (defaults to: [])

    the attributes to be set after this string. If this parameter is not set, the default terminal values are loaded

Returns:

  • (string)

    the string with the attributes set



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/blackjack/strings/string.rb', line 30

def set_attributes(attributes, previous_attributes = [])

    mode_start_string = ''
    modes_end_string = "\e[0m"
    attributes.each { |attribute|
        mode_start_string += "\e[#{attribute}m"
    }
    attributed_string = mode_start_string
    attributed_string += self.gsub("\n", "#{modes_end_string}\n#{mode_start_string}")
    attributed_string += modes_end_string

    previous_attributes.each { |attribute|
        attributed_string += "\e[#{attribute}m"
    }
    attributed_string
end