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) -> @hash: (brush) ->
"#{brush.fg}_#{brush.bg}_#{brush.bright}_#{brush.underline}_#{brush.italic}_#{brush.blink}" "#{brush.fg}_#{brush.bg}_#{brush.bright}_#{brush.underline}_#{brush.italic}_#{brush.blink}"
@create: (options) -> @create: (options = {}) ->
options ||= {}
key = @hash(options) key = @hash(options)
brush = @cache[key] brush = @cache[key]
@ -19,10 +17,16 @@ class AsciiIo.Brush
brush brush
@normal: ->
@_normal ||= @create()
constructor: (options) -> constructor: (options) ->
@fg = options.fg @fg = options.fg
@bg = options.bg @bg = options.bg
@blink = options.blink
@bright = options.bright @bright = options.bright
@underline = options.underline
@italic = options.italic @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 @$el.find(".line:eq(" + n + ")").html html
spanFromBrush: (brush, hasCursor) -> spanFromBrush: (brush, hasCursor) ->
key = "#{AsciiIo.Brush.hash(brush)}_#{hasCursor}" key = "#{brush.hash()}_#{hasCursor}"
span = @cachedSpans[key] span = @cachedSpans[key]
if not span 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 != AsciiIo.Brush.normal()
span = "<span class=\"" span = "<span class=\""
if brush.fg isnt undefined if brush.fg isnt undefined

@ -1,6 +1,7 @@
class AsciiIo.VT class AsciiIo.VT
constructor: (@cols, @lines, @view) -> constructor: (@cols, @lines, @view) ->
@sgrInterpreter = new AsciiIo.SgrInterpreter()
@data = '' @data = ''
@resetTerminal() @resetTerminal()
@render() @render()
@ -256,57 +257,7 @@ class AsciiIo.VT
throw "no handler for CSI term: " + term throw "no handler for CSI term: " + term
handleSGR: (numbers) -> handleSGR: (numbers) ->
numbers = [0] if numbers.length is 0 @buffer.setBrush @sgrInterpreter.buildBrush(@buffer.brush, numbers)
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)
bell: -> bell: ->
@view.visualBell() @view.visualBell()

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