* @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
*/
private _refresh(start: number, end: number): void {
- let x, y, i, line, out, ch, ch_width, width, data, attr, bg, fg, flags, row, parent, focused = document.activeElement;
-
// If this is a big refresh, remove the terminal rows from the DOM for faster calculations
+ let parent;
if (end - start >= this._terminal.rows / 2) {
parent = this._terminal.element.parentNode;
if (parent) {
}
}
- width = this._terminal.cols;
- y = start;
+ let width = this._terminal.cols;
+ let y = start;
if (end >= this._terminal.rows) {
this._terminal.log('`end` is too large. Most likely a bad CSR.');
}
for (; y <= end; y++) {
- row = y + this._terminal.ydisp;
+ let row = y + this._terminal.buffer.ydisp;
- line = this._terminal.lines.get(row);
- out = '';
+ let line = this._terminal.buffer.lines.get(row);
- if (this._terminal.y === y - (this._terminal.ybase - this._terminal.ydisp)
- && this._terminal.cursorState
- && !this._terminal.cursorHidden) {
- x = this._terminal.x;
+ let x;
+ if (this._terminal.buffer.y === y - (this._terminal.buffer.ybase - this._terminal.buffer.ydisp) &&
+ this._terminal.cursorState &&
+ !this._terminal.cursorHidden) {
+ x = this._terminal.buffer.x;
} else {
x = -1;
}
- attr = this._terminal.defAttr;
- i = 0;
+ let attr = this._terminal.defAttr;
- var documentFragment = document.createDocumentFragment();
- var innerHTML = '';
- var currentElement;
+ const documentFragment = document.createDocumentFragment();
+ let innerHTML = '';
+ let currentElement;
// Return the row's spans to the pool
while (this._terminal.children[y].children.length) {
- var child = this._terminal.children[y].children[0];
+ const child = this._terminal.children[y].children[0];
this._terminal.children[y].removeChild(child);
this._spanElementObjectPool.release(<HTMLElement>child);
}
- for (; i < width; i++) {
- data = line[i][0];
- ch = line[i][1];
- ch_width = line[i][2];
- if (!ch_width)
+ for (let i = 0; i < width; i++) {
+ // TODO: Could data be a more specific type?
+ let data: any = line[i][0];
+ const ch = line[i][1];
+ const ch_width: any = line[i][2];
+ const isCursor: boolean = i === x;
+ if (!ch_width) {
continue;
+ }
- if (i === x) data = -1;
-
- if (data !== attr) {
- if (attr !== this._terminal.defAttr) {
+ if (data !== attr || isCursor) {
+ if (attr !== this._terminal.defAttr && !isCursor) {
if (innerHTML) {
currentElement.innerHTML = innerHTML;
innerHTML = '';
}
documentFragment.appendChild(currentElement);
currentElement = null;
- //out += '</span>';
}
- if (data !== this._terminal.defAttr) {
+ if (data !== this._terminal.defAttr || isCursor) {
if (innerHTML && !currentElement) {
currentElement = this._spanElementObjectPool.acquire();
- // For some reason the text nodes only containing don't get added to the DOM
- //currentElement = document.createTextNode('');
}
if (currentElement) {
if (innerHTML) {
documentFragment.appendChild(currentElement);
}
currentElement = this._spanElementObjectPool.acquire();
- if (data === -1) {
- currentElement.classList.add('reverse-video', 'terminal-cursor');
- //out += '<span class="reverse-video terminal-cursor';
- // if (this._terminal.getOption('cursorBlink')) {
- // currentElement.classList.add('blinking');
- // //out += ' blinking';
- // }
- //out += '">';
- } else {
- //var classNames = [];
-
- bg = data & 0x1ff;
- fg = (data >> 9) & 0x1ff;
- flags = data >> 18;
-
- if (flags & FLAGS.BOLD) {
- if (!brokenBold) {
- currentElement.classList.add('xterm-bold');
- //classNames.push('xterm-bold');
- }
- // See: XTerm*boldColors
- if (fg < 8) fg += 8;
- }
- if (flags & FLAGS.UNDERLINE) {
- currentElement.classList.add('xterm-underline');
- //classNames.push('xterm-underline');
- }
+ let bg = data & 0x1ff;
+ let fg = (data >> 9) & 0x1ff;
+ let flags = data >> 18;
- if (flags & FLAGS.BLINK) {
- currentElement.classList.add('xterm-blink');
- //classNames.push('xterm-blink');
- }
+ if (isCursor) {
+ currentElement.classList.add('reverse-video');
+ currentElement.classList.add('terminal-cursor');
+ }
- // If inverse flag is on, then swap the foreground and background variables.
- if (flags & FLAGS.INVERSE) {
- /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
- bg = [fg, fg = bg][0];
- // Should inverse just be before the
- // above boldColors effect instead?
- if ((flags & 1) && fg < 8) fg += 8;
+ if (flags & FLAGS.BOLD) {
+ if (!brokenBold) {
+ currentElement.classList.add('xterm-bold');
}
-
- if (flags & FLAGS.INVISIBLE) {
- currentElement.classList.add('xterm-hidden');
- //classNames.push('xterm-hidden');
+ // See: XTerm*boldColors
+ if (fg < 8) {
+ fg += 8;
}
+ }
- /**
- * Weird situation: Invert flag used black foreground and white background results
- * in invalid background color, positioned at the 256 index of the 256 terminal
- * color map. Pin the colors manually in such a case.
- *
- * Source: https://github.com/sourcelair/xterm.js/issues/57
- */
- if (flags & FLAGS.INVERSE) {
- if (bg == 257) {
- bg = 15;
- }
- if (fg == 256) {
- fg = 0;
- }
- }
+ if (flags & FLAGS.UNDERLINE) {
+ currentElement.classList.add('xterm-underline');
+ }
+
+ if (flags & FLAGS.BLINK) {
+ currentElement.classList.add('xterm-blink');
+ }
- if (bg < 256) {
- currentElement.classList.add('xterm-bg-color-' + bg);
- //classNames.push('xterm-bg-color-' + bg);
+ // If inverse flag is on, then swap the foreground and background variables.
+ if (flags & FLAGS.INVERSE) {
+ let temp = bg;
+ bg = fg;
+ fg = temp;
+ // Should inverse just be before the above boldColors effect instead?
+ if ((flags & 1) && fg < 8) {
+ fg += 8;
}
+ }
+
+ if (flags & FLAGS.INVISIBLE && !isCursor) {
+ currentElement.classList.add('xterm-hidden');
+ }
- if (fg < 256) {
- currentElement.classList.add('xterm-color-' + fg);
- //classNames.push('xterm-color-' + fg);
+ /**
+ * Weird situation: Invert flag used black foreground and white background results
+ * in invalid background color, positioned at the 256 index of the 256 terminal
+ * color map. Pin the colors manually in such a case.
+ *
+ * Source: https://github.com/sourcelair/xterm.js/issues/57
+ */
+ if (flags & FLAGS.INVERSE) {
+ if (bg === 257) {
+ bg = 15;
+ }
+ if (fg === 256) {
+ fg = 0;
}
+ }
- // out += '<span';
- // if (classNames.length) {
- // out += ' class="' + classNames.join(' ') + '"';
- // }
- // out += '>';
+ if (bg < 256) {
+ currentElement.classList.add(`xterm-bg-color-${bg}`);
}
- }
- }
- // TODO: Consider performance implications of not pulling these from the pool
- if (ch_width === 2) {
- innerHTML += '<span class="xterm-wide-char">';
- } else {
- // TODO: Only wrap unicode characters that may vary in width
- innerHTML += '<span class="xterm-normal-char">';
- }
- switch (ch) {
- case '&':
- innerHTML += '&';
- //out += '&';
- break;
- case '<':
- innerHTML += '<';
- //out += '<';
- break;
- case '>':
- innerHTML += '>';
- //out += '>';
- break;
- default:
- if (ch <= ' ') {
- innerHTML += ' ';
- //out += ' ';
- } else {
- innerHTML += ch;
- // out += ch;
+ if (fg < 256) {
+ currentElement.classList.add(`xterm-color-${fg}`);
}
- break;
+
+ }
}
+
if (ch_width === 2) {
- innerHTML += '</span>';
+ // Wrap wide characters so they're sized correctly. It's more difficult to release these
+ // from the object pool so just create new ones via innerHTML.
+ innerHTML += `<span class="xterm-wide-char">${ch}</span>`;
+ } else if (ch.charCodeAt(0) > 255) {
+ // Wrap any non-wide unicode character as some fonts size them badly
+ innerHTML += `<span class="xterm-normal-char">${ch}</span>`;
} else {
- // TODO: Only wrap unicode characters that may vary in width
- innerHTML += '</span>';
+ switch (ch) {
+ case '&':
+ innerHTML += '&';
+ break;
+ case '<':
+ innerHTML += '<';
+ break;
+ case '>':
+ innerHTML += '>';
+ break;
+ default:
+ if (ch <= ' ') {
+ innerHTML += ' ';
+ } else {
+ innerHTML += ch;
+ }
+ break;
+ }
}
- attr = data;
+ // The cursor needs its own element, therefore we set attr to -1
+ // which will cause the next character to be rendered in a new element
+ attr = isCursor ? -1 : data;
+
}
if (innerHTML && !currentElement) {
currentElement = this._spanElementObjectPool.acquire();
- // For some reason the text nodes only containing don't get added to the DOM
- //currentElement = document.createTextNode('');
}
if (currentElement) {
if (innerHTML) {
documentFragment.appendChild(currentElement);
currentElement = null;
}
- // if (attr !== this.defAttr) {
- // out += '</span>';
- // }
-
- //this.children[y].innerHTML = out;
- //this.children[y].innerHTML = '';
- this._terminal.children[y].appendChild(documentFragment)
+ this._terminal.children[y].appendChild(documentFragment);
}
if (parent) {
this._terminal.emit('refresh', {element: this._terminal.element, start: start, end: end});
};
+
+ /**
+ * Refreshes the selection in the DOM.
+ * @param start The selection start.
+ * @param end The selection end.
+ */
+ public refreshSelection(start: [number, number], end: [number, number]) {
+ // Remove all selections
+ while (this._terminal.selectionContainer.children.length) {
+ this._terminal.selectionContainer.removeChild(this._terminal.selectionContainer.children[0]);
+ }
+
+ // Selection does not exist
+ if (!start || !end) {
+ return;
+ }
+
+ // Translate from buffer position to viewport position
+ const viewportStartRow = start[1] - this._terminal.buffer.ydisp;
+ const viewportEndRow = end[1] - this._terminal.buffer.ydisp;
+ const viewportCappedStartRow = Math.max(viewportStartRow, 0);
+ const viewportCappedEndRow = Math.min(viewportEndRow, this._terminal.rows - 1);
+
+ // No need to draw the selection
+ if (viewportCappedStartRow >= this._terminal.rows || viewportCappedEndRow < 0) {
+ return;
+ }
+
+ // Create the selections
+ const documentFragment = document.createDocumentFragment();
+ // Draw first row
+ const startCol = viewportStartRow === viewportCappedStartRow ? start[0] : 0;
+ const endCol = viewportCappedStartRow === viewportCappedEndRow ? end[0] : this._terminal.cols;
+ documentFragment.appendChild(this._createSelectionElement(viewportCappedStartRow, startCol, endCol));
+ // Draw middle rows
+ const middleRowsCount = viewportCappedEndRow - viewportCappedStartRow - 1;
+ documentFragment.appendChild(this._createSelectionElement(viewportCappedStartRow + 1, 0, this._terminal.cols, middleRowsCount));
+ // Draw final row
+ if (viewportCappedStartRow !== viewportCappedEndRow) {
+ // Only draw viewportEndRow if it's not the same as viewporttartRow
+ const endCol = viewportEndRow === viewportCappedEndRow ? end[0] : this._terminal.cols;
+ documentFragment.appendChild(this._createSelectionElement(viewportCappedEndRow, 0, endCol));
+ }
+ this._terminal.selectionContainer.appendChild(documentFragment);
+ }
+
+ /**
+ * Creates a selection element at the specified position.
+ * @param row The row of the selection.
+ * @param colStart The start column.
+ * @param colEnd The end columns.
+ */
+ private _createSelectionElement(row: number, colStart: number, colEnd: number, rowCount: number = 1): HTMLElement {
+ const element = document.createElement('div');
+ element.style.height = `${rowCount * this._terminal.charMeasure.height}px`;
+ element.style.top = `${row * this._terminal.charMeasure.height}px`;
+ element.style.left = `${colStart * this._terminal.charMeasure.width}px`;
+ element.style.width = `${this._terminal.charMeasure.width * (colEnd - colStart)}px`;
+ return element;
+ }
}
-// if bold is broken, we can't
-// use it in the terminal.
+// If bold is broken, we can't use it in the terminal.
function checkBoldBroken(terminal) {
const document = terminal.ownerDocument;
const el = document.createElement('span');