Module: TermColor::Rule

Extended by:
Rule
Included in:
Rule
Defined in:
lib/term_color/rule.rb

Overview

Rule processor

}

Examples:

Basic Rule

# Foreground color set to blue
# Underline style turned on
# Not broken into `a:` and `z:`, so rules will
# be treated as `a:`, `z:` will be auto-generated
# to reset forground color and disable underline
rule = { fg: :blue, enable: :underline }

Full after reset

# Insize: (`a:`) Foreground yellow, bg red, dark style on
# After: Resets all color and style options to default,
# including those set by other rules
rule = {
    a: {
        fg: :yellow, bg: :red, enable: :dark
    },
    z: {
        reset: :all
    }

Italic red, only clearing color at end

# Inside: red fg, italic style
# After: color will reset to default, italic will remain on
rule = { a: { fg: :red, enable: :italic }, z: { reset: :fg }}

Author:

Defined Under Namespace

Classes: Compiled

Constant Summary collapse

Colors =

Named Standard ANSI Color constants (Basic named values for ‘fg` and `bg` rule option attributes)

{
    black: 0,
    red: 1,
    green: 2,
    yellow: 3,
    blue: 4,
    magenta: 5,
    cyan: 6,
    white: 7,
    bright_black: 60,
    bright_red: 61,
    bright_green: 62,
    bright_yellow: 63,
    bright_blue: 64,
    bright_magenta: 65,
    bright_cyan: 66,
    bright_white: 67
}.freeze
ColorTargets =

Numerical modifiers used with Color Values to target foreground or background.

  • For Named Standard Colors, value is added to given color’s numerical value

  • For XTerm 256/16m color codes, value is added to mode base

Examples:

Named Standard Color Background

{ bg: :red } #=> 40 + 1 = 41

XTerm 256 Foreground

{ fg: [208] } #=> 8 + 30 = 38
{
    fg: 30, # Foreground target
    bg: 40  # Background target
}.freeze
Styles =

Style option constants (Values that can be included in style ‘enable` and `disable` rule option attributes)

{
    bold: 1,
    ##
    # Alias for bold
    intense: 1,
    dim: 2,
    ##
    # Alias for dim
    dark: 2,
    italic: 3,
    underline: 4,
    inverse: 7,
    hidden: 8,
    strikethrough: 9
}.freeze
StyleActions =

Style action codes (Numerical modifiers applied to Style Codes to enable/disable them based on which option attribute action was used)

Examples:

Disable italic

(:disable) + (:italic) #=> 20 + 3 = 23
{
    # Enable style(s) action
    enable: 0,
    # Disable style(s) action
    disable: 20
}.freeze
Resets =

Reset option constants (Values for ‘reset` rule option attribute)

{
    # Reset everything
    all: 0,
    # Reset foreground color only
    fg: 39,
    # Reset background color only
    bg: 49,
    # Reset style
    style: StyleActions[:disable]
}.freeze
ResetOps =

Operations associated with reset

[ :reset, :keep ].freeze
ResetsExtra =
[ :style ].freeze
Parts =

Descriptive aliases for part names

{
    # Style applied on rule open
    inside: :inside,
    # Style appled when rule close is given
    after: :after
}.freeze
Ops =

Valid rule operations mapped to accepted const values (colors [fg, bg] can also accept integers)

{
    # Foreground color option
    fg: Colors.keys,
    # Background color option
    bg: Colors.keys,
    # Enable style(s) action
    enable: Styles.keys,
    # Disable style(s) action
    disable: Styles.keys,
    # Reset action
    reset: Resets.keys,
    # Keep action (opposite of reset)
    keep: Resets.keys
}.freeze
OpNormalize =

Normalize rules for ops; either ‘:keep` to not change original value, or `:array` to wrap single value inside array

{
  fg: :keep,
  bg: :keep,
  enable: :array,
  disable: :array,
  reset: :array,
  keep: :array
}.freeze
PartOps =

Allowed ops by part

{
  inside: [:fg, :bg, :enable, :disable],
  after: [:fg, :bg, :enable, :disable, :reset, :keep]
}.freeze
AfterOps =

Operations allowed within ‘after’

(Ops.filter{|k,v| PartOps[:after].include?(k)}).freeze
InsideOps =

Operations allowed within ‘inside’

(Ops.filter {|k,v| PartOps[:inside].include?(k)}).freeze
XTERM_COLOR_TARGET =

Value added to ColorTarget when using XTerm colors

8
XTERM_COLOR_256 =

Mode constant for XTerm 256 Colors

5
XTERM_COLOR_16M =

Mode constant for XTerm 16m Colors

2

Instance Method Summary collapse

Instance Method Details

#compile(rule, rs, is_reset = false) ⇒ Compiled

Compile rule into frozen instance of ‘Compiled` struct

Parameters:

  • rule (Hash)

    Rule hash

  • rs (RuleSet)

    Rule set

  • is_reset (Boolean) (defaults to: false)

    Set to true to indicate rule is for reset operation, and should ignore default after resolution

Returns:

  • (Compiled)

    Frozen instance of ‘Compiled` struct containing compiled rule



217
218
219
220
221
222
223
224
# File 'lib/term_color/rule.rb', line 217

def compile(rule, rs, is_reset=false)
    evaluated = evaluate(rule,rs,is_reset)
    return Compiled.new(
        rule,
        evaluated,
        codes(evaluated)
    ).freeze
end