Class: BoPeep::Console::SelectGraphicRendition

Inherits:
Object
  • Object
show all
Defined in:
lib/bopeep.rb

Defined Under Namespace

Classes: Renderer

Constant Summary collapse

TEXT_COLORS =
{
  "red"     => "31",
  "green"   => "32",
  "yellow"  => "33",
  "blue"    => "34",
  "magenta" => "35",
  "cyan"    => "36",
  "white"   => "37",
}
BACKGROUND_COLORS =
TEXT_COLORS.map { |name, value| [name, (value.to_i + 10).to_s] }.to_h
EFFECTS =
{
  "bold"          => "1",
  "faint"         => "2",
  "italic"        => "3",
  "underline"     => "4",
  "blink_slow"    => "5",
  "blink_fast"    => "6",
  "invert_colors" => "7",
  "hide"          => "8",
  "strikethrough" => "9"
}
RESET =
new

Instance Method Summary collapse

Constructor Details

#initialize(text_color: "0", background_color: "0", effect: "0") ⇒ SelectGraphicRendition

Returns a new instance of SelectGraphicRendition.



518
519
520
521
522
# File 'lib/bopeep.rb', line 518

def initialize(text_color: "0", background_color: "0", effect: "0")
  @text_color = TEXT_COLORS.fetch(text_color.to_s, text_color)
  @background_color = BACKGROUND_COLORS.fetch(background_color.to_s, background_color)
  @effect = EFFECTS.fetch(effect.to_s, effect)
end

Instance Method Details

#call(text) ⇒ Object



524
525
526
# File 'lib/bopeep.rb', line 524

def call(text)
  "#{self}#{text}#{RESET}"
end

#modify(**attrs) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/bopeep.rb', line 532

def modify(**attrs)
  combination = to_h.merge(attrs) do |key, old_value, new_value|
    if old_value == "0"
      new_value
    elsif new_value == "0"
      old_value
    else
      new_value
    end
  end

  self.class.new(**combination)
end

#to_hObject



558
559
560
561
562
563
564
# File 'lib/bopeep.rb', line 558

def to_h
  {
    text_color: @text_color,
    background_color: @background_color,
    effect: @effect
  }
end

#to_sObject



554
555
556
# File 'lib/bopeep.rb', line 554

def to_s
  to_str
end

#to_strObject



550
551
552
# File 'lib/bopeep.rb', line 550

def to_str
  "\e[#{@background_color};#{@effect};#{@text_color}m"
end

#wrap(text) ⇒ Object



528
529
530
# File 'lib/bopeep.rb', line 528

def wrap(text)
  call(text)
end

#|(other) ⇒ Object



546
547
548
# File 'lib/bopeep.rb', line 546

def |(other)
  modify(**other.to_h)
end