From d6495a9cfddb65fb9b3cb09771ee0bef88d5d9f7 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 10 Dec 2017 01:19:52 -0400 Subject: Set the ANSI compressor on OVERLOAD (underload?) --- util/ansi.js | 145 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 106 insertions(+), 39 deletions(-) (limited to 'util/ansi.js') diff --git a/util/ansi.js b/util/ansi.js index c796a2e..cfe7b1f 100644 --- a/util/ansi.js +++ b/util/ansi.js @@ -119,7 +119,7 @@ const ansi = { }, - interpret(text, scrRows, scrCols) { + interpret(text, scrRows, scrCols, oldChars = null, lastChar = null) { // Interprets the given ansi code, more or less. const blank = { @@ -285,56 +285,123 @@ const ansi = { } } - // Character concatenation ----------- + // SPOooooOOoky diffing! ------------- + // + // - Search for series of differences. This means a collection of characters + // which have different text or attribute properties. + // + // - Figure out how to print these differences. Move the cursor to the beginning + // character's row/column, then print the differences. + + const newChars = chars + + const differences = [] + + if (oldChars === null) { + differences.push({i: 0, chars: [...newChars]}) + } else { + const charsEqual = (oldChar, newChar) => { + // TODO: Check attributes. + + if (oldChar.char !== newChar.char) { + return false + } + + let oldAttrs = oldChar.attributes.slice() + let newAttrs = newChar.attributes.slice() + + while (newAttrs.length) { + const attr = newAttrs.shift() + if (oldAttrs.includes(attr)) { + oldAttrs.splice(oldAttrs.indexOf(attr), 1) + } else { + return false + } + } + + oldAttrs = oldChar.attributes.slice() + newAttrs = newChar.attributes.slice() + + while (oldAttrs.length) { + const attr = oldAttrs.shift() + if (newAttrs.includes(attr)) { + newAttrs.splice(newAttrs.indexOf(attr), 1) + } else { + return false + } + } + + return true + } + + let curDiff = null + + for (let i = 0; i < chars.length; i++) { + const oldChar = oldChars[i] + const newChar = newChars[i] + + // TODO: Some sort of "distance" before we should clear curDiff? + // It may take *less* characters if this diff and the next are merged + // (entering a single character is smaller than the length of the code + // used to move past that character). Probably not very significant of + // an impact, though. + if (charsEqual(oldChar, newChar)) { + curDiff = null + } else { + if (curDiff === null) { + curDiff = {i, chars: []} + differences.push(curDiff) + } + + curDiff.chars.push(newChar) + } + } + } - // Move to the top left of the screen initially. - const result = [ ansi.moveCursorRaw(1, 1) ] + // Character concatenation ----------- - let lastChar = { + lastChar = lastChar || { char: '', attributes: [] } - //let n = 1 // debug - - for (const char of chars) { - const newAttributes = ( - char.attributes.filter(attr => !(lastChar.attributes.includes(attr))) - ) - - const removedAttributes = ( - lastChar.attributes.filter(attr => !(char.attributes.includes(attr))) - ) - - // The only way to practically remove any character attribute is to - // reset all of its attributes and then re-add its existing attributes. - // If we do that, there's no need to add new attributes. - if (removedAttributes.length) { - // console.log( - // `removed some attributes "${char.char}"`, removedAttributes - // ) - result.push(ansi.resetAttributes()) - result.push(`${ESC}[${char.attributes.join(';')}m`) - } else if (newAttributes.length) { - result.push(`${ESC}[${newAttributes.join(';')}m`) - } + const result = [] - // debug - /* - if (char.char !== ' ') { - console.log( - `#2-char ${char.char}; ${chars.indexOf(char) - n} inbetween` + for (const diff of differences) { + const col = diff.i % scrCols + const row = (diff.i - col) / scrCols + result.push(ansi.moveCursor(row, col)) + + for (const char of diff.chars) { + const newAttributes = ( + char.attributes.filter(attr => !(lastChar.attributes.includes(attr))) ) - n = chars.indexOf(char) - } - */ - result.push(char.char) + const removedAttributes = ( + lastChar.attributes.filter(attr => !(char.attributes.includes(attr))) + ) - lastChar = char + // The only way to practically remove any character attribute is to + // reset all of its attributes and then re-add its existing attributes. + // If we do that, there's no need to add new attributes. + if (removedAttributes.length) { + result.push(ansi.resetAttributes()) + result.push(`${ESC}[${char.attributes.join(';')}m`) + } else if (newAttributes.length) { + result.push(`${ESC}[${newAttributes.join(';')}m`) + } + + result.push(char.char) + + lastChar = char + } } - return result.join('') + return { + newChars: newChars.slice(), + lastChar: Object.assign({}, lastChar), + screen: result.join('') + } } } -- cgit 1.3.0-6-gf8a5