5 import { ITerminal } from './Interfaces';
8 * The maximum number of refresh frames to skip when the write buffer is non-
9 * empty. Note that these frames may be intermingled with frames that are
10 * skipped via requestAnimationFrame's mechanism.
12 const MAX_REFRESH_FRAME_SKIP = 5;
15 * Flags used to render terminal text properly.
25 let brokenBold: boolean = null;
27 export class Renderer {
28 /** A queue of the rows to be refreshed */
29 private _refreshRowsQueue: {start: number, end: number}[] = [];
30 private _refreshFramesSkipped = 0;
31 private _refreshAnimationFrame = null;
33 constructor(private _terminal: ITerminal) {
34 // Figure out whether boldness affects
35 // the character width of monospace fonts.
36 if (brokenBold === null) {
37 brokenBold = checkBoldBroken((<any>this._terminal).document);
40 // TODO: Pull more DOM interactions into Renderer.constructor, element for
41 // example should be owned by Renderer (and also exposed by Terminal due to
42 // to established public API).
46 * Queues a refresh between two rows (inclusive), to be done on next animation
48 * @param {number} start The start row.
49 * @param {number} end The end row.
51 public queueRefresh(start: number, end: number): void {
52 this._refreshRowsQueue.push({ start: start, end: end });
53 if (!this._refreshAnimationFrame) {
54 this._refreshAnimationFrame = window.requestAnimationFrame(this._refreshLoop.bind(this));
59 * Performs the refresh loop callback, calling refresh only if a refresh is
60 * necessary before queueing up the next one.
62 private _refreshLoop(): void {
63 // Skip MAX_REFRESH_FRAME_SKIP frames if the writeBuffer is non-empty as it
64 // will need to be immediately refreshed anyway. This saves a lot of
65 // rendering time as the viewport DOM does not need to be refreshed, no
66 // scroll events, no layouts, etc.
67 const skipFrame = this._terminal.writeBuffer.length > 0 && this._refreshFramesSkipped++ <= MAX_REFRESH_FRAME_SKIP;
69 this._refreshAnimationFrame = window.requestAnimationFrame(this._refreshLoop.bind(this));
73 this._refreshFramesSkipped = 0;
76 if (this._refreshRowsQueue.length > 4) {
77 // Just do a full refresh when 5+ refreshes are queued
79 end = this._terminal.rows - 1;
81 // Get start and end rows that need refreshing
82 start = this._refreshRowsQueue[0].start;
83 end = this._refreshRowsQueue[0].end;
84 for (let i = 1; i < this._refreshRowsQueue.length; i++) {
85 if (this._refreshRowsQueue[i].start < start) {
86 start = this._refreshRowsQueue[i].start;
88 if (this._refreshRowsQueue[i].end > end) {
89 end = this._refreshRowsQueue[i].end;
93 this._refreshRowsQueue = [];
94 this._refreshAnimationFrame = null;
95 this._refresh(start, end);
99 * Refreshes (re-renders) terminal content within two rows (inclusive)
103 * In the screen buffer, each character is stored as a an array with a character
104 * and a 32-bit integer:
105 * - First value: a utf-16 character.
107 * - Next 9 bits: background color (0-511).
108 * - Next 9 bits: foreground color (0-511).
109 * - Next 14 bits: a mask for misc. flags:
116 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
117 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
119 private _refresh(start: number, end: number): void {
120 let x, y, i, line, out, ch, ch_width, width, data, attr, bg, fg, flags, row, parent, focused = document.activeElement;
122 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
123 if (end - start >= this._terminal.rows / 2) {
124 parent = this._terminal.element.parentNode;
126 this._terminal.element.removeChild(this._terminal.rowContainer);
130 width = this._terminal.cols;
133 if (end >= this._terminal.rows) {
134 this._terminal.log('`end` is too large. Most likely a bad CSR.');
135 end = this._terminal.rows - 1;
138 for (; y <= end; y++) {
139 row = y + this._terminal.ydisp;
141 line = this._terminal.lines.get(row);
142 if (!line || !this._terminal.children[y]) {
143 // Continue if the line is not available, this means a resize is currently in progress
148 if (this._terminal.y === y - (this._terminal.ybase - this._terminal.ydisp)
149 && this._terminal.cursorState
150 && !this._terminal.cursorHidden) {
151 x = this._terminal.x;
156 attr = this._terminal.defAttr;
159 for (; i < width; i++) {
161 // Continue if the character is not available, this means a resize is currently in progress
166 ch_width = line[i][2];
170 if (i === x) data = -1;
173 if (attr !== this._terminal.defAttr) {
176 if (data !== this._terminal.defAttr) {
178 out += '<span class="reverse-video terminal-cursor">';
183 fg = (data >> 9) & 0x1ff;
186 if (flags & FLAGS.BOLD) {
188 classNames.push('xterm-bold');
190 // See: XTerm*boldColors
194 if (flags & FLAGS.UNDERLINE) {
195 classNames.push('xterm-underline');
198 if (flags & FLAGS.BLINK) {
199 classNames.push('xterm-blink');
202 // If inverse flag is on, then swap the foreground and background variables.
203 if (flags & FLAGS.INVERSE) {
204 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
205 bg = [fg, fg = bg][0];
206 // Should inverse just be before the
207 // above boldColors effect instead?
208 if ((flags & 1) && fg < 8) fg += 8;
211 if (flags & FLAGS.INVISIBLE) {
212 classNames.push('xterm-hidden');
216 * Weird situation: Invert flag used black foreground and white background results
217 * in invalid background color, positioned at the 256 index of the 256 terminal
218 * color map. Pin the colors manually in such a case.
220 * Source: https://github.com/sourcelair/xterm.js/issues/57
222 if (flags & FLAGS.INVERSE) {
232 classNames.push('xterm-bg-color-' + bg);
236 classNames.push('xterm-color-' + fg);
240 if (classNames.length) {
241 out += ' class="' + classNames.join(' ') + '"';
248 if (ch_width === 2) {
249 out += '<span class="xterm-wide-char">';
269 if (ch_width === 2) {
276 if (attr !== this._terminal.defAttr) {
280 this._terminal.children[y].innerHTML = out;
284 this._terminal.element.appendChild(this._terminal.rowContainer);
287 this._terminal.emit('refresh', {element: this._terminal.element, start: start, end: end});
292 // if bold is broken, we can't
293 // use it in the terminal.
294 function checkBoldBroken(document) {
295 const body = document.getElementsByTagName('body')[0];
296 const el = document.createElement('span');
297 el.innerHTML = 'hello world';
298 body.appendChild(el);
299 const w1 = el.scrollWidth;
300 el.style.fontWeight = 'bold';
301 const w2 = el.scrollWidth;
302 body.removeChild(el);