Module: Hilighter::Codes

Included in:
String
Defined in:
lib/hilighter/codes.rb

Instance Method Summary collapse

Instance Method Details

#add_methodsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hilighter/codes.rb', line 3

def add_methods
    color_regex = /(^|\n)(\e\[([0-9;]+m|K))+(\n|$)/

    colors.each do |key, val|
        if (key.start_with?("on_"))
            on_default = colors["on_default"]
            define_method key do
                return self.plain if (Hilighter.disable?)

                return [
                    "\e[#{val}m",
                    self.plain_bg.gsub(
                        /\n/,
                        "\e[#{on_default}m\n\e[#{val}m"
                    ),
                    "\e[#{on_default}m"
                ].join.gsub(color_regex, "\\1\\4")
            end
        else
            default = colors["default"]
            define_method key do
                return self.plain if (Hilighter.disable?)

                return [
                    "\e[#{val}m",
                    self.plain_fg.gsub(
                        /\n/,
                        "\e[#{default}m\n\e[#{val}m"
                    ),
                    "\e[#{default}m"
                ].join.gsub(color_regex, "\\1\\4")
            end
        end
    end

    modes.each do |key, val|
        rm = [
            modes[key],
            modes["no_#{key}".gsub(/^no_no_/, "")]
        ].delete_if(&:nil?).uniq.join("|")

        off = "no_#{key}"
        if modes.has_key?(off)
            off = "\e[#{modes[off]}m"
        else
            off = ""
        end

        define_method key do
            return self.plain if (Hilighter.disable?)
            self.gsub!(/\e\[(#{rm})m/, "")
            return "\e[#{val}m#{self}#{off}".gsub(
                color_regex,
                "\\1\\4"
            )
        end
    end
end

#colorsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hilighter/codes.rb', line 62

def colors
    @valid_colors ||= {
        "black" => 30,
        "red" => 31,
        "green" => 32,
        "yellow" => 33,
        "blue" => 34,
        "magenta" => 35,
        "cyan" => 36,
        "white" => 37,
        "light_black" => 90,
        "light_red" => 91,
        "light_green" => 92,
        "light_yellow" => 93,
        "light_blue" => 94,
        "light_magenta" => 95,
        "light_cyan" => 96,
        "light_white" => 97,

        "on_black" => 40,
        "on_red" => 41,
        "on_green" => 42,
        "on_yellow" => 43,
        "on_blue" => 44,
        "on_magenta" => 45,
        "on_cyan" => 46,
        "on_white" => 47,
        "on_light_black" => 100,
        "on_light_red" => 101,
        "on_light_green" => 102,
        "on_light_yellow" => 103,
        "on_light_blue" => 104,
        "on_light_magenta" => 105,
        "on_light_cyan" => 106,
        "on_light_white" => 107,

        "default" => 39,
        "on_default" => 49
    }
    if (@valid_colors.length < 256)
        256.times.each do |i|
            clr = i.to_s.rjust(3, "0")
            @valid_colors["color_#{clr}"] = "38;5;#{clr}"
            @valid_colors["on_color_#{clr}"] = "48;5;#{clr}"
        end
    end
    return @valid_colors
end

#modesObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hilighter/codes.rb', line 111

def modes
    return {
        "reset" => 0,
        "normal" => 0,
        "bold" => 1,
        "dim" => 2,
        "faint" => 2,
        "italic" => 3,
        "underline" => 4,
        "blink" => 5,
        "blink_slow" => 5,
        "blink_rapid" => 6,
        "inverse" => 7,
        "negative" => 7,
        "swap" => 7,
        "hide" => 8,
        "conceal" => 8,
        "crossed_out" => 9,
        "strikethrough" => 9,
        "fraktur" => 20,

        "no_bold" => 21,
        "no_dim" => 22,
        "no_faint" => 22,
        "no_italic" => 23,
        "no_fraktur" => 23,
        "no_underline" => 24,
        "no_blink" => 25,
        "no_blink_slow" => 25,
        "no_blink_rapid" => 26,
        "no_inverse" => 27,
        "no_negative" => 27,
        "no_swap" => 27,
        "no_hide" => 28,
        "no_conceal" => 28,
        "no_crossed_out" => 29,
        "no_strikethrough" => 29
    }
end