js/{lex,matrix}.js: encode to mIRC more efficiently, pt. II.

This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-10-25 00:54:01 +02:00
parent e4ed3c9b9e
commit f9ee0ee4a8
2 changed files with 15 additions and 15 deletions

View File

@ -47,22 +47,21 @@ Lex.prototype.sanitize = function(){
default: return this.char default: return this.char
} }
} }
var lastBg = -1, lastFg = -1 Lex.prototype.mirc = function(bg_, fg_){
Lex.prototype.mirc = function(){
var char = this.char || " " var char = this.char || " "
var charIsNaN = isNaN(parseInt(char)) var charIsNaN = isNaN(parseInt(char))
if ((lastBg == this.fg) && (lastFg == this.bg)) { if ((bg_ == this.fg) && (fg_ == this.bg)) {
lastBg = this.bg; lastFg = this.fg bg_ = this.bg; fg_ = this.fg
return "\x16" + char return [bg_, fg_, "\x16" + char]
} else if ((lastBg != this.bg) && (lastFg == this.fg)) { } else if ((bg_ != this.bg) && (fg_ == this.fg)) {
lastBg = this.bg bg_ = this.bg
return "\x03," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char return [bg_, fg_, "\x03," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char]
} else if ((lastBg == this.bg) && (lastFg != this.fg)) { } else if ((bg_ == this.bg) && (fg_ != this.fg)) {
lastFg = this.fg fg_ = this.fg
return "\x03" + ((this.fg&15) < 10 && !charIsNaN ? "0" : "") + (this.fg&15) + char return [bg_, fg_, "\x03" + ((this.fg&15) < 10 && !charIsNaN ? "0" : "") + (this.fg&15) + char]
} else { } else {
lastBg = this.bg; lastFg = this.fg bg_ = this.bg; fg_ = this.fg
return "\x03" + (this.fg&15) + "," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char return [bg_, fg_, "\x03" + (this.fg&15) + "," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char]
} }
} }
Lex.prototype.ansi = function(){ Lex.prototype.ansi = function(){

View File

@ -272,12 +272,13 @@ Matrix.prototype.mirc = function (opts) {
var lines = this.aa.map(function(row, y){ var lines = this.aa.map(function(row, y){
var last, line = "" var last, line = ""
row.forEach(function(lex, x) { row.forEach(function(lex, x) {
var bg_ = -1, fg_ = 15
if (lex.eqColor(last)) { if (lex.eqColor(last)) {
line += lex.sanitize() line += lex.sanitize()
} }
else { else {
line += lex.mirc() [bg_, fg_, line_] = lex.mirc(bg_, fg_)
last = lex line += line_; last = lex;
} }
}) })
if (opts && opts.cutoff && line.length > opts.cutoff) { if (opts && opts.cutoff && line.length > opts.cutoff) {