<commit_before>
const defaultSettings = {
    charWidth: 10,
    charHeight: 20,
    maxColumns: 120,
    maxRows: 50,
    leftOffset: 0
};

function BufferView(rootElem, config) {
    console.log('BufferView created.');

    const _config = config || {};

    this.lineElems = [null];
    this.charWidth = _config.charWidth || defaultSettings.charWidth; // pixels
    this.charHeight = _config.charHeight || defaultSettings.charHeight; // pixels
    this.maxColumns = _config.maxColumns || defaultSettings.maxColumns;
    this.maxRows = _config.maxRows || defaultSettings.maxRows;

    this.domNode = document.createElement('div');
    this.domNode.className = 'buffer';
    this.domNode.style.left = (_config.leftOffset || defaultSettings.leftOffset) + 'px';
    this.domNode.style.width = (this.maxColumns * this.charWidth) + 'px';
    this.domNode.style.height = (this.maxRows * this.charHeight) + 'px';

    // Start with empty line.
    this.appendLine(' ');

    if (rootElem) {
        rootElem.appendChild(this.domNode); 
    } else {
        document.body.appendChild(this.domNode); 
    }
}

BufferView.prototype.appendLine = function(text) {
    const line = document.createElement('span');
    line.className = 'line';
    line.style.height = this.charHeight;
    line.innerHTML = text;

    this.lineElems.push(line);
    this.domNode.appendChild(line);
};

BufferView.prototype.changeLine = function(num, text) {
    const line = this.lineElems[num];
    if (!line) {
        throw new Error('BufferView: No line with number ' + num); 
    }

    console.log('Changing line ' + num); // TODO: remove
    line.innerHTML = text; 
};

BufferView.prototype.insertLine = function(num, text) {
    if (num < 1 || num > this.lineElems.length) {
        throw new Error('BufferView: No line with number ' + num); 
    }
    if (num === this.lineElems.length) {
        this.appendLine(text);
        return;
    }
    
    const line = document.createElement('span');
    line.className = 'line';
    line.innerHTML = text;

    this.lineElems.splice(num, 0, line);
    this.domNode.insertBefore(line, this.lineElems[num + 1]); 
};

BufferView.prototype.removeLine = function(num) {
    if (num < 1 || num >= this.lineElems.length) {
        throw new Error('BufferView: No line with number ' + num); 
    }
    const removed = this.lineElems.splice(num, 1)[0];
    this.domNode.removeChild(removed); 
};

BufferView.prototype.lastRowNum = function() {
    return this.lineElems.length - 1;
};

BufferView.prototype.setLeftOffset = function(width) {
    this.domNode.style.left = width + 'px';
};

module.exports.BufferView = BufferView; <commit_msg>Adjust height and width of buffer properly.<commit_after>
const defaultSettings = {
    charWidth: 10,
    charHeight: 20,
    maxColumns: 120,
    maxRows: 50,
    leftOffset: 0
};

// TODO: Fix setting of height after insertion/line removal.

function BufferView(rootElem, config) {
    console.log('BufferView created.');

    const _config = config || {};

    this.lineElems = [null];
    this.charWidth = _config.charWidth || defaultSettings.charWidth; // pixels
    this.charHeight = _config.charHeight || defaultSettings.charHeight; // pixels
    this.maxColumns = _config.maxColumns || defaultSettings.maxColumns;
    this.maxRows = _config.maxRows || defaultSettings.maxRows;

    this.domNode = document.createElement('div');
    this.domNode.className = 'buffer';
    this.domNode.style.left = (_config.leftOffset || defaultSettings.leftOffset) + 'px';
    this.domNode.style.width = this.getWidth() + 'px';
    this.domNode.style.height = this.getHeight() + 'px';

    // Start with empty line.
    this.appendLine('');

    if (rootElem) {
        rootElem.appendChild(this.domNode); 
    } else {
        document.body.appendChild(this.domNode); 
    }
}

BufferView.prototype.appendLine = function(text) {
    this.insertLine(this.lineElems.length, text);
};

BufferView.prototype.changeLine = function(num, text) {
    if (num < 1 || num > this.lineElems.length) {
        throw new Error('BufferView: No line with number ' + num);
    }

    if (text.length > this.maxColumns) {
        this.maxColumns += 10;
        this.domNode.style.width = this.getWidth() + 'px'; 
    }
    
    const line = this.lineElems[num];
    line.innerHTML = text; 
};

BufferView.prototype.insertLine = function(num, text) {
    if (num < 1 || num > this.lineElems.length) {
        throw new Error('BufferView: No line with number ' + num); 
    }

    if (text.length > this.maxColumns) {
        this.maxColumns += 10;
        this.domNode.style.width = this.getWidth() + 'px';
    }
    
    if (this.lastRowNum() >= this.maxRows) {
        this.maxRows++;
        this.domNode.style.height = this.getHeight() + 'px';
    }

    const line = document.createElement('span');
    line.className = 'line';
    line.style.height = this.charHeight;
    line.innerHTML = text;

    this.lineElems.splice(num, 0, line);
    if (num == this.lineElems.length) {
        this.domNode.appendChild(line);
    } else {
        this.domNode.insertBefore(line, this.lineElems[num + 1]);         
    }
};

BufferView.prototype.removeLine = function(num) {
    if (num < 1 || num >= this.lineElems.length) {
        throw new Error('BufferView: No line with number ' + num); 
    }
    const removed = this.lineElems.splice(num, 1)[0];
    this.domNode.removeChild(removed); 
};

BufferView.prototype.lastRowNum = function() {
    return this.lineElems.length - 1;
};

BufferView.prototype.setLeftOffset = function(width) {
    this.domNode.style.left = width + 'px';
};

BufferView.prototype.getWidth = function() { return this.maxColumns * this.charWidth; };
BufferView.prototype.getHeight = function() { return this.maxRows * this.charHeight; };

module.exports.BufferView = BufferView; 
{'fraction_non_alphanumeric': 0.14001713796058268, 'fraction_numerical': 0.006683804627249357, 'mean_word_length': 3.089698668535389, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '759687', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1817, 'n_words': 366}