Class: Warg::Console::SelectGraphicRendition

Inherits:
Object
  • Object
show all
Defined in:
lib/warg.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.



504
505
506
507
508
# File 'lib/warg.rb', line 504

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



510
511
512
# File 'lib/warg.rb', line 510

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

#modify(**attrs) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/warg.rb', line 518

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



544
545
546
547
548
549
550
# File 'lib/warg.rb', line 544

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

#to_sObject



540
541
542
# File 'lib/warg.rb', line 540

def to_s
  to_str
end

#to_strObject



536
537
538
# File 'lib/warg.rb', line 536

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

#wrap(text) ⇒ Object



514
515
516
# File 'lib/warg.rb', line 514

def wrap(text)
  call(text)
end

#|(other) ⇒ Object



532
533
534
# File 'lib/warg.rb', line 532

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