From f9ee0ee4a8ec1df3401b4c7ca921110ca849011e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucio=20Andr=C3=A9s=20Illanes=20Albornoz?= Date: Thu, 25 Oct 2018 00:54:01 +0200 Subject: [PATCH] js/{lex,matrix}.js: encode to mIRC more efficiently, pt. II. --- js/lex.js | 25 ++++++++++++------------- js/matrix.js | 5 +++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/js/lex.js b/js/lex.js index aa1a889..9bfd128 100644 --- a/js/lex.js +++ b/js/lex.js @@ -47,22 +47,21 @@ Lex.prototype.sanitize = function(){ default: return this.char } } -var lastBg = -1, lastFg = -1 -Lex.prototype.mirc = function(){ +Lex.prototype.mirc = function(bg_, fg_){ var char = this.char || " " var charIsNaN = isNaN(parseInt(char)) - if ((lastBg == this.fg) && (lastFg == this.bg)) { - lastBg = this.bg; lastFg = this.fg - return "\x16" + char - } else if ((lastBg != this.bg) && (lastFg == this.fg)) { - lastBg = this.bg - return "\x03," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char - } else if ((lastBg == this.bg) && (lastFg != this.fg)) { - lastFg = this.fg - return "\x03" + ((this.fg&15) < 10 && !charIsNaN ? "0" : "") + (this.fg&15) + char + if ((bg_ == this.fg) && (fg_ == this.bg)) { + bg_ = this.bg; fg_ = this.fg + return [bg_, fg_, "\x16" + char] + } else if ((bg_ != this.bg) && (fg_ == this.fg)) { + bg_ = this.bg + return [bg_, fg_, "\x03," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char] + } else if ((bg_ == this.bg) && (fg_ != this.fg)) { + fg_ = this.fg + return [bg_, fg_, "\x03" + ((this.fg&15) < 10 && !charIsNaN ? "0" : "") + (this.fg&15) + char] } else { - lastBg = this.bg; lastFg = this.fg - return "\x03" + (this.fg&15) + "," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char + bg_ = this.bg; fg_ = this.fg + return [bg_, fg_, "\x03" + (this.fg&15) + "," + ((this.bg&15) < 10 && !charIsNaN ? "0" : "") + (this.bg&15) + char] } } Lex.prototype.ansi = function(){ diff --git a/js/matrix.js b/js/matrix.js index dbc76c7..034c551 100644 --- a/js/matrix.js +++ b/js/matrix.js @@ -272,12 +272,13 @@ Matrix.prototype.mirc = function (opts) { var lines = this.aa.map(function(row, y){ var last, line = "" row.forEach(function(lex, x) { + var bg_ = -1, fg_ = 15 if (lex.eqColor(last)) { line += lex.sanitize() } else { - line += lex.mirc() - last = lex + [bg_, fg_, line_] = lex.mirc(bg_, fg_) + line += line_; last = lex; } }) if (opts && opts.cutoff && line.length > opts.cutoff) {