blob: 11fdfbf7e8dacbaf1c37738653b9263f7ae7d0e9 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * Constructor for the Terminal class.
7 *
8 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
9 * classes to provide the complete terminal functionality.
10 *
11 * There are a number of lower-level Terminal methods that can be called
12 * directly to manipulate the cursor, text, scroll region, and other terminal
13 * attributes. However, the primary method is interpret(), which parses VT
14 * escape sequences and invokes the appropriate Terminal methods.
15 *
16 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
17 *
18 * TODO(rginda): Eventually we're going to need to support characters which are
19 * displayed twice as wide as standard latin characters. This is to support
20 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080021 *
22 * @param {string} opt_profileName Optional preference profile name. If not
23 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080024 */
rginda9f5222b2012-03-05 11:53:28 -080025hterm.Terminal = function(opt_profileName) {
26 this.profileName_ = null;
27 this.setProfile(opt_profileName || 'default');
28
rginda8ba33642011-12-14 12:31:31 -080029 // Two screen instances.
30 this.primaryScreen_ = new hterm.Screen();
31 this.alternateScreen_ = new hterm.Screen();
32
33 // The "current" screen.
34 this.screen_ = this.primaryScreen_;
35
rginda8ba33642011-12-14 12:31:31 -080036 // The local notion of the screen size. ScreenBuffers also have a size which
37 // indicates their present size. During size changes, the two may disagree.
38 // Also, the inactive screen's size is not altered until it is made the active
39 // screen.
40 this.screenSize = new hterm.Size(0, 0);
41
rginda8ba33642011-12-14 12:31:31 -080042 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080043 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080044 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
45 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080046 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rginda8ba33642011-12-14 12:31:31 -080047
rginda87b86462011-12-14 13:48:03 -080048 // The div that contains this terminal.
49 this.div_ = null;
50
rgindac9bc5502012-01-18 11:48:44 -080051 // The document that contains the scrollPort. Defaulted to the global
52 // document here so that the terminal is functional even if it hasn't been
53 // inserted into a document yet, but re-set in decorate().
54 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080055
rginda8ba33642011-12-14 12:31:31 -080056 // The rows that have scrolled off screen and are no longer addressable.
57 this.scrollbackRows_ = [];
58
rgindac9bc5502012-01-18 11:48:44 -080059 // Saved tab stops.
60 this.tabStops_ = [];
61
rginda8ba33642011-12-14 12:31:31 -080062 // The VT's notion of the top and bottom rows. Used during some VT
63 // cursor positioning and scrolling commands.
64 this.vtScrollTop_ = null;
65 this.vtScrollBottom_ = null;
66
67 // The DIV element for the visible cursor.
68 this.cursorNode_ = null;
69
rginda9f5222b2012-03-05 11:53:28 -080070 // These prefs are cached so we don't have to read from local storage with
71 // each output and keystroke.
72 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
73 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
74
rgindaf0090c92012-02-10 14:58:52 -080075 // Terminal bell sound.
76 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080077 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080078 this.bellAudio_.setAttribute('preload', 'auto');
79
rginda6d397402012-01-17 10:58:29 -080080 // Cursor position and attributes saved with DECSC.
81 this.savedOptions_ = {};
82
rginda8ba33642011-12-14 12:31:31 -080083 // The current mode bits for the terminal.
84 this.options_ = new hterm.Options();
85
86 // Timeouts we might need to clear.
87 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080088
89 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080090 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080091
rgindafeaf3142012-01-31 15:14:20 -080092 // The keyboard hander.
93 this.keyboard = new hterm.Keyboard(this);
94
rginda87b86462011-12-14 13:48:03 -080095 // General IO interface that can be given to third parties without exposing
96 // the entire terminal object.
97 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -080098
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +040099 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800100 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800101};
102
103/**
rginda35c456b2012-02-09 17:29:05 -0800104 * Default tab with of 8 to match xterm.
105 */
106hterm.Terminal.prototype.tabWidth = 8;
107
108/**
rginda35c456b2012-02-09 17:29:05 -0800109 * The assumed width of a scrollbar.
110 */
111hterm.Terminal.prototype.scrollbarWidthPx = 16;
112
113/**
rginda9f5222b2012-03-05 11:53:28 -0800114 * Select a preference profile.
115 *
116 * This will load the terminal preferences for the given profile name and
117 * associate subsequent preference changes with the new preference profile.
118 *
119 * @param {string} newName The name of the preference profile. Forward slash
120 * characters will be removed from the name.
121 */
122hterm.Terminal.prototype.setProfile = function(profileName) {
123 // If we already have a profile selected, we're going to need to re-sync
124 // with the new profile.
125 var needSync = !!this.profileName_;
126
127 this.profileName_ = profileName.replace(/\//g, '');
128
129 this.prefs_ = new hterm.PreferenceManager(
130 '/hterm/prefs/profiles/' + this.profileName_);
131
132 var self = this;
133 this.prefs_.definePreferences
134 ([/**
135 * The default colors for text with no other color attributes.
136 */
137 ['foreground-color', 'white', function(v) {
138 self.scrollPort_.setForegroundColor(v);
139 }
140 ],
141
142 ['background-color', 'black', function(v) {
143 self.scrollPort_.setBackgroundColor(v);
144 }
145 ],
146
147 /**
148 * Default font family for the terminal text.
149 */
150 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
151 'FreeMono, "Menlo", "Lucida Console", ' +
152 'monospace'),
153 function(v) { self.syncFontFamily() }
154 ],
155
156 /**
157 * Anti-aliasing.
158 */
159 ['font-smoothing', 'antialiased',
160 function(v) { self.syncFontFamily() }
161 ],
162
163 /**
164 * True if we should use bold weight font for text with the bold/bright
165 * attribute. False to use bright colors only. Null to autodetect.
166 */
167 ['enable-bold', null, function(v) {
168 self.syncBoldSafeState();
169 }
170 ],
171
172 /**
173 * The color of the visible cursor.
174 */
175 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
176 self.cursorNode_.style.backgroundColor = v;
177 }
178 ],
179
180 /**
181 * If true, scroll to the bottom on any keystroke.
182 */
183 ['scroll-on-keystroke', true, function(v) {
184 self.scrollOnKeystroke_ = v;
185 }
186 ],
187
188 /**
189 * If true, scroll to the bottom on terminal output.
190 */
191 ['scroll-on-output', false, function(v) {
192 self.scrollOnOutput_ = v;
193 }
194 ],
195
196 /**
197 * The default font size in pixels.
198 */
199 ['font-size', 15, function(v) {
200 self.setFontSize(v);
201 }
202 ],
203
204 /**
205 * Terminal bell sound. Empty string for no audible bell.
206 */
207 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
208 self.bellAudio_.setAttribute('src', v);
209 }
210 ],
David Reveman8f552492012-03-28 12:18:41 -0400211
212 /**
213 * The vertical scrollbar mode.
214 */
215 ['scrollbar-visible', true, function(v) {
216 self.setScrollbarVisible(v);
217 }
218 ],
rginda9f5222b2012-03-05 11:53:28 -0800219 ]);
220
221 if (needSync)
222 this.prefs_.notifyAll();
223};
224
225/**
226 * Return the current terminal background color.
227 *
228 * Intended for use by other classes, so we don't have to expose the entire
229 * prefs_ object.
230 */
231hterm.Terminal.prototype.getBackgroundColor = function() {
232 return this.prefs_.get('background-color');
233};
234
235/**
236 * Return the current terminal foreground color.
237 *
238 * Intended for use by other classes, so we don't have to expose the entire
239 * prefs_ object.
240 */
241hterm.Terminal.prototype.getForegroundColor = function() {
242 return this.prefs_.get('foreground-color');
243};
244
245/**
rginda87b86462011-12-14 13:48:03 -0800246 * Create a new instance of a terminal command and run it with a given
247 * argument string.
248 *
249 * @param {function} commandClass The constructor for a terminal command.
250 * @param {string} argString The argument string to pass to the command.
251 */
252hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
253 var self = this;
254 this.command = new commandClass(
255 { argString: argString || '',
256 io: this.io.push(),
257 onExit: function(code) {
258 self.io.pop();
259 self.io.println(hterm.msg('COMMAND_COMPLETE',
260 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800261 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800262 }
263 });
264
rgindafeaf3142012-01-31 15:14:20 -0800265 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800266 this.command.run();
267};
268
269/**
rgindafeaf3142012-01-31 15:14:20 -0800270 * Returns true if the current screen is the primary screen, false otherwise.
271 */
272hterm.Terminal.prototype.isPrimaryScreen = function() {
273 return this.screen_ = this.primaryScreen_;
274};
275
276/**
277 * Install the keyboard handler for this terminal.
278 *
279 * This will prevent the browser from seeing any keystrokes sent to the
280 * terminal.
281 */
282hterm.Terminal.prototype.installKeyboard = function() {
283 this.keyboard.installKeyboard(this.document_.body.firstChild);
284}
285
286/**
287 * Uninstall the keyboard handler for this terminal.
288 */
289hterm.Terminal.prototype.uninstallKeyboard = function() {
290 this.keyboard.installKeyboard(null);
291}
292
293/**
rginda35c456b2012-02-09 17:29:05 -0800294 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800295 *
296 * Call setFontSize(0) to reset to the default font size.
297 *
298 * This function does not modify the font-size preference.
299 *
300 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800301 */
302hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800303 if (px === 0)
304 px = this.prefs_.get('font-size');
305
rginda35c456b2012-02-09 17:29:05 -0800306 this.scrollPort_.setFontSize(px);
307};
308
309/**
310 * Get the current font size.
311 */
312hterm.Terminal.prototype.getFontSize = function() {
313 return this.scrollPort_.getFontSize();
314};
315
316/**
317 * Set the CSS "font-family" for this terminal.
318 */
rginda9f5222b2012-03-05 11:53:28 -0800319hterm.Terminal.prototype.syncFontFamily = function() {
320 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
321 this.prefs_.get('font-smoothing'));
322 this.syncBoldSafeState();
323};
324
325hterm.Terminal.prototype.syncBoldSafeState = function() {
326 var enableBold = this.prefs_.get('enable-bold');
327 if (enableBold !== null) {
328 this.screen_.textAttributes.enableBold = enableBold;
329 return;
330 }
331
rgindaf7521392012-02-28 17:20:34 -0800332 var normalSize = this.scrollPort_.measureCharacterSize();
333 var boldSize = this.scrollPort_.measureCharacterSize('bold');
334
335 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800336 if (!isBoldSafe) {
337 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700338 'from normal. Font family is: ' +
339 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800340 }
rginda9f5222b2012-03-05 11:53:28 -0800341
342 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800343};
344
345/**
rginda87b86462011-12-14 13:48:03 -0800346 * Return a copy of the current cursor position.
347 *
348 * @return {hterm.RowCol} The RowCol object representing the current position.
349 */
350hterm.Terminal.prototype.saveCursor = function() {
351 return this.screen_.cursorPosition.clone();
352};
353
rgindaa19afe22012-01-25 15:40:22 -0800354hterm.Terminal.prototype.getTextAttributes = function() {
355 return this.screen_.textAttributes;
356};
357
rginda87b86462011-12-14 13:48:03 -0800358/**
rginda9846e2f2012-01-27 13:53:33 -0800359 * Change the title of this terminal's window.
360 */
361hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800362 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800363};
364
365/**
rginda87b86462011-12-14 13:48:03 -0800366 * Restore a previously saved cursor position.
367 *
368 * @param {hterm.RowCol} cursor The position to restore.
369 */
370hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800371 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
372 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
373 this.screen_.setCursorPosition(row, column);
374 if (cursor.column > column ||
375 cursor.column == column && cursor.overflow) {
376 this.screen_.cursorPosition.overflow = true;
377 }
rginda87b86462011-12-14 13:48:03 -0800378};
379
380/**
381 * Set the width of the terminal, resizing the UI to match.
382 */
383hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800384 if (columnCount == null) {
385 this.div_.style.width = '100%';
386 return;
387 }
388
rginda35c456b2012-02-09 17:29:05 -0800389 this.div_.style.width = this.scrollPort_.characterSize.width *
390 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400391 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800392 this.scheduleSyncCursorPosition_();
393};
rginda87b86462011-12-14 13:48:03 -0800394
rgindac9bc5502012-01-18 11:48:44 -0800395/**
rginda35c456b2012-02-09 17:29:05 -0800396 * Set the height of the terminal, resizing the UI to match.
397 */
398hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800399 if (rowCount == null) {
400 this.div_.style.height = '100%';
401 return;
402 }
403
rginda35c456b2012-02-09 17:29:05 -0800404 this.div_.style.height =
rginda9f5222b2012-03-05 11:53:28 -0800405 this.scrollPort_.characterSize.height * rowCount + 1 + 'px';
rginda35c456b2012-02-09 17:29:05 -0800406 this.realizeSize_(this.screenSize.width, rowCount);
407 this.scheduleSyncCursorPosition_();
408};
409
410/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400411 * Deal with terminal size changes.
412 *
413 */
414hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
415 if (columnCount != this.screenSize.width)
416 this.realizeWidth_(columnCount);
417
418 if (rowCount != this.screenSize.height)
419 this.realizeHeight_(rowCount);
420
421 // Send new terminal size to plugin.
422 this.io.onTerminalResize(columnCount, rowCount);
423};
424
425/**
rgindac9bc5502012-01-18 11:48:44 -0800426 * Deal with terminal width changes.
427 *
428 * This function does what needs to be done when the terminal width changes
429 * out from under us. It happens here rather than in onResize_() because this
430 * code may need to run synchronously to handle programmatic changes of
431 * terminal width.
432 *
433 * Relying on the browser to send us an async resize event means we may not be
434 * in the correct state yet when the next escape sequence hits.
435 */
436hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
437 var deltaColumns = columnCount - this.screen_.getWidth();
438
rginda87b86462011-12-14 13:48:03 -0800439 this.screenSize.width = columnCount;
440 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800441
442 if (deltaColumns > 0) {
443 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
444 } else {
445 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
446 if (this.tabStops_[i] <= columnCount)
447 break;
448
449 this.tabStops_.pop();
450 }
451 }
452
453 this.screen_.setColumnCount(this.screenSize.width);
454};
455
456/**
457 * Deal with terminal height changes.
458 *
459 * This function does what needs to be done when the terminal height changes
460 * out from under us. It happens here rather than in onResize_() because this
461 * code may need to run synchronously to handle programmatic changes of
462 * terminal height.
463 *
464 * Relying on the browser to send us an async resize event means we may not be
465 * in the correct state yet when the next escape sequence hits.
466 */
467hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
468 var deltaRows = rowCount - this.screen_.getHeight();
469
470 this.screenSize.height = rowCount;
471
472 var cursor = this.saveCursor();
473
474 if (deltaRows < 0) {
475 // Screen got smaller.
476 deltaRows *= -1;
477 while (deltaRows) {
478 var lastRow = this.getRowCount() - 1;
479 if (lastRow - this.scrollbackRows_.length == cursor.row)
480 break;
481
482 if (this.getRowText(lastRow))
483 break;
484
485 this.screen_.popRow();
486 deltaRows--;
487 }
488
489 var ary = this.screen_.shiftRows(deltaRows);
490 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
491
492 // We just removed rows from the top of the screen, we need to update
493 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800494 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800495 } else if (deltaRows > 0) {
496 // Screen got larger.
497
498 if (deltaRows <= this.scrollbackRows_.length) {
499 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
500 var rows = this.scrollbackRows_.splice(
501 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
502 this.screen_.unshiftRows(rows);
503 deltaRows -= scrollbackCount;
504 cursor.row += scrollbackCount;
505 }
506
507 if (deltaRows)
508 this.appendRows_(deltaRows);
509 }
510
rginda35c456b2012-02-09 17:29:05 -0800511 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800512 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800513};
514
515/**
516 * Scroll the terminal to the top of the scrollback buffer.
517 */
518hterm.Terminal.prototype.scrollHome = function() {
519 this.scrollPort_.scrollRowToTop(0);
520};
521
522/**
523 * Scroll the terminal to the end.
524 */
525hterm.Terminal.prototype.scrollEnd = function() {
526 this.scrollPort_.scrollRowToBottom(this.getRowCount());
527};
528
529/**
530 * Scroll the terminal one page up (minus one line) relative to the current
531 * position.
532 */
533hterm.Terminal.prototype.scrollPageUp = function() {
534 var i = this.scrollPort_.getTopRowIndex();
535 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
536};
537
538/**
539 * Scroll the terminal one page down (minus one line) relative to the current
540 * position.
541 */
542hterm.Terminal.prototype.scrollPageDown = function() {
543 var i = this.scrollPort_.getTopRowIndex();
544 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800545};
546
rgindac9bc5502012-01-18 11:48:44 -0800547/**
548 * Full terminal reset.
549 */
rginda87b86462011-12-14 13:48:03 -0800550hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800551 this.clearAllTabStops();
552 this.setDefaultTabStops();
rgindac9bc5502012-01-18 11:48:44 -0800553 this.setVTScrollRegion(null, null);
rginda9ea433c2012-03-16 11:57:00 -0700554
555 this.clearHome(this.primaryScreen_);
556 this.primaryScreen_.textAttributes.reset();
557
558 this.clearHome(this.alternateScreen_);
559 this.alternateScreen_.textAttributes.reset();
560
rgindac9bc5502012-01-18 11:48:44 -0800561 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800562};
563
rgindac9bc5502012-01-18 11:48:44 -0800564/**
565 * Soft terminal reset.
566 */
rginda0f5c0292012-01-13 11:00:13 -0800567hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800568 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800569 this.setCursorVisible(true);
570 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800571};
572
rgindac9bc5502012-01-18 11:48:44 -0800573/**
574 * Move the cursor forward to the next tab stop, or to the last column
575 * if no more tab stops are set.
576 */
577hterm.Terminal.prototype.forwardTabStop = function() {
578 var column = this.screen_.cursorPosition.column;
579
580 for (var i = 0; i < this.tabStops_.length; i++) {
581 if (this.tabStops_[i] > column) {
582 this.setCursorColumn(this.tabStops_[i]);
583 return;
584 }
585 }
586
587 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800588};
589
rgindac9bc5502012-01-18 11:48:44 -0800590/**
591 * Move the cursor backward to the previous tab stop, or to the first column
592 * if no previous tab stops are set.
593 */
594hterm.Terminal.prototype.backwardTabStop = function() {
595 var column = this.screen_.cursorPosition.column;
596
597 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
598 if (this.tabStops_[i] < column) {
599 this.setCursorColumn(this.tabStops_[i]);
600 return;
601 }
602 }
603
604 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800605};
606
rgindac9bc5502012-01-18 11:48:44 -0800607/**
608 * Set a tab stop at the given column.
609 *
610 * @param {int} column Zero based column.
611 */
612hterm.Terminal.prototype.setTabStop = function(column) {
613 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
614 if (this.tabStops_[i] == column)
615 return;
616
617 if (this.tabStops_[i] < column) {
618 this.tabStops_.splice(i + 1, 0, column);
619 return;
620 }
621 }
622
623 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800624};
625
rgindac9bc5502012-01-18 11:48:44 -0800626/**
627 * Clear the tab stop at the current cursor position.
628 *
629 * No effect if there is no tab stop at the current cursor position.
630 */
631hterm.Terminal.prototype.clearTabStopAtCursor = function() {
632 var column = this.screen_.cursorPosition.column;
633
634 var i = this.tabStops_.indexOf(column);
635 if (i == -1)
636 return;
637
638 this.tabStops_.splice(i, 1);
639};
640
641/**
642 * Clear all tab stops.
643 */
644hterm.Terminal.prototype.clearAllTabStops = function() {
645 this.tabStops_.length = 0;
646};
647
648/**
649 * Set up the default tab stops, starting from a given column.
650 *
651 * This sets a tabstop every (column % this.tabWidth) column, starting
652 * from the specified column, or 0 if no column is provided.
653 *
654 * This does not clear the existing tab stops first, use clearAllTabStops
655 * for that.
656 *
657 * @param {int} opt_start Optional starting zero based starting column, useful
658 * for filling out missing tab stops when the terminal is resized.
659 */
660hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
661 var start = opt_start || 0;
662 var w = this.tabWidth;
663 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
664 for (var i = 0; i < stopCount; i++) {
665 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
666 }
rginda87b86462011-12-14 13:48:03 -0800667};
668
rginda6d397402012-01-17 10:58:29 -0800669/**
670 * Save cursor position and attributes.
671 *
672 * TODO(rginda): Save attributes once we support them.
673 */
rginda87b86462011-12-14 13:48:03 -0800674hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800675 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800676 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800677};
678
rginda6d397402012-01-17 10:58:29 -0800679/**
680 * Restore cursor position and attributes.
681 *
682 * TODO(rginda): Restore attributes once we support them.
683 */
rginda87b86462011-12-14 13:48:03 -0800684hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800685 if (this.savedOptions_.cursor)
686 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800687 if (this.savedOptions_.textAttributes)
688 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800689};
690
691/**
692 * Interpret a sequence of characters.
693 *
694 * Incomplete escape sequences are buffered until the next call.
695 *
696 * @param {string} str Sequence of characters to interpret or pass through.
697 */
698hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800699 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800700 this.scheduleSyncCursorPosition_();
701};
702
703/**
704 * Take over the given DIV for use as the terminal display.
705 *
706 * @param {HTMLDivElement} div The div to use as the terminal display.
707 */
708hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800709 this.div_ = div;
710
rginda8ba33642011-12-14 12:31:31 -0800711 this.scrollPort_.decorate(div);
rginda0918b652012-04-04 11:26:24 -0700712 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800713
rginda9f5222b2012-03-05 11:53:28 -0800714 this.setFontSize(this.prefs_.get('font-size'));
715 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800716
David Reveman8f552492012-03-28 12:18:41 -0400717 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
718
rginda8ba33642011-12-14 12:31:31 -0800719 this.document_ = this.scrollPort_.getDocument();
720
rginda8ba33642011-12-14 12:31:31 -0800721 this.cursorNode_ = this.document_.createElement('div');
722 this.cursorNode_.style.cssText =
723 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800724 'top: -99px;' +
725 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800726 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
727 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800728 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800729 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800730 this.document_.body.appendChild(this.cursorNode_);
731
732 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800733
rginda87b86462011-12-14 13:48:03 -0800734 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800735 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800736};
737
rginda0918b652012-04-04 11:26:24 -0700738/**
739 * Return the HTML document that contains the terminal DOM nodes.
740 */
rginda87b86462011-12-14 13:48:03 -0800741hterm.Terminal.prototype.getDocument = function() {
742 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800743};
744
745/**
rginda0918b652012-04-04 11:26:24 -0700746 * Focus the terminal.
747 */
748hterm.Terminal.prototype.focus = function() {
749 this.scrollPort_.focus();
750};
751
752/**
rginda8ba33642011-12-14 12:31:31 -0800753 * Return the HTML Element for a given row index.
754 *
755 * This is a method from the RowProvider interface. The ScrollPort uses
756 * it to fetch rows on demand as they are scrolled into view.
757 *
758 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
759 * pairs to conserve memory.
760 *
761 * @param {integer} index The zero-based row index, measured relative to the
762 * start of the scrollback buffer. On-screen rows will always have the
763 * largest indicies.
764 * @return {HTMLElement} The 'x-row' element containing for the requested row.
765 */
766hterm.Terminal.prototype.getRowNode = function(index) {
767 if (index < this.scrollbackRows_.length)
768 return this.scrollbackRows_[index];
769
770 var screenIndex = index - this.scrollbackRows_.length;
771 return this.screen_.rowsArray[screenIndex];
772};
773
774/**
775 * Return the text content for a given range of rows.
776 *
777 * This is a method from the RowProvider interface. The ScrollPort uses
778 * it to fetch text content on demand when the user attempts to copy their
779 * selection to the clipboard.
780 *
781 * @param {integer} start The zero-based row index to start from, measured
782 * relative to the start of the scrollback buffer. On-screen rows will
783 * always have the largest indicies.
784 * @param {integer} end The zero-based row index to end on, measured
785 * relative to the start of the scrollback buffer.
786 * @return {string} A single string containing the text value of the range of
787 * rows. Lines will be newline delimited, with no trailing newline.
788 */
789hterm.Terminal.prototype.getRowsText = function(start, end) {
790 var ary = [];
791 for (var i = start; i < end; i++) {
792 var node = this.getRowNode(i);
793 ary.push(node.textContent);
794 }
795
796 return ary.join('\n');
797};
798
799/**
800 * Return the text content for a given row.
801 *
802 * This is a method from the RowProvider interface. The ScrollPort uses
803 * it to fetch text content on demand when the user attempts to copy their
804 * selection to the clipboard.
805 *
806 * @param {integer} index The zero-based row index to return, measured
807 * relative to the start of the scrollback buffer. On-screen rows will
808 * always have the largest indicies.
809 * @return {string} A string containing the text value of the selected row.
810 */
811hterm.Terminal.prototype.getRowText = function(index) {
812 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800813 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800814};
815
816/**
817 * Return the total number of rows in the addressable screen and in the
818 * scrollback buffer of this terminal.
819 *
820 * This is a method from the RowProvider interface. The ScrollPort uses
821 * it to compute the size of the scrollbar.
822 *
823 * @return {integer} The number of rows in this terminal.
824 */
825hterm.Terminal.prototype.getRowCount = function() {
826 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
827};
828
829/**
830 * Create DOM nodes for new rows and append them to the end of the terminal.
831 *
832 * This is the only correct way to add a new DOM node for a row. Notice that
833 * the new row is appended to the bottom of the list of rows, and does not
834 * require renumbering (of the rowIndex property) of previous rows.
835 *
836 * If you think you want a new blank row somewhere in the middle of the
837 * terminal, look into moveRows_().
838 *
839 * This method does not pay attention to vtScrollTop/Bottom, since you should
840 * be using moveRows() in cases where they would matter.
841 *
842 * The cursor will be positioned at column 0 of the first inserted line.
843 */
844hterm.Terminal.prototype.appendRows_ = function(count) {
845 var cursorRow = this.screen_.rowsArray.length;
846 var offset = this.scrollbackRows_.length + cursorRow;
847 for (var i = 0; i < count; i++) {
848 var row = this.document_.createElement('x-row');
849 row.appendChild(this.document_.createTextNode(''));
850 row.rowIndex = offset + i;
851 this.screen_.pushRow(row);
852 }
853
854 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
855 if (extraRows > 0) {
856 var ary = this.screen_.shiftRows(extraRows);
857 Array.prototype.push.apply(this.scrollbackRows_, ary);
858 this.scheduleScrollDown_();
859 }
860
861 if (cursorRow >= this.screen_.rowsArray.length)
862 cursorRow = this.screen_.rowsArray.length - 1;
863
rginda87b86462011-12-14 13:48:03 -0800864 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800865};
866
867/**
868 * Relocate rows from one part of the addressable screen to another.
869 *
870 * This is used to recycle rows during VT scrolls (those which are driven
871 * by VT commands, rather than by the user manipulating the scrollbar.)
872 *
873 * In this case, the blank lines scrolled into the scroll region are made of
874 * the nodes we scrolled off. These have their rowIndex properties carefully
875 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800876 */
877hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
878 var ary = this.screen_.removeRows(fromIndex, count);
879 this.screen_.insertRows(toIndex, ary);
880
881 var start, end;
882 if (fromIndex < toIndex) {
883 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800884 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800885 } else {
886 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800887 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800888 }
889
890 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800891 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800892};
893
894/**
895 * Renumber the rowIndex property of the given range of rows.
896 *
897 * The start and end indicies are relative to the screen, not the scrollback.
898 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800899 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800900 * no need to renumber scrollback rows.
901 */
902hterm.Terminal.prototype.renumberRows_ = function(start, end) {
903 var offset = this.scrollbackRows_.length;
904 for (var i = start; i < end; i++) {
905 this.screen_.rowsArray[i].rowIndex = offset + i;
906 }
907};
908
909/**
910 * Print a string to the terminal.
911 *
912 * This respects the current insert and wraparound modes. It will add new lines
913 * to the end of the terminal, scrolling off the top into the scrollback buffer
914 * if necessary.
915 *
916 * The string is *not* parsed for escape codes. Use the interpret() method if
917 * that's what you're after.
918 *
919 * @param{string} str The string to print.
920 */
921hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800922 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
923 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800924
rgindaa19afe22012-01-25 15:40:22 -0800925 if (this.options_.insertMode) {
926 this.screen_.insertString(str);
927 } else {
928 this.screen_.overwriteString(str);
929 }
930
931 var overflow = this.screen_.maybeClipCurrentRow();
932
933 if (this.options_.wraparound && overflow) {
934 var lastColumn;
935
936 do {
rginda35c456b2012-02-09 17:29:05 -0800937 this.newLine();
938 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800939
940 if (!this.options_.insertMode)
941 this.screen_.deleteChars(overflow.characterLength);
942
943 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800944
945 overflow = this.screen_.maybeClipCurrentRow();
946 } while (overflow);
947
948 this.setCursorColumn(lastColumn);
949 }
rginda8ba33642011-12-14 12:31:31 -0800950
951 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800952
rginda9f5222b2012-03-05 11:53:28 -0800953 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -0800954 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800955};
956
957/**
rginda87b86462011-12-14 13:48:03 -0800958 * Set the VT scroll region.
959 *
rginda87b86462011-12-14 13:48:03 -0800960 * This also resets the cursor position to the absolute (0, 0) position, since
961 * that's what xterm appears to do.
962 *
963 * @param {integer} scrollTop The zero-based top of the scroll region.
964 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
965 * inclusive.
966 */
967hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
968 this.vtScrollTop_ = scrollTop;
969 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800970};
971
972/**
rginda8ba33642011-12-14 12:31:31 -0800973 * Return the top row index according to the VT.
974 *
975 * This will return 0 unless the terminal has been told to restrict scrolling
976 * to some lower row. It is used for some VT cursor positioning and scrolling
977 * commands.
978 *
979 * @return {integer} The topmost row in the terminal's scroll region.
980 */
981hterm.Terminal.prototype.getVTScrollTop = function() {
982 if (this.vtScrollTop_ != null)
983 return this.vtScrollTop_;
984
985 return 0;
rginda87b86462011-12-14 13:48:03 -0800986};
rginda8ba33642011-12-14 12:31:31 -0800987
988/**
989 * Return the bottom row index according to the VT.
990 *
991 * This will return the height of the terminal unless the it has been told to
992 * restrict scrolling to some higher row. It is used for some VT cursor
993 * positioning and scrolling commands.
994 *
995 * @return {integer} The bottommost row in the terminal's scroll region.
996 */
997hterm.Terminal.prototype.getVTScrollBottom = function() {
998 if (this.vtScrollBottom_ != null)
999 return this.vtScrollBottom_;
1000
rginda87b86462011-12-14 13:48:03 -08001001 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001002}
1003
1004/**
1005 * Process a '\n' character.
1006 *
1007 * If the cursor is on the final row of the terminal this will append a new
1008 * blank row to the screen and scroll the topmost row into the scrollback
1009 * buffer.
1010 *
1011 * Otherwise, this moves the cursor to column zero of the next row.
1012 */
1013hterm.Terminal.prototype.newLine = function() {
1014 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001015 // If we're at the end of the screen we need to append a new line and
1016 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001017 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001018 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1019 // End of the scroll region does not affect the scrollback buffer.
1020 this.vtScrollUp(1);
1021 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001022 } else {
rginda87b86462011-12-14 13:48:03 -08001023 // Anywhere else in the screen just moves the cursor.
1024 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001025 }
1026};
1027
1028/**
1029 * Like newLine(), except maintain the cursor column.
1030 */
1031hterm.Terminal.prototype.lineFeed = function() {
1032 var column = this.screen_.cursorPosition.column;
1033 this.newLine();
1034 this.setCursorColumn(column);
1035};
1036
1037/**
rginda87b86462011-12-14 13:48:03 -08001038 * If autoCarriageReturn is set then newLine(), else lineFeed().
1039 */
1040hterm.Terminal.prototype.formFeed = function() {
1041 if (this.options_.autoCarriageReturn) {
1042 this.newLine();
1043 } else {
1044 this.lineFeed();
1045 }
1046};
1047
1048/**
1049 * Move the cursor up one row, possibly inserting a blank line.
1050 *
1051 * The cursor column is not changed.
1052 */
1053hterm.Terminal.prototype.reverseLineFeed = function() {
1054 var scrollTop = this.getVTScrollTop();
1055 var currentRow = this.screen_.cursorPosition.row;
1056
1057 if (currentRow == scrollTop) {
1058 this.insertLines(1);
1059 } else {
1060 this.setAbsoluteCursorRow(currentRow - 1);
1061 }
1062};
1063
1064/**
rginda8ba33642011-12-14 12:31:31 -08001065 * Replace all characters to the left of the current cursor with the space
1066 * character.
1067 *
1068 * TODO(rginda): This should probably *remove* the characters (not just replace
1069 * with a space) if there are no characters at or beyond the current cursor
1070 * position. Once it does that, it'll have the same text-attribute related
1071 * issues as hterm.Screen.prototype.clearCursorRow :/
1072 */
1073hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001074 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001075 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001076 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1077 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001078};
1079
1080/**
1081 * Erase a given number of characters to the right of the cursor, shifting
1082 * remaining characters to the left.
1083 *
1084 * The cursor position is unchanged.
1085 *
1086 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1087 * the end of the text.
1088 *
1089 * TODO(rginda): This likely has text-attribute related troubles similar to the
1090 * todo on hterm.Screen.prototype.clearCursorRow.
1091 */
1092hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001093 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001094
rginda87b86462011-12-14 13:48:03 -08001095 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001096 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1097 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001098 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001099};
1100
1101/**
1102 * Erase the current line.
1103 *
1104 * The cursor position is unchanged.
1105 *
1106 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1107 * has a text-attribute related TODO.
1108 */
1109hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001110 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001111 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001112 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001113};
1114
1115/**
1116 * Erase all characters from the start of the scroll region to the current
1117 * cursor position.
1118 *
1119 * The cursor position is unchanged.
1120 *
1121 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1122 * has a text-attribute related TODO.
1123 */
1124hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001125 var cursor = this.saveCursor();
1126
1127 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001128
1129 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001130 for (var i = top; i < cursor.row; i++) {
1131 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001132 this.screen_.clearCursorRow();
1133 }
1134
rginda87b86462011-12-14 13:48:03 -08001135 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001136};
1137
1138/**
1139 * Erase all characters from the current cursor position to the end of the
1140 * scroll region.
1141 *
1142 * The cursor position is unchanged.
1143 *
1144 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1145 * has a text-attribute related TODO.
1146 */
1147hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001148 var cursor = this.saveCursor();
1149
1150 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001151
1152 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001153 for (var i = cursor.row + 1; i <= bottom; i++) {
1154 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001155 this.screen_.clearCursorRow();
1156 }
1157
rginda87b86462011-12-14 13:48:03 -08001158 this.restoreCursor(cursor);
1159};
1160
1161/**
1162 * Fill the terminal with a given character.
1163 *
1164 * This methods does not respect the VT scroll region.
1165 *
1166 * @param {string} ch The character to use for the fill.
1167 */
1168hterm.Terminal.prototype.fill = function(ch) {
1169 var cursor = this.saveCursor();
1170
1171 this.setAbsoluteCursorPosition(0, 0);
1172 for (var row = 0; row < this.screenSize.height; row++) {
1173 for (var col = 0; col < this.screenSize.width; col++) {
1174 this.setAbsoluteCursorPosition(row, col);
1175 this.screen_.overwriteString(ch);
1176 }
1177 }
1178
1179 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001180};
1181
1182/**
rginda9ea433c2012-03-16 11:57:00 -07001183 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001184 *
rginda9ea433c2012-03-16 11:57:00 -07001185 * This does not respect the scroll region.
1186 *
1187 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1188 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001189 *
1190 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1191 * has a text-attribute related TODO.
1192 */
rginda9ea433c2012-03-16 11:57:00 -07001193hterm.Terminal.prototype.clearHome = function(opt_screen) {
1194 var screen = opt_screen || this.screen_;
1195 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001196
rgindae4d29232012-01-19 10:47:13 -08001197 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001198 screen.setCursorPosition(i, 0);
1199 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001200 }
1201
rginda9ea433c2012-03-16 11:57:00 -07001202 screen.setCursorPosition(0, 0);
1203};
1204
1205/**
1206 * Erase the entire display without changing the cursor position.
1207 *
1208 * The cursor position is unchanged. This does not respect the scroll
1209 * region.
1210 *
1211 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1212 * to the current screen.
1213 *
1214 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1215 * has a text-attribute related TODO.
1216 */
1217hterm.Terminal.prototype.clear = function(opt_screen) {
1218 var screen = opt_screen || this.screen_;
1219 var cursor = screen.cursorPosition.clone();
1220 this.clearHome(screen);
1221 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001222};
1223
1224/**
1225 * VT command to insert lines at the current cursor row.
1226 *
1227 * This respects the current scroll region. Rows pushed off the bottom are
1228 * lost (they won't show up in the scrollback buffer).
1229 *
1230 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1231 * has a text-attribute related TODO.
1232 *
1233 * @param {integer} count The number of lines to insert.
1234 */
1235hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001236 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001237
1238 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001239 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001240
rgindae4d29232012-01-19 10:47:13 -08001241 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001242 if (start != cursor.row)
1243 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001244
1245 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001246 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001247 this.screen_.clearCursorRow();
1248 }
1249
rginda87b86462011-12-14 13:48:03 -08001250 cursor.column = 0;
1251 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001252};
1253
1254/**
1255 * VT command to delete lines at the current cursor row.
1256 *
1257 * New rows are added to the bottom of scroll region to take their place. New
1258 * rows are strictly there to take up space and have no content or style.
1259 */
1260hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001261 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001262
rginda87b86462011-12-14 13:48:03 -08001263 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001264 var bottom = this.getVTScrollBottom();
1265
rginda87b86462011-12-14 13:48:03 -08001266 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001267 count = Math.min(count, maxCount);
1268
rginda87b86462011-12-14 13:48:03 -08001269 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001270 if (count != maxCount)
1271 this.moveRows_(top, count, moveStart);
1272
1273 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001274 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001275 this.screen_.clearCursorRow();
1276 }
1277
rginda87b86462011-12-14 13:48:03 -08001278 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001279};
1280
1281/**
1282 * Inserts the given number of spaces at the current cursor position.
1283 *
rginda87b86462011-12-14 13:48:03 -08001284 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001285 */
1286hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001287 var cursor = this.saveCursor();
1288
rginda0f5c0292012-01-13 11:00:13 -08001289 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001290 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001291 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001292
1293 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001294};
1295
1296/**
1297 * Forward-delete the specified number of characters starting at the cursor
1298 * position.
1299 *
1300 * @param {integer} count The number of characters to delete.
1301 */
1302hterm.Terminal.prototype.deleteChars = function(count) {
1303 this.screen_.deleteChars(count);
1304};
1305
1306/**
1307 * Shift rows in the scroll region upwards by a given number of lines.
1308 *
1309 * New rows are inserted at the bottom of the scroll region to fill the
1310 * vacated rows. The new rows not filled out with the current text attributes.
1311 *
1312 * This function does not affect the scrollback rows at all. Rows shifted
1313 * off the top are lost.
1314 *
rginda87b86462011-12-14 13:48:03 -08001315 * The cursor position is not altered.
1316 *
rginda8ba33642011-12-14 12:31:31 -08001317 * @param {integer} count The number of rows to scroll.
1318 */
1319hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001320 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001321
rginda87b86462011-12-14 13:48:03 -08001322 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001323 this.deleteLines(count);
1324
rginda87b86462011-12-14 13:48:03 -08001325 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001326};
1327
1328/**
1329 * Shift rows below the cursor down by a given number of lines.
1330 *
1331 * This function respects the current scroll region.
1332 *
1333 * New rows are inserted at the top of the scroll region to fill the
1334 * vacated rows. The new rows not filled out with the current text attributes.
1335 *
1336 * This function does not affect the scrollback rows at all. Rows shifted
1337 * off the bottom are lost.
1338 *
1339 * @param {integer} count The number of rows to scroll.
1340 */
1341hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001342 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001343
rginda87b86462011-12-14 13:48:03 -08001344 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001345 this.insertLines(opt_count);
1346
rginda87b86462011-12-14 13:48:03 -08001347 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001348};
1349
rginda87b86462011-12-14 13:48:03 -08001350
rginda8ba33642011-12-14 12:31:31 -08001351/**
1352 * Set the cursor position.
1353 *
1354 * The cursor row is relative to the scroll region if the terminal has
1355 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1356 *
1357 * @param {integer} row The new zero-based cursor row.
1358 * @param {integer} row The new zero-based cursor column.
1359 */
1360hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1361 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001362 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001363 } else {
rginda87b86462011-12-14 13:48:03 -08001364 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001365 }
rginda87b86462011-12-14 13:48:03 -08001366};
rginda8ba33642011-12-14 12:31:31 -08001367
rginda87b86462011-12-14 13:48:03 -08001368hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1369 var scrollTop = this.getVTScrollTop();
1370 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001371 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001372 this.screen_.setCursorPosition(row, column);
1373};
1374
1375hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001376 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1377 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001378 this.screen_.setCursorPosition(row, column);
1379};
1380
1381/**
1382 * Set the cursor column.
1383 *
1384 * @param {integer} column The new zero-based cursor column.
1385 */
1386hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001387 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001388};
1389
1390/**
1391 * Return the cursor column.
1392 *
1393 * @return {integer} The zero-based cursor column.
1394 */
1395hterm.Terminal.prototype.getCursorColumn = function() {
1396 return this.screen_.cursorPosition.column;
1397};
1398
1399/**
1400 * Set the cursor row.
1401 *
1402 * The cursor row is relative to the scroll region if the terminal has
1403 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1404 *
1405 * @param {integer} row The new cursor row.
1406 */
rginda87b86462011-12-14 13:48:03 -08001407hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1408 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001409};
1410
1411/**
1412 * Return the cursor row.
1413 *
1414 * @return {integer} The zero-based cursor row.
1415 */
1416hterm.Terminal.prototype.getCursorRow = function(row) {
1417 return this.screen_.cursorPosition.row;
1418};
1419
1420/**
1421 * Request that the ScrollPort redraw itself soon.
1422 *
1423 * The redraw will happen asynchronously, soon after the call stack winds down.
1424 * Multiple calls will be coalesced into a single redraw.
1425 */
1426hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001427 if (this.timeouts_.redraw)
1428 return;
rginda8ba33642011-12-14 12:31:31 -08001429
1430 var self = this;
rginda87b86462011-12-14 13:48:03 -08001431 this.timeouts_.redraw = setTimeout(function() {
1432 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001433 self.scrollPort_.redraw_();
1434 }, 0);
1435};
1436
1437/**
1438 * Request that the ScrollPort be scrolled to the bottom.
1439 *
1440 * The scroll will happen asynchronously, soon after the call stack winds down.
1441 * Multiple calls will be coalesced into a single scroll.
1442 *
1443 * This affects the scrollbar position of the ScrollPort, and has nothing to
1444 * do with the VT scroll commands.
1445 */
1446hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1447 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001448 return;
rginda8ba33642011-12-14 12:31:31 -08001449
1450 var self = this;
1451 this.timeouts_.scrollDown = setTimeout(function() {
1452 delete self.timeouts_.scrollDown;
1453 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1454 }, 10);
1455};
1456
1457/**
1458 * Move the cursor up a specified number of rows.
1459 *
1460 * @param {integer} count The number of rows to move the cursor.
1461 */
1462hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001463 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001464};
1465
1466/**
1467 * Move the cursor down a specified number of rows.
1468 *
1469 * @param {integer} count The number of rows to move the cursor.
1470 */
1471hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001472 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001473 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1474 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1475 this.screenSize.height - 1);
1476
1477 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1478 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001479 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001480};
1481
1482/**
1483 * Move the cursor left a specified number of columns.
1484 *
1485 * @param {integer} count The number of columns to move the cursor.
1486 */
1487hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001488 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001489};
1490
1491/**
1492 * Move the cursor right a specified number of columns.
1493 *
1494 * @param {integer} count The number of columns to move the cursor.
1495 */
1496hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001497 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001498 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001499 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001500 this.setCursorColumn(column);
1501};
1502
1503/**
1504 * Reverse the foreground and background colors of the terminal.
1505 *
1506 * This only affects text that was drawn with no attributes.
1507 *
1508 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1509 * been drawn with attributes that happen to coincide with the default
1510 * 'no-attribute' colors. My guess is probably not.
1511 */
1512hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001513 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001514 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001515 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1516 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001517 } else {
rginda9f5222b2012-03-05 11:53:28 -08001518 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1519 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001520 }
1521};
1522
1523/**
rginda87b86462011-12-14 13:48:03 -08001524 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001525 */
1526hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001527 if (this.bellAudio_.getAttribute('src'))
1528 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001529
rginda6d397402012-01-17 10:58:29 -08001530 this.cursorNode_.style.backgroundColor =
1531 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001532
1533 var self = this;
1534 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001535 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001536 }, 200);
rginda87b86462011-12-14 13:48:03 -08001537};
1538
1539/**
rginda8ba33642011-12-14 12:31:31 -08001540 * Set the origin mode bit.
1541 *
1542 * If origin mode is on, certain VT cursor and scrolling commands measure their
1543 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1544 * to the top of the addressable screen.
1545 *
1546 * Defaults to off.
1547 *
1548 * @param {boolean} state True to set origin mode, false to unset.
1549 */
1550hterm.Terminal.prototype.setOriginMode = function(state) {
1551 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001552 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001553};
1554
1555/**
1556 * Set the insert mode bit.
1557 *
1558 * If insert mode is on, existing text beyond the cursor position will be
1559 * shifted right to make room for new text. Otherwise, new text overwrites
1560 * any existing text.
1561 *
1562 * Defaults to off.
1563 *
1564 * @param {boolean} state True to set insert mode, false to unset.
1565 */
1566hterm.Terminal.prototype.setInsertMode = function(state) {
1567 this.options_.insertMode = state;
1568};
1569
1570/**
rginda87b86462011-12-14 13:48:03 -08001571 * Set the auto carriage return bit.
1572 *
1573 * If auto carriage return is on then a formfeed character is interpreted
1574 * as a newline, otherwise it's the same as a linefeed. The difference boils
1575 * down to whether or not the cursor column is reset.
1576 */
1577hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1578 this.options_.autoCarriageReturn = state;
1579};
1580
1581/**
rginda8ba33642011-12-14 12:31:31 -08001582 * Set the wraparound mode bit.
1583 *
1584 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1585 * to the start of the following row. Otherwise, the cursor is clamped to the
1586 * end of the screen and attempts to write past it are ignored.
1587 *
1588 * Defaults to on.
1589 *
1590 * @param {boolean} state True to set wraparound mode, false to unset.
1591 */
1592hterm.Terminal.prototype.setWraparound = function(state) {
1593 this.options_.wraparound = state;
1594};
1595
1596/**
1597 * Set the reverse-wraparound mode bit.
1598 *
1599 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1600 * to the end of the previous row. Otherwise, the cursor is clamped to column
1601 * 0.
1602 *
1603 * Defaults to off.
1604 *
1605 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1606 */
1607hterm.Terminal.prototype.setReverseWraparound = function(state) {
1608 this.options_.reverseWraparound = state;
1609};
1610
1611/**
1612 * Selects between the primary and alternate screens.
1613 *
1614 * If alternate mode is on, the alternate screen is active. Otherwise the
1615 * primary screen is active.
1616 *
1617 * Swapping screens has no effect on the scrollback buffer.
1618 *
1619 * Each screen maintains its own cursor position.
1620 *
1621 * Defaults to off.
1622 *
1623 * @param {boolean} state True to set alternate mode, false to unset.
1624 */
1625hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001626 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001627 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1628
rginda35c456b2012-02-09 17:29:05 -08001629 if (this.screen_.rowsArray.length &&
1630 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1631 // If the screen changed sizes while we were away, our rowIndexes may
1632 // be incorrect.
1633 var offset = this.scrollbackRows_.length;
1634 var ary = this.screen_.rowsArray;
1635 for (i = 0; i < ary.length; i++) {
1636 ary[i].rowIndex = offset + i;
1637 }
1638 }
rginda8ba33642011-12-14 12:31:31 -08001639
rginda35c456b2012-02-09 17:29:05 -08001640 this.realizeWidth_(this.screenSize.width);
1641 this.realizeHeight_(this.screenSize.height);
1642 this.scrollPort_.syncScrollHeight();
1643 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001644
rginda6d397402012-01-17 10:58:29 -08001645 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001646 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001647};
1648
1649/**
1650 * Set the cursor-blink mode bit.
1651 *
1652 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1653 * a visible cursor does not blink.
1654 *
1655 * You should make sure to turn blinking off if you're going to dispose of a
1656 * terminal, otherwise you'll leak a timeout.
1657 *
1658 * Defaults to on.
1659 *
1660 * @param {boolean} state True to set cursor-blink mode, false to unset.
1661 */
1662hterm.Terminal.prototype.setCursorBlink = function(state) {
1663 this.options_.cursorBlink = state;
1664
1665 if (!state && this.timeouts_.cursorBlink) {
1666 clearTimeout(this.timeouts_.cursorBlink);
1667 delete this.timeouts_.cursorBlink;
1668 }
1669
1670 if (this.options_.cursorVisible)
1671 this.setCursorVisible(true);
1672};
1673
1674/**
1675 * Set the cursor-visible mode bit.
1676 *
1677 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1678 *
1679 * Defaults to on.
1680 *
1681 * @param {boolean} state True to set cursor-visible mode, false to unset.
1682 */
1683hterm.Terminal.prototype.setCursorVisible = function(state) {
1684 this.options_.cursorVisible = state;
1685
1686 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001687 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001688 return;
1689 }
1690
rginda87b86462011-12-14 13:48:03 -08001691 this.syncCursorPosition_();
1692
1693 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001694
1695 if (this.options_.cursorBlink) {
1696 if (this.timeouts_.cursorBlink)
1697 return;
1698
1699 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1700 500);
1701 } else {
1702 if (this.timeouts_.cursorBlink) {
1703 clearTimeout(this.timeouts_.cursorBlink);
1704 delete this.timeouts_.cursorBlink;
1705 }
1706 }
1707};
1708
1709/**
rginda87b86462011-12-14 13:48:03 -08001710 * Synchronizes the visible cursor and document selection with the current
1711 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001712 */
1713hterm.Terminal.prototype.syncCursorPosition_ = function() {
1714 var topRowIndex = this.scrollPort_.getTopRowIndex();
1715 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1716 var cursorRowIndex = this.scrollbackRows_.length +
1717 this.screen_.cursorPosition.row;
1718
1719 if (cursorRowIndex > bottomRowIndex) {
1720 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001721 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001722 return;
1723 }
1724
rginda35c456b2012-02-09 17:29:05 -08001725 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1726 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1727
rginda8ba33642011-12-14 12:31:31 -08001728 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001729 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1730 'px';
1731 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1732 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001733
1734 this.cursorNode_.setAttribute('title',
1735 '(' + this.screen_.cursorPosition.row +
1736 ', ' + this.screen_.cursorPosition.column +
1737 ')');
1738
1739 // Update the caret for a11y purposes.
1740 var selection = this.document_.getSelection();
1741 if (selection && selection.isCollapsed)
1742 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001743};
1744
1745/**
1746 * Synchronizes the visible cursor with the current cursor coordinates.
1747 *
1748 * The sync will happen asynchronously, soon after the call stack winds down.
1749 * Multiple calls will be coalesced into a single sync.
1750 */
1751hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1752 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001753 return;
rginda8ba33642011-12-14 12:31:31 -08001754
1755 var self = this;
1756 this.timeouts_.syncCursor = setTimeout(function() {
1757 self.syncCursorPosition_();
1758 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001759 }, 0);
1760};
1761
rgindacc2996c2012-02-24 14:59:31 -08001762/**
1763 * Show the terminal overlay for a given amount of time.
1764 *
1765 * The terminal overlay appears in inverse video in a large font, centered
1766 * over the terminal. You should probably keep the overlay message brief,
1767 * since it's in a large font and you probably aren't going to check the size
1768 * of the terminal first.
1769 *
1770 * @param {string} msg The text (not HTML) message to display in the overlay.
1771 * @param {number} opt_timeout The amount of time to wait before fading out
1772 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1773 * stay up forever (or until the next overlay).
1774 */
1775hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001776 if (!this.overlayNode_) {
1777 if (!this.div_)
1778 return;
1779
1780 this.overlayNode_ = this.document_.createElement('div');
1781 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001782 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001783 'font-size: xx-large;' +
1784 'opacity: 0.75;' +
1785 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1786 'position: absolute;' +
1787 '-webkit-user-select: none;' +
1788 '-webkit-transition: opacity 180ms ease-in;');
1789 }
1790
rginda9f5222b2012-03-05 11:53:28 -08001791 this.overlayNode_.style.color = this.prefs_.get('background-color');
1792 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1793 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1794
rgindaf0090c92012-02-10 14:58:52 -08001795 this.overlayNode_.textContent = msg;
1796 this.overlayNode_.style.opacity = '0.75';
1797
1798 if (!this.overlayNode_.parentNode)
1799 this.div_.appendChild(this.overlayNode_);
1800
1801 this.overlayNode_.style.top = (
1802 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1803 this.overlayNode_.style.left = (
1804 this.div_.clientWidth - this.overlayNode_.clientWidth -
1805 this.scrollbarWidthPx) / 2;
1806
1807 var self = this;
1808
1809 if (this.overlayTimeout_)
1810 clearTimeout(this.overlayTimeout_);
1811
rgindacc2996c2012-02-24 14:59:31 -08001812 if (opt_timeout === null)
1813 return;
1814
rgindaf0090c92012-02-10 14:58:52 -08001815 this.overlayTimeout_ = setTimeout(function() {
1816 self.overlayNode_.style.opacity = '0';
1817 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001818 if (self.overlayNode_.parentNode)
1819 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001820 self.overlayTimeout_ = null;
1821 self.overlayNode_.style.opacity = '0.75';
1822 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001823 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001824};
1825
1826hterm.Terminal.prototype.overlaySize = function() {
1827 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1828};
1829
rginda87b86462011-12-14 13:48:03 -08001830/**
1831 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1832 *
1833 * @param {string} string The VT string representing the keystroke.
1834 */
1835hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001836 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001837 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1838
1839 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001840};
1841
1842/**
1843 * React when the ScrollPort is scrolled.
1844 */
1845hterm.Terminal.prototype.onScroll_ = function() {
1846 this.scheduleSyncCursorPosition_();
1847};
1848
1849/**
rginda9846e2f2012-01-27 13:53:33 -08001850 * React when text is pasted into the scrollPort.
1851 */
1852hterm.Terminal.prototype.onPaste_ = function(e) {
1853 this.io.onVTKeystroke(e.text);
1854};
1855
1856/**
rginda8ba33642011-12-14 12:31:31 -08001857 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001858 *
1859 * Note: This function should not directly contain code that alters the internal
1860 * state of the terminal. That kind of code belongs in realizeWidth or
1861 * realizeHeight, so that it can be executed synchronously in the case of a
1862 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001863 */
1864hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001865 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001866 this.scrollPort_.characterSize.width);
1867 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1868 this.scrollPort_.characterSize.height);
1869
1870 if (!(columnCount || rowCount)) {
1871 // We avoid these situations since they happen sometimes when the terminal
1872 // gets removed from the document, and we can't deal with that.
1873 return;
1874 }
1875
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001876 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001877 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001878 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001879};
1880
1881/**
1882 * Service the cursor blink timeout.
1883 */
1884hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001885 if (this.cursorNode_.style.opacity == '0') {
1886 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001887 } else {
rginda87b86462011-12-14 13:48:03 -08001888 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001889 }
1890};
David Reveman8f552492012-03-28 12:18:41 -04001891
1892/**
1893 * Set the scrollbar-visible mode bit.
1894 *
1895 * If scrollbar-visible is on, the vertical scrollbar will be visible.
1896 * Otherwise it will not.
1897 *
1898 * Defaults to on.
1899 *
1900 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
1901 */
1902hterm.Terminal.prototype.setScrollbarVisible = function(state) {
1903 this.scrollPort_.setScrollbarVisible(state);
1904};