Extract SGR codes interpretation to separate class

openid
Marcin Kulik 13 years ago
parent 3371a238b7
commit 6eeafdf699

@ -7,9 +7,7 @@ class AsciiIo.Brush
@hash: (brush) ->
"#{brush.fg}_#{brush.bg}_#{brush.bright}_#{brush.underline}_#{brush.italic}_#{brush.blink}"
@create: (options) ->
options ||= {}
@create: (options = {}) ->
key = @hash(options)
brush = @cache[key]
@ -19,10 +17,16 @@ class AsciiIo.Brush
brush
@normal: ->
@_normal ||= @create()
constructor: (options) ->
@fg = options.fg
@bg = options.bg
@blink = options.blink
@bright = options.bright
@underline = options.underline
@italic = options.italic
@blink = options.blink
@underline = options.underline
hash: ->
AsciiIo.Brush.hash(this)

@ -0,0 +1,60 @@
class AsciiIo.SgrInterpreter
reset: ->
@attrs =
fg : undefined
bg : undefined
blink : false
bright : false
italic : false
underline: false
buildBrush: (oldBrush, numbers) ->
@attrs =
fg : oldBrush.fg
bg : oldBrush.bg
blink : oldBrush.blink
bright : oldBrush.bright
italic : oldBrush.italic
underline: oldBrush.underline
numbers = [0] if numbers.length is 0
i = 0
while i < numbers.length
n = numbers[i]
if n is 0
@reset()
else if n is 1
@attrs.bright = true
else if n is 3
@attrs.italic = true
else if n is 4
@attrs.underline = true
else if n is 5
@attrs.blink = true
else if n is 23
@attrs.italic = false
else if n is 24
@attrs.underline = false
else if n is 25
@attrs.blink = false
else if n >= 30 and n <= 37
@attrs.fg = n - 30
else if n is 38
@attrs.fg = numbers[i + 2]
i += 2
else if n is 39
@attrs.fg = undefined
else if n >= 40 and n <= 47
@attrs.bg = n - 40
else if n is 48
@attrs.bg = numbers[i + 2]
i += 2
else if n is 49
@attrs.bg = undefined
i++
AsciiIo.Brush.create @attrs

@ -71,13 +71,13 @@ class AsciiIo.TerminalView extends Backbone.View
@$el.find(".line:eq(" + n + ")").html html
spanFromBrush: (brush, hasCursor) ->
key = "#{AsciiIo.Brush.hash(brush)}_#{hasCursor}"
key = "#{brush.hash()}_#{hasCursor}"
span = @cachedSpans[key]
if not span
span = ""
if hasCursor or brush.fg isnt undefined or brush.bg isnt undefined or brush.bright or brush.underline
if hasCursor or brush != AsciiIo.Brush.normal()
span = "<span class=\""
if brush.fg isnt undefined

@ -1,6 +1,7 @@
class AsciiIo.VT
constructor: (@cols, @lines, @view) ->
@sgrInterpreter = new AsciiIo.SgrInterpreter()
@data = ''
@resetTerminal()
@render()
@ -256,57 +257,7 @@ class AsciiIo.VT
throw "no handler for CSI term: " + term
handleSGR: (numbers) ->
numbers = [0] if numbers.length is 0
i = 0
while i < numbers.length
n = numbers[i]
if n is 0
@fg = @bg = undefined
@bright = false
@underline = false
# TODO: reset blink (and others?)
else if n is 1
@bright = true
else if n is 3
@italic = true
else if n is 4
@underline = true
else if n is 5
@blink = true
else if n is 23
@italic = false
else if n is 24
@underline = false
else if n is 25
@blink = false
else if n >= 30 and n <= 37
@fg = n - 30
else if n is 38
@fg = numbers[i + 2]
i += 2
else if n is 39
@fg = undefined
else if n >= 40 and n <= 47
@bg = n - 40
else if n is 48
@bg = numbers[i + 2]
i += 2
else if n is 49
@bg = undefined
i++
props = {}
props.fg = @fg if @fg isnt undefined
props.bg = @bg if @bg isnt undefined
props.bright = true if @bright
props.underline = true if @underline
props.italic = true if @italic
props.blink = true if @blink
@buffer.setBrush AsciiIo.Brush.create(props)
@buffer.setBrush @sgrInterpreter.buildBrush(@buffer.brush, numbers)
bell: ->
@view.visualBell()

@ -0,0 +1,6 @@
describe 'AsciiIo.SgrInterpreter', ->
describe '#reset', ->
describe '#buildBrush', ->
Loading…
Cancel
Save