Permalink
Browse files

Optimize escape codes; skip them for consecutive colours.

  • Loading branch information...
1 parent c4282bf commit 2a05348575df34584b54eb46be178be4d4fff726 @pda committed Jan 15, 2012
Showing with 12 additions and 1 deletion.
  1. +12 −1 asc.rb
View
13 asc.rb
@@ -63,9 +63,16 @@ def draw renderable
def render start_time
print "\e[H"
buffer = ''
+ previous_pixel = nil
(0...height).each do |y|
(OFFSET...(width + OFFSET)).each do |x|
- buffer << @fb.get(x, y).to_s
+ pixel = @fb.get(x, y)
+ if Pixel === previous_pixel && Pixel === pixel && pixel.color_equal?(previous_pixel)
+ buffer << pixel.char
+ else
+ buffer << pixel.to_s
+ end
+ previous_pixel = pixel
end
buffer << "\n"
end
@@ -90,11 +97,15 @@ def initialize char = " ", fg = nil, bg = nil
@char = char
@fg, @bg = fg, bg
end
+ attr_reader :char
def fg; @fg || 255 end
def bg; @bg || 0 end
def to_s
"\033[48;5;%dm\033[38;5;%dm%s" % [ bg, fg, @char ]
end
+ def color_equal? other
+ fg == other.fg && bg == other.bg
+ end
end
class Background

0 comments on commit 2a05348

Please sign in to comment.