lib/ruco/style_map.rb in ruco-0.0.50 vs lib/ruco/style_map.rb in ruco-0.0.51

- old
+ new

@@ -9,33 +9,55 @@ def add(style, line, columns) @lines[line] ||= [] @lines[line] << [style, columns] end + def prepend(style, line, columns) + @lines[line] ||= [] + @lines[line].unshift [style, columns] + end + def flatten @lines.map do |styles| next unless styles # start and one after end of every column-range changes styles - points_of_change = styles.map{|s,c| [c.first, c.last+1] }.flatten + points_of_change = styles.map{|s,c| [c.first, c.last+1] }.flatten.uniq flat = [] styles.each do |style, columns| points_of_change.each do |point| next unless columns.include?(point) - flat[point] ||= [] - flat[point].unshift style + flat[point] = style end end - max = styles.map{|s,c|c.last}.max - flat[max+1] = [] + max = styles.map{|_,columns| columns.last }.max + flat[max+1] = :normal flat end end + def left_pad!(offset) + @lines.compact.each do |styles| + next unless styles + styles.map! do |style, columns| + [style, (columns.first + offset)..(columns.last + offset)] + end + end + end + + def invert! + map = {:reverse => :normal, :normal => :reverse} + @lines.compact.each do |styles| + styles.map! do |style, columns| + [map[style] || style, columns] + end + end + end + def +(other) lines = self.lines + other.lines new = StyleMap.new(0) new.lines = lines new @@ -53,22 +75,17 @@ end def pop slice!(-1, 1) end - - STYLES = { - :normal => 0, - :reverse => Curses::A_REVERSE - } def self.styled(content, styles) styles ||= [] content = content.dup build = [] - build << [[]] + build << [:normal] buffered = '' styles.each do |style| if style build[-1] << buffered @@ -81,10 +98,22 @@ end build[-1] << buffered + content build end - def self.curses_style(styles) - styles.sum{|style| STYLES[style] or raise("Unknown style #{style}") } + STYLES = { + :normal => 0, + :reverse => Curses::A_REVERSE + } + + def self.curses_style(style) + return 0 unless style + STYLES[style] or raise("Unknown style #{style.inspect}") end + + def self.single_line_reversed(columns) + map = StyleMap.new(1) + map.add(:reverse, 0, 0...columns) + map + end end -end \ No newline at end of file +end