asciinema.org/app/assets/javascripts/player/brush.js.coffee

87 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2012-02-05 13:21:10 +00:00
class AsciiIo.Brush
2012-11-23 21:42:39 +00:00
@default_fg = 7
@default_bg = 0
2012-08-25 18:45:56 +00:00
@cache: {}
@clearCache: ->
@cache = {}
2012-08-25 17:52:43 +00:00
@default: ->
2012-08-25 18:45:56 +00:00
@_default ||= @create()
2012-02-05 13:21:10 +00:00
2012-02-12 21:48:04 +00:00
@hash: (brush) ->
2012-11-23 21:42:39 +00:00
"#{brush.fg}_#{brush.bg}_#{brush.blink}_#{brush.bright}_#{brush.italic}_#{brush.underline}_#{brush.reverse}"
2012-02-12 21:48:04 +00:00
2012-08-25 18:45:56 +00:00
@create: (options = {}) ->
key = @hash options
brush = @cache[key]
if not brush
brush = new AsciiIo.Brush(options)
@cache[key] = brush
brush
2012-08-25 17:52:43 +00:00
constructor: (options = {}) ->
2012-08-28 21:35:45 +00:00
@fg = undefined
@bg = undefined
@blink = false
@bright = false
@italic = false
@underline = false
2012-11-23 21:42:39 +00:00
@reverse = false
2012-08-28 21:35:45 +00:00
for name, value of options
this[name] = value
hash: ->
2012-08-25 17:52:43 +00:00
AsciiIo.Brush.hash this
attributes: ->
fg : @fg
bg : @bg
blink : @blink
bright : @bright
italic : @italic
underline: @underline
2012-11-23 21:42:39 +00:00
reverse : @reverse
fgColor: ->
2012-11-23 21:42:39 +00:00
if @reverse
@calculateBgColor()
else
@calculateFgColor()
bgColor: ->
if @reverse
@calculateFgColor()
else
@calculateBgColor()
calculateFgColor: ->
color = @fg
2012-11-23 21:42:39 +00:00
color = AsciiIo.Brush.default_fg if color is undefined
color += 8 if color < 8 and @bright
color
2012-11-23 21:42:39 +00:00
calculateBgColor: ->
color = @bg
2012-11-23 21:42:39 +00:00
color = AsciiIo.Brush.default_bg if color is undefined
color += 8 if color < 8 and @blink
color
2012-11-23 21:42:39 +00:00
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