class Bash_Visual::Font

Constants

COLORS
RESET
TYPES

Attributes

background[R]
foreground[R]
types[R]

Public Class Methods

new(types = :std, foreground = :white, background = nil) click to toggle source
# File lib/bash-visual/font.rb, line 39
def initialize(types = :std, foreground = :white, background = nil)

  build(types, foreground, background)
end
rand_color(exclude_color = nil) click to toggle source

@param [Symbol] exclude_color

# File lib/bash-visual/font.rb, line 78
def rand_color (exclude_color = nil)
  color = rand(16)
  color += 2 if color > 7

  if color == @@colors_map[exclude_color]
    color += 1
    color = 0 if color > 17
  end

  @@colors_map.each do |name, code|
    return name if code == color
  end

end
rand_font() click to toggle source
# File lib/bash-visual/font.rb, line 93
def rand_font
   color = self.rand_color
   Font.new :std, color
end

Public Instance Methods

add_type(type) click to toggle source
# File lib/bash-visual/font.rb, line 44
def add_type(type)
  return false if @types.include? type

  @types << type
  build(@types, @foreground, @background)

  true
end
inspect() click to toggle source
# File lib/bash-visual/font.rb, line 64
def inspect
  "<Font types=%s, foreground=%s, background=%s>" %
      [@types, @foreground, (@background ? @background : 'nil')]
end
remove_type(type) click to toggle source
# File lib/bash-visual/font.rb, line 53
def remove_type(type)

  return false unless @types.include? type
  return false if @types == [:std]

  @types.delete type
  build(@types, @foreground, @background)

  true
end
to_bash()
Alias for: to_s
to_s() click to toggle source
# File lib/bash-visual/font.rb, line 69
def to_s
  @bash_command
end
Also aliased as: to_bash

Private Instance Methods

build(types, foreground, background) click to toggle source
# File lib/bash-visual/font.rb, line 101
def build(types, foreground, background)

  unless COLORS.include?(foreground)
    raise "Illegal color #{foreground}"
  end

  @foreground, @background = foreground, background

  @types = prepare_types types

  sequence = []
  @types.each do |type|
    sequence << @@types_map[type]
  end

  color_index = @@colors_map[@foreground]

  sequence << if color_index < 10
                "3#{color_index}"
              else
                "9#{color_index - 10}"
              end

  if @background
    color_index = @@colors_map[@background]
    sequence << if color_index < 10
                  "4#{color_index}"
                else
                  "10#{color_index - 10}"
                end
  end

  @bash_command = "\e[#{sequence.join(';')}m"
end
prepare_types(types) click to toggle source

@return [Array]

# File lib/bash-visual/font.rb, line 138
def prepare_types(types)
  types = case types
            when Symbol then
              [types]
            when Array then
              if types.size > 2
                types.delete_if { |type| type == :std }
              else
                types
              end
            else
              raise "types must be Array or Symbol"
          end

  if types.size > 1 && types.include?(:std)
    types.delete :std
  end

  types
end