Cache generated brush spans

openid
Marcin Kulik 13 years ago
parent df1c1f5a4a
commit ee7a051172

@ -4,10 +4,13 @@ class AsciiIo.Brush
@clearCache: -> @clearCache: ->
@cache = {} @cache = {}
@hash: (brush) ->
"#{brush.fg}_#{brush.bg}_#{brush.bright}_#{brush.underline}"
@create: (options) -> @create: (options) ->
options ||= {} options ||= {}
key = "#{options.fg}_#{options.bg}_#{options.bright}_#{options.underline}" key = @hash(options)
brush = @cache[key] brush = @cache[key]
if not brush if not brush

@ -5,6 +5,7 @@ class AsciiIo.TerminalView extends Backbone.View
initialize: (options) -> initialize: (options) ->
@cols = options.cols @cols = options.cols
@lines = options.lines @lines = options.lines
@cachedSpans = {}
@createChildElements() @createChildElements()
@showCursor true @showCursor true
@ -49,7 +50,7 @@ class AsciiIo.TerminalView extends Backbone.View
if prevBrush if prevBrush
html += '</span>' html += '</span>'
html += @createSpan(brush, i is cursorX) html += @spanFromBrush(brush, i is cursorX)
prevBrush = brush prevBrush = brush
@ -62,7 +63,11 @@ class AsciiIo.TerminalView extends Backbone.View
@$el.find(".line:eq(" + n + ")").html html #.join("") @$el.find(".line:eq(" + n + ")").html html #.join("")
createSpan: (brush, hasCursor) -> spanFromBrush: (brush, hasCursor) ->
key = "#{AsciiIo.Brush.hash(brush)}_#{hasCursor}"
span = @cachedSpans[key]
if not span
span = "" span = ""
if hasCursor or brush.fg isnt undefined or brush.bg isnt undefined or brush.bright or brush.underline if hasCursor or brush.fg isnt undefined or brush.bg isnt undefined or brush.bright or brush.underline
@ -81,6 +86,8 @@ class AsciiIo.TerminalView extends Backbone.View
span += " cursor visible" if hasCursor span += " cursor visible" if hasCursor
span += "\">" span += "\">"
@cachedSpans[key] = span
span span
showCursor: (show) -> showCursor: (show) ->

Loading…
Cancel
Save