Bring back color support

openid
Marcin Kulik 12 years ago
parent 1185e3a4d9
commit b87649aec2

@ -1,35 +1,31 @@
class AsciiIo.Brush
@cache: {}
@defaultAttrs =
fg : undefined
bg : undefined
blink : false
bright : false
italic : false
underline: false
@clearCache: ->
@cache = {}
@default: ->
@_default ||= new AsciiIo.Brush()
@hash: (brush) ->
"#{brush.fg}_#{brush.bg}_#{brush.bright}_#{brush.underline}_#{brush.italic}_#{brush.blink}"
@create: (options = {}) ->
key = @hash(options)
brush = @cache[key]
if not brush
brush = new AsciiIo.Brush(options)
@cache[key] = brush
brush
@normal: ->
@_normal ||= @create()
constructor: (options) ->
@fg = options.fg
@bg = options.bg
@blink = options.blink
@bright = options.bright
@italic = options.italic
@underline = options.underline
constructor: (options = {}) ->
_(this).extend AsciiIo.Brush.defaultAttrs, options
hash: ->
AsciiIo.Brush.hash(this)
AsciiIo.Brush.hash this
attributes: ->
fg : @fg
bg : @bg
blink : @blink
bright : @bright
italic : @italic
underline: @underline
fgColor: ->
color = @fg

@ -58,7 +58,7 @@ class AsciiIo.Renderer.Base extends Backbone.View
renderSnapshot: (text) ->
i = 0
for line in text.split("\n")
fragments = [[line, AsciiIo.Brush.normal()]]
fragments = [[line, AsciiIo.Brush.default()]]
@renderLine i, fragments, undefined
i++

@ -58,7 +58,7 @@ class AsciiIo.Renderer.Pre extends AsciiIo.Renderer.Base
text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
spanFromBrush: (brush) ->
brush = AsciiIo.Brush.create brush
brush = new AsciiIo.Brush brush
key = brush.hash()
span = @cachedSpans[key]
@ -66,7 +66,7 @@ class AsciiIo.Renderer.Pre extends AsciiIo.Renderer.Base
if not span
span = ""
if brush != AsciiIo.Brush.normal()
if brush != AsciiIo.Brush.default()
span = "<span class=\""
if brush.fg isnt undefined

@ -265,7 +265,9 @@ class AsciiIo.AnsiInterpreter
numbers
handleSGR: (numbers) ->
# @buffer.setBrush @sgrInterpreter.buildBrush(@buffer.getBrush(), numbers)
numbers = [0] if numbers.length is 0
changes = @sgrInterpreter.parse numbers
@cb 'updateBrush', changes
formattedData: (data) ->
head = data.slice(0, 100)

@ -7,7 +7,7 @@ class AsciiIo.ScreenBuffer
@cursorX = 0
@cursorY = 0
@setBrush AsciiIo.Brush.normal()
@setBrush AsciiIo.Brush.default()
@setCharset 'us'
topMargin: ->
@ -35,7 +35,7 @@ class AsciiIo.ScreenBuffer
data = @lineData[n] || []
fragments = []
currentBrush = AsciiIo.Brush.normal()
currentBrush = AsciiIo.Brush.default()
currentText = ''
for i in [0...@cols]
@ -244,6 +244,10 @@ class AsciiIo.ScreenBuffer
repeatLastCharacter: (n = 1) ->
updateBrush: (changes) ->
attrs = _.extend({}, @brush.attributes(), changes)
@brush = new AsciiIo.Brush attrs
# ----- Scroll control
inScrollRegion: ->

@ -1,64 +1,47 @@
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
parse: (numbers) ->
changes = {}
i = 0
while i < numbers.length
n = numbers[i]
if n is 0
@reset()
_(changes).extend AsciiIo.Brush.defaultAttrs
else if n is 1
@attrs.bright = true
changes.bright = true
else if n is 3
@attrs.italic = true
changes.italic = true
else if n is 4
@attrs.underline = true
changes.underline = true
else if n is 5
@attrs.blink = true
changes.blink = true
else if n is 23
@attrs.italic = false
changes.italic = false
else if n is 24
@attrs.underline = false
changes.underline = false
else if n is 25
@attrs.blink = false
changes.blink = false
else if n >= 30 and n <= 37
@attrs.fg = n - 30
changes.fg = n - 30
else if n is 38
@attrs.fg = numbers[i + 2]
changes.fg = numbers[i + 2]
i += 2
else if n is 39
@attrs.fg = undefined
changes.fg = undefined
else if n >= 40 and n <= 47
@attrs.bg = n - 40
changes.bg = n - 40
else if n is 48
@attrs.bg = numbers[i + 2]
changes.bg = numbers[i + 2]
i += 2
else if n is 49
@attrs.bg = undefined
changes.bg = undefined
else if n >= 90 and n <= 97
@attrs.fg = n - 90
changes.fg = n - 90
else if n >= 100 and n <= 107
@attrs.bg = n - 100
changes.bg = n - 100
i++
AsciiIo.Brush.create @attrs
changes

@ -226,6 +226,9 @@ class AsciiIo.VT
clearAllHorizontalTabStops: ->
@buffer.clearAllHorizontalTabStops()
updateBrush: (attrs) ->
@buffer.updateBrush attrs
# References:
# http://en.wikipedia.org/wiki/ANSI_escape_code
# http://ttssh2.sourceforge.jp/manual/en/about/ctrlseq.html

Loading…
Cancel
Save