"Reverse video" support

openid
Marcin Kulik 12 years ago
parent 454228bc28
commit 70b1f27234

@ -1,4 +1,7 @@
class AsciiIo.Brush
@default_fg = 7
@default_bg = 0
@cache: {}
@clearCache: ->
@ -8,7 +11,7 @@ class AsciiIo.Brush
@_default ||= @create()
@hash: (brush) ->
"#{brush.fg}_#{brush.bg}_#{brush.blink}_#{brush.bright}_#{brush.italic}_#{brush.underline}"
"#{brush.fg}_#{brush.bg}_#{brush.blink}_#{brush.bright}_#{brush.italic}_#{brush.underline}_#{brush.reverse}"
@create: (options = {}) ->
key = @hash options
@ -27,6 +30,7 @@ class AsciiIo.Brush
@bright = false
@italic = false
@underline = false
@reverse = false
for name, value of options
this[name] = value
@ -41,15 +45,42 @@ class AsciiIo.Brush
bright : @bright
italic : @italic
underline: @underline
reverse : @reverse
fgColor: ->
if @reverse
@calculateBgColor()
else
@calculateFgColor()
bgColor: ->
if @reverse
@calculateFgColor()
else
@calculateBgColor()
calculateFgColor: ->
color = @fg
color = 7 if color is undefined
color = AsciiIo.Brush.default_fg if color is undefined
color += 8 if color < 8 and @bright
color
bgColor: ->
calculateBgColor: ->
color = @bg
color = 0 if color is undefined
color = AsciiIo.Brush.default_bg if color is undefined
color += 8 if color < 8 and @blink
color
applyChanges: (changes) ->
attrs = @attributes()
for attr, val of changes
attrs[attr] = val
AsciiIo.Brush.create attrs
hasDefaultFg: ->
@fgColor() == AsciiIo.Brush.default_fg
hasDefaultBg: ->
@bgColor() == AsciiIo.Brush.default_bg

@ -69,10 +69,10 @@ class AsciiIo.Renderer.Pre extends AsciiIo.Renderer.Base
if brush != AsciiIo.Brush.default()
span = "<span class=\""
if brush.fg isnt undefined
unless brush.hasDefaultFg()
span += " fg" + brush.fgColor()
if brush.bg isnt undefined
unless brush.hasDefaultBg()
span += " bg" + brush.bgColor()
if brush.bright

@ -242,12 +242,7 @@ class AsciiIo.ScreenBuffer
repeatLastCharacter: (n = 1) ->
updateBrush: (changes) ->
attrs = @brush.attributes()
for attr, val of changes
attrs[attr] = val
@brush = AsciiIo.Brush.create attrs
@brush = @brush.applyChanges changes
# ----- Scroll control

@ -14,6 +14,7 @@ class AsciiIo.SgrInterpreter
changes.bright = false
changes.italic = false
changes.underline = false
changes.reverse = false
else if n is 1
changes.bright = true
else if n is 3
@ -22,12 +23,16 @@ class AsciiIo.SgrInterpreter
changes.underline = true
else if n is 5
changes.blink = true
else if n is 7
changes.reverse = true
else if n is 23
changes.italic = false
else if n is 24
changes.underline = false
else if n is 25
changes.blink = false
else if n is 27
changes.reverse = false
else if n >= 30 and n <= 37
changes.fg = n - 30
else if n is 38

@ -53,6 +53,10 @@ describe 'AsciiIo.Brush', ->
brush = new AsciiIo.Brush fg: 3, bright: true
expect(brush.fgColor()).toEqual 11
it 'returns bg if reverse is on', ->
brush = new AsciiIo.Brush fg: 1, bg: 2, reverse: true
expect(brush.fgColor()).toEqual 2
describe '#bgColor', ->
it 'returns 0 if bg is undefined', ->
@ -67,6 +71,10 @@ describe 'AsciiIo.Brush', ->
brush = new AsciiIo.Brush bg: 4, blink: true
expect(brush.bgColor()).toEqual 12
it 'returns fg if reverse is on', ->
brush = new AsciiIo.Brush fg: 1, bg: 2, reverse: true
expect(brush.bgColor()).toEqual 1
describe '#attributes', ->
it 'includes fg, bg, blink, bright, italic, underline', ->
@ -78,3 +86,17 @@ describe 'AsciiIo.Brush', ->
for attr in expectedAttrs
expect(_(attrs).has(attr)).toBeTruthy()
describe '#applyChanges', ->
it 'returns new brush', ->
brush = new AsciiIo.Brush
newBrush = brush.applyChanges fg: 5
expect(newBrush).toNotEqual brush
it 'applies changes to attributes', ->
brush = new AsciiIo.Brush fg: 1
newBrush = brush.applyChanges fg: 2
expect(newBrush.fg).toEqual 2

@ -22,6 +22,9 @@ describe 'AsciiIo.SgrInterpreter', ->
it 'sets blink attr for 5', ->
expectChange [5], blink: true
it 'sets reverse attr for 7', ->
expectChange [7], reverse: true
it 'unsets italic for 23', ->
expectChange [23], italic: false
@ -31,6 +34,9 @@ describe 'AsciiIo.SgrInterpreter', ->
it 'unsets blink attr for 25', ->
expectChange [25], blink: false
it 'unsets reverse attr for 27', ->
expectChange [27], reverse: false
it 'sets foreground for 30-37', ->
expectChange [30], fg: 0
expectChange [32], fg: 2

Loading…
Cancel
Save