module SimpleColor::Utilities

Public Instance Methods

[](*args) click to toggle source
# File lib/simplecolor.rb, line 145
def [](*args)
        color(*args)
end
attributes_from_colors(s) click to toggle source

scan s from left to right and for each ansi sequences found split it into elementary components and output the symbolic meaning eg: SimpleColor.attributes_from_colors(SimpleColor.color(“foo”, :red))

=> [:red, :reset]
# File lib/simplecolor.rb, line 75
def attributes_from_colors(s)
        s.scan(Colorer.regexp(:ansi)).flat_map do |a|
                next :reset if a=="\e[m" #alternative for reset
                stack=[]
                # a[/\e\[(.*)m/,1].split(';').map {|c| Colorer.colors.key(c.to_i)}
                codes=a[/\e\[(.*)m/,1].split(';')
                i=0; while i<codes.length do
                        c=codes[i]
                        if c=="38" or c=="48"
                                if codes[i+1]=="5" #256 colors
                                        v=codes[i+2]
                                        stack << RGB.new(v.to_i, mode: 256, background: c=="48")
                                        i+=3
                                elsif codes[i+1]=="2" #true colors
                                        r=codes[i+2].to_i; g=codes[i+3].to_i; b=codes[i+4].to_i
                                        stack << RGB.new([r,g,b], background: c=="48")
                                        i+=5
                                end
                        else
                                stack << Colorer.colors.key(c.to_i)
                                i+=1
                        end
                end
                stack
        end
end
clear() click to toggle source
# File lib/simplecolor.rb, line 141
def clear
        color(:clear)
end
color_entities(l, color_regexp: Colorer.regexp(:color)) click to toggle source

split the line into characters and ANSII color sequences

# File lib/simplecolor.rb, line 117
def color_entities(l, color_regexp: Colorer.regexp(:color))
        l.split(/(#{color_regexp})/).flat_map {|c| color?(c) ? [c] : c.split('') }
end
color_strings(l, color_regexp: Colorer.regexp(:color)) click to toggle source

same as above but split into strings

# File lib/simplecolor.rb, line 121
def color_strings(l, color_regexp: Colorer.regexp(:color))
        u=l.split(/(#{color_regexp})/)
        # if we start with an ANSI sequence, u is ["", ...], so we need to
        # get rid of that ""
        u.shift if u.first == ""
        u
end
copy_colors(s,t) click to toggle source

copy the colors from s to t

# File lib/simplecolor.rb, line 111
def copy_colors(s,t)
        b,e=current_colors(s)
        b+t+e
end
current_colors(s) click to toggle source

get the ansi sequences on s (assume the whole line is colored) returns left_ansi, right_ansi, string

# File lib/simplecolor.rb, line 104
def current_colors(s)
        m=s.match(/^(#{Colorer.regexp(:match)})(.*?)(#{Colorer.regexp(:match)})$/)
        [m[1],m[3],m[2]]
rescue ArgumentError #rescue from "invalid byte sequence in UTF-8"
        ["","",s]
end
fill(s, columns:ENV['COLUMNS']&.to_i || 80) click to toggle source
# File lib/simplecolor.rb, line 129
def fill(s, columns:ENV['COLUMNS']&.to_i || 80)
        r=s.each_line.map do |l|
                l=l.chomp
                length=uncolor(l).length
                to_fill=columns - (length % columns)
                to_fill = 0 if to_fill == columns
                l+" "*to_fill
        end.join("\n")
        r+="\n" if s.end_with?("\n")
        r
end