Class: Sai::Decorator
- Inherits:
-
Object
- Object
- Sai::Decorator
- Includes:
- ColorManipulations, Gradients, HexColors, NamedColors, NamedStyles, RGBColors
- Defined in:
- lib/sai/decorator.rb,
lib/sai/decorator/delegator.rb,
lib/sai/decorator/gradients.rb,
lib/sai/decorator/hex_colors.rb,
lib/sai/decorator/rgb_colors.rb,
lib/sai/decorator/named_colors.rb,
lib/sai/decorator/named_styles.rb,
lib/sai/decorator/color_manipulations.rb
Overview
For each named color, two methods are dynamically generated:
- color_name - Applies the color to the foreground
- on_color_name - Applies the color to the backgroundAll color methods return Decorator
A decorator for applying ANSI styles and colors to text
Defined Under Namespace
Modules: ColorManipulations, Delegator, Gradients, HexColors, NamedColors, NamedStyles, RGBColors
Instance Method Summary collapse
-
#blink ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "blink" to the text.
-
#bold ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "bold" to the text.
-
#conceal ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "conceal" to the text.
-
#darken_background(amount) ⇒ Decorator
(also: #darken_bg)
included
from ColorManipulations
Darken the background color by a percentage.
-
#darken_text(amount) ⇒ Decorator
(also: #darken_fg, #darken_foreground)
included
from ColorManipulations
Darken the text color by a percentage.
-
#decorate(text) ⇒ ANSI::SequencedString
(also: #apply, #call, #encode)
Apply the styles and colors to the text.
-
#dim ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "dim" to the text.
-
#gradient(start_color, end_color, steps) ⇒ Decorator
included
from Gradients
Build a foreground gradient between two colors for text decoration.
-
#hex(code) ⇒ Decorator
included
from HexColors
Apply a hexadecimal color to the foreground.
-
#initialize(mode: Sai.mode.auto) ⇒ Decorator
constructor
private
Initialize a new instance of Decorator.
-
#italic ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "italic" to the text.
-
#lighten_background(amount) ⇒ Decorator
(also: #lighten_bg)
included
from ColorManipulations
Lighten the background color by a percentage.
-
#lighten_text(amount) ⇒ Decorator
(also: #lighten_fg, #lighten_foreground)
included
from ColorManipulations
Lighten the text color by a percentage.
-
#no_blink ⇒ Decorator
included
from NamedStyles
Remove the ANSI style "blink" from the text.
-
#no_conceal ⇒ Decorator
included
from NamedStyles
Remove the ANSI style "conceal" from the text.
-
#no_italic ⇒ Decorator
included
from NamedStyles
Remove the ANSI style "italic" from the text.
-
#no_reverse ⇒ Decorator
included
from NamedStyles
Remove the ANSI style "reverse" from the text.
-
#no_strike ⇒ Decorator
included
from NamedStyles
Remove the ANSI style "strike" from the text.
-
#no_underline ⇒ Decorator
included
from NamedStyles
Remove the ANSI style "underline" from the text.
-
#normal_intensity ⇒ Decorator
included
from NamedStyles
Remove any intensity styles (bold or dim) from the text.
-
#on_gradient(start_color, end_color, steps) ⇒ Decorator
included
from Gradients
Build a background gradient between two colors for text decoration.
-
#on_hex(code) ⇒ Decorator
included
from HexColors
Apply a hexadecimal color to the background.
-
#on_rainbow(steps) ⇒ Decorator
included
from Gradients
Build a background rainbow gradient for text decoration.
-
#on_rgb(red, green, blue) ⇒ Decorator
included
from RGBColors
Apply an RGB color to the background.
-
#rainbow(steps) ⇒ Decorator
included
from Gradients
Build a foreground rainbow gradient for text decoration.
-
#rapid_blink ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "rapid_blink" to the text.
-
#reverse ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "reverse" to the text.
-
#rgb(red, green, blue) ⇒ Decorator
included
from RGBColors
Apply an RGB color to the foreground.
-
#strike ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "strike" to the text.
-
#underline ⇒ Decorator
included
from NamedStyles
Apply the ANSI style "underline" to the text.
-
#with_mode(mode) ⇒ Decorator
Apply a specific color mode to the decorator.
Constructor Details
#initialize(mode: Sai.mode.auto) ⇒ Decorator
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.
Initialize a new instance of Decorator
49 50 51 52 53 54 55 56 |
# File 'lib/sai/decorator.rb', line 49 def initialize(mode: Sai.mode.auto) @background = nil @background_sequence = nil @mode = mode @foreground = nil @foreground_sequence = nil @styles = [] #: Array[Symbol] end |
Instance Method Details
#blink ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "blink" to the text
#bold ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "bold" to the text
#conceal ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "conceal" to the text
#darken_background(amount) ⇒ Decorator Also known as: darken_bg Originally defined in module ColorManipulations
Darken the background color by a percentage
#darken_text(amount) ⇒ Decorator Also known as: darken_fg, darken_foreground Originally defined in module ColorManipulations
Darken the text color by a percentage
#decorate(text) ⇒ ANSI::SequencedString Also known as: apply, call, encode
Apply the styles and colors to the text
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sai/decorator.rb', line 72 def decorate(text) return ANSI::SequencedString.new(text) unless should_decorate? return apply_sequence_gradient(text) if @foreground_sequence || @background_sequence sequences = [ @foreground && Conversion::ColorSequence.resolve(@foreground, @mode), @background && Conversion::ColorSequence.resolve(@background, @mode, :background), style_sequences.join ].compact.join ANSI::SequencedString.new("#{sequences}#{text}#{ANSI::RESET}") end |
#dim ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "dim" to the text
#gradient(start_color, end_color, steps) ⇒ Decorator Originally defined in module Gradients
Build a foreground gradient between two colors for text decoration
#hex(code) ⇒ Decorator Originally defined in module HexColors
Apply a hexadecimal color to the foreground
#italic ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "italic" to the text
#lighten_background(amount) ⇒ Decorator Also known as: lighten_bg Originally defined in module ColorManipulations
Lighten the background color by a percentage
#lighten_text(amount) ⇒ Decorator Also known as: lighten_fg, lighten_foreground Originally defined in module ColorManipulations
Lighten the text color by a percentage
#no_blink ⇒ Decorator Originally defined in module NamedStyles
Remove the ANSI style "blink" from the text
#no_conceal ⇒ Decorator Originally defined in module NamedStyles
Remove the ANSI style "conceal" from the text
#no_italic ⇒ Decorator Originally defined in module NamedStyles
Remove the ANSI style "italic" from the text
#no_reverse ⇒ Decorator Originally defined in module NamedStyles
Remove the ANSI style "reverse" from the text
#no_strike ⇒ Decorator Originally defined in module NamedStyles
Remove the ANSI style "strike" from the text
#no_underline ⇒ Decorator Originally defined in module NamedStyles
Remove the ANSI style "underline" from the text
#normal_intensity ⇒ Decorator Originally defined in module NamedStyles
Remove any intensity styles (bold or dim) from the text
#on_gradient(start_color, end_color, steps) ⇒ Decorator Originally defined in module Gradients
Build a background gradient between two colors for text decoration
#on_hex(code) ⇒ Decorator Originally defined in module HexColors
Apply a hexadecimal color to the background
#on_rainbow(steps) ⇒ Decorator Originally defined in module Gradients
Build a background rainbow gradient for text decoration
#on_rgb(red, green, blue) ⇒ Decorator Originally defined in module RGBColors
Apply an RGB color to the background
#rainbow(steps) ⇒ Decorator Originally defined in module Gradients
Build a foreground rainbow gradient for text decoration
#rapid_blink ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "rapid_blink" to the text
#reverse ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "reverse" to the text
#rgb(red, green, blue) ⇒ Decorator Originally defined in module RGBColors
Apply an RGB color to the foreground
#strike ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "strike" to the text
#underline ⇒ Decorator Originally defined in module NamedStyles
Apply the ANSI style "underline" to the text
#with_mode(mode) ⇒ Decorator
Apply a specific color mode to the decorator
102 103 104 |
# File 'lib/sai/decorator.rb', line 102 def with_mode(mode) dup.tap { |duped| duped.instance_variable_set(:@mode, mode) } end |