lib/ansiterm/string.rb in ansiterm-0.3.3 vs lib/ansiterm/string.rb in ansiterm-0.4

- old
+ new

@@ -54,34 +54,38 @@ end def set_attr(range, attr) min = [range.first - 1,0].max fattr = @attrs[min] - attr = fattr.merge(attr) + #attr = fattr.merge(attr) r = Array(@attrs[range]).count # Inefficient, but saves dealing with negative offsets etc. "manually" last = nil - @attrs[range] = @attrs[range].map do |a| - n = a.merge(attr) + @attrs[range] = @attrs[range]&.map do |a| + n = a ? a.merge(attr) : attr last == n ? last : n - end - rm = range.last + end || [] + rm = range.last+1 if a = @attrs[rm] - @attrs[rm] = Attr.new(bgcol:40).merge(fattr).merge(a) + @attrs[rm] = Attr.new.merge(fattr).merge(a) + if !a.bgcol + @attrs[rm] = @attrs[rm].merge({bgcol: 49}) + end end end def merge_attr_below(range, attr) min = [0,range.first - 1].max fattr = @attrs[min] r = Array(@attrs[range]).count # Inefficient, but saves dealing with negative offsets etc. "manually" last = nil @attrs[range] = @attrs[range].map do |a| if a.bgcol == 49 - a.merge(attr) + n = attr.merge(a, ignore: :bgcol) else - attr.merge(a) + n = attr.merge(a) end + last == n ? last : n end #fattr #@attrs[min+r].merge(fattr) end def[]= range, str @@ -89,11 +93,11 @@ a = @attrs parse(str) @str = s[0..(range.min-1)].to_s + @str + s[(range.max)..-1].to_s @attrs = a[0..(range.min-1)].to_a + @attrs + a[(range.max)..-1].to_a end - + def[] i str = @str[i] if str a = self.class.new a.set(str,@attrs[i]) @@ -104,11 +108,11 @@ end def char_at(index) @str[index] end - + def attr_at(index) @attrs[index] end def << str @@ -131,24 +135,24 @@ if col == 38 || col == 48 par = params.shift if par == "5" col = [col,5,params.shift].join(";") elsif par == "2" - col = ([col,2] << params.slice!(0..2)).join(";") + col = [col,2,*params.slice!(0..2)].join(";") # ,params.shift,params.shift, params.shift].join(";") end end a.merge(attr_name => col) end - + def parse(str) @str = "" @attrs = [] a = AnsiTerm::Attr.new max = str.length i = 0 - + while i < max c = str[i] if c == "\e" && str[i+1] == "[" # CSI params = [] i += 2