blob: bed8fb7131ea33057fb2dc05055cd092d7829047 [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);
rgindaf7521392012-02-28 17:20:34 -0800712
rginda9f5222b2012-03-05 11:53:28 -0800713 this.setFontSize(this.prefs_.get('font-size'));
714 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800715
David Reveman8f552492012-03-28 12:18:41 -0400716 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
717
rginda8ba33642011-12-14 12:31:31 -0800718 this.document_ = this.scrollPort_.getDocument();
719
rginda8ba33642011-12-14 12:31:31 -0800720 this.cursorNode_ = this.document_.createElement('div');
721 this.cursorNode_.style.cssText =
722 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800723 'top: -99px;' +
724 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800725 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
726 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800727 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800728 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800729 this.document_.body.appendChild(this.cursorNode_);
730
731 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800732
rginda87b86462011-12-14 13:48:03 -0800733 var link = this.document_.createElement('link');
734 link.setAttribute('href', '../css/dialogs.css');
735 link.setAttribute('rel', 'stylesheet');
736 this.document_.head.appendChild(link);
737
738 this.alertDialog = new AlertDialog(this.document_.body);
739 this.promptDialog = new PromptDialog(this.document_.body);
740 this.confirmDialog = new ConfirmDialog(this.document_.body);
741
742 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800743 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800744};
745
746hterm.Terminal.prototype.getDocument = function() {
747 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800748};
749
750/**
751 * Return the HTML Element for a given row index.
752 *
753 * This is a method from the RowProvider interface. The ScrollPort uses
754 * it to fetch rows on demand as they are scrolled into view.
755 *
756 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
757 * pairs to conserve memory.
758 *
759 * @param {integer} index The zero-based row index, measured relative to the
760 * start of the scrollback buffer. On-screen rows will always have the
761 * largest indicies.
762 * @return {HTMLElement} The 'x-row' element containing for the requested row.
763 */
764hterm.Terminal.prototype.getRowNode = function(index) {
765 if (index < this.scrollbackRows_.length)
766 return this.scrollbackRows_[index];
767
768 var screenIndex = index - this.scrollbackRows_.length;
769 return this.screen_.rowsArray[screenIndex];
770};
771
772/**
773 * Return the text content for a given range of rows.
774 *
775 * This is a method from the RowProvider interface. The ScrollPort uses
776 * it to fetch text content on demand when the user attempts to copy their
777 * selection to the clipboard.
778 *
779 * @param {integer} start The zero-based row index to start from, measured
780 * relative to the start of the scrollback buffer. On-screen rows will
781 * always have the largest indicies.
782 * @param {integer} end The zero-based row index to end on, measured
783 * relative to the start of the scrollback buffer.
784 * @return {string} A single string containing the text value of the range of
785 * rows. Lines will be newline delimited, with no trailing newline.
786 */
787hterm.Terminal.prototype.getRowsText = function(start, end) {
788 var ary = [];
789 for (var i = start; i < end; i++) {
790 var node = this.getRowNode(i);
791 ary.push(node.textContent);
792 }
793
794 return ary.join('\n');
795};
796
797/**
798 * Return the text content for a given row.
799 *
800 * This is a method from the RowProvider interface. The ScrollPort uses
801 * it to fetch text content on demand when the user attempts to copy their
802 * selection to the clipboard.
803 *
804 * @param {integer} index The zero-based row index to return, measured
805 * relative to the start of the scrollback buffer. On-screen rows will
806 * always have the largest indicies.
807 * @return {string} A string containing the text value of the selected row.
808 */
809hterm.Terminal.prototype.getRowText = function(index) {
810 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800811 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800812};
813
814/**
815 * Return the total number of rows in the addressable screen and in the
816 * scrollback buffer of this terminal.
817 *
818 * This is a method from the RowProvider interface. The ScrollPort uses
819 * it to compute the size of the scrollbar.
820 *
821 * @return {integer} The number of rows in this terminal.
822 */
823hterm.Terminal.prototype.getRowCount = function() {
824 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
825};
826
827/**
828 * Create DOM nodes for new rows and append them to the end of the terminal.
829 *
830 * This is the only correct way to add a new DOM node for a row. Notice that
831 * the new row is appended to the bottom of the list of rows, and does not
832 * require renumbering (of the rowIndex property) of previous rows.
833 *
834 * If you think you want a new blank row somewhere in the middle of the
835 * terminal, look into moveRows_().
836 *
837 * This method does not pay attention to vtScrollTop/Bottom, since you should
838 * be using moveRows() in cases where they would matter.
839 *
840 * The cursor will be positioned at column 0 of the first inserted line.
841 */
842hterm.Terminal.prototype.appendRows_ = function(count) {
843 var cursorRow = this.screen_.rowsArray.length;
844 var offset = this.scrollbackRows_.length + cursorRow;
845 for (var i = 0; i < count; i++) {
846 var row = this.document_.createElement('x-row');
847 row.appendChild(this.document_.createTextNode(''));
848 row.rowIndex = offset + i;
849 this.screen_.pushRow(row);
850 }
851
852 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
853 if (extraRows > 0) {
854 var ary = this.screen_.shiftRows(extraRows);
855 Array.prototype.push.apply(this.scrollbackRows_, ary);
856 this.scheduleScrollDown_();
857 }
858
859 if (cursorRow >= this.screen_.rowsArray.length)
860 cursorRow = this.screen_.rowsArray.length - 1;
861
rginda87b86462011-12-14 13:48:03 -0800862 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800863};
864
865/**
866 * Relocate rows from one part of the addressable screen to another.
867 *
868 * This is used to recycle rows during VT scrolls (those which are driven
869 * by VT commands, rather than by the user manipulating the scrollbar.)
870 *
871 * In this case, the blank lines scrolled into the scroll region are made of
872 * the nodes we scrolled off. These have their rowIndex properties carefully
873 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800874 */
875hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
876 var ary = this.screen_.removeRows(fromIndex, count);
877 this.screen_.insertRows(toIndex, ary);
878
879 var start, end;
880 if (fromIndex < toIndex) {
881 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800882 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800883 } else {
884 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800885 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800886 }
887
888 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800889 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800890};
891
892/**
893 * Renumber the rowIndex property of the given range of rows.
894 *
895 * The start and end indicies are relative to the screen, not the scrollback.
896 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800897 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800898 * no need to renumber scrollback rows.
899 */
900hterm.Terminal.prototype.renumberRows_ = function(start, end) {
901 var offset = this.scrollbackRows_.length;
902 for (var i = start; i < end; i++) {
903 this.screen_.rowsArray[i].rowIndex = offset + i;
904 }
905};
906
907/**
908 * Print a string to the terminal.
909 *
910 * This respects the current insert and wraparound modes. It will add new lines
911 * to the end of the terminal, scrolling off the top into the scrollback buffer
912 * if necessary.
913 *
914 * The string is *not* parsed for escape codes. Use the interpret() method if
915 * that's what you're after.
916 *
917 * @param{string} str The string to print.
918 */
919hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800920 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
921 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800922
rgindaa19afe22012-01-25 15:40:22 -0800923 if (this.options_.insertMode) {
924 this.screen_.insertString(str);
925 } else {
926 this.screen_.overwriteString(str);
927 }
928
929 var overflow = this.screen_.maybeClipCurrentRow();
930
931 if (this.options_.wraparound && overflow) {
932 var lastColumn;
933
934 do {
rginda35c456b2012-02-09 17:29:05 -0800935 this.newLine();
936 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800937
938 if (!this.options_.insertMode)
939 this.screen_.deleteChars(overflow.characterLength);
940
941 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800942
943 overflow = this.screen_.maybeClipCurrentRow();
944 } while (overflow);
945
946 this.setCursorColumn(lastColumn);
947 }
rginda8ba33642011-12-14 12:31:31 -0800948
949 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800950
rginda9f5222b2012-03-05 11:53:28 -0800951 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -0800952 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800953};
954
955/**
rginda87b86462011-12-14 13:48:03 -0800956 * Set the VT scroll region.
957 *
rginda87b86462011-12-14 13:48:03 -0800958 * This also resets the cursor position to the absolute (0, 0) position, since
959 * that's what xterm appears to do.
960 *
961 * @param {integer} scrollTop The zero-based top of the scroll region.
962 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
963 * inclusive.
964 */
965hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
966 this.vtScrollTop_ = scrollTop;
967 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800968};
969
970/**
rginda8ba33642011-12-14 12:31:31 -0800971 * Return the top row index according to the VT.
972 *
973 * This will return 0 unless the terminal has been told to restrict scrolling
974 * to some lower row. It is used for some VT cursor positioning and scrolling
975 * commands.
976 *
977 * @return {integer} The topmost row in the terminal's scroll region.
978 */
979hterm.Terminal.prototype.getVTScrollTop = function() {
980 if (this.vtScrollTop_ != null)
981 return this.vtScrollTop_;
982
983 return 0;
rginda87b86462011-12-14 13:48:03 -0800984};
rginda8ba33642011-12-14 12:31:31 -0800985
986/**
987 * Return the bottom row index according to the VT.
988 *
989 * This will return the height of the terminal unless the it has been told to
990 * restrict scrolling to some higher row. It is used for some VT cursor
991 * positioning and scrolling commands.
992 *
993 * @return {integer} The bottommost row in the terminal's scroll region.
994 */
995hterm.Terminal.prototype.getVTScrollBottom = function() {
996 if (this.vtScrollBottom_ != null)
997 return this.vtScrollBottom_;
998
rginda87b86462011-12-14 13:48:03 -0800999 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001000}
1001
1002/**
1003 * Process a '\n' character.
1004 *
1005 * If the cursor is on the final row of the terminal this will append a new
1006 * blank row to the screen and scroll the topmost row into the scrollback
1007 * buffer.
1008 *
1009 * Otherwise, this moves the cursor to column zero of the next row.
1010 */
1011hterm.Terminal.prototype.newLine = function() {
1012 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001013 // If we're at the end of the screen we need to append a new line and
1014 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001015 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001016 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1017 // End of the scroll region does not affect the scrollback buffer.
1018 this.vtScrollUp(1);
1019 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001020 } else {
rginda87b86462011-12-14 13:48:03 -08001021 // Anywhere else in the screen just moves the cursor.
1022 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001023 }
1024};
1025
1026/**
1027 * Like newLine(), except maintain the cursor column.
1028 */
1029hterm.Terminal.prototype.lineFeed = function() {
1030 var column = this.screen_.cursorPosition.column;
1031 this.newLine();
1032 this.setCursorColumn(column);
1033};
1034
1035/**
rginda87b86462011-12-14 13:48:03 -08001036 * If autoCarriageReturn is set then newLine(), else lineFeed().
1037 */
1038hterm.Terminal.prototype.formFeed = function() {
1039 if (this.options_.autoCarriageReturn) {
1040 this.newLine();
1041 } else {
1042 this.lineFeed();
1043 }
1044};
1045
1046/**
1047 * Move the cursor up one row, possibly inserting a blank line.
1048 *
1049 * The cursor column is not changed.
1050 */
1051hterm.Terminal.prototype.reverseLineFeed = function() {
1052 var scrollTop = this.getVTScrollTop();
1053 var currentRow = this.screen_.cursorPosition.row;
1054
1055 if (currentRow == scrollTop) {
1056 this.insertLines(1);
1057 } else {
1058 this.setAbsoluteCursorRow(currentRow - 1);
1059 }
1060};
1061
1062/**
rginda8ba33642011-12-14 12:31:31 -08001063 * Replace all characters to the left of the current cursor with the space
1064 * character.
1065 *
1066 * TODO(rginda): This should probably *remove* the characters (not just replace
1067 * with a space) if there are no characters at or beyond the current cursor
1068 * position. Once it does that, it'll have the same text-attribute related
1069 * issues as hterm.Screen.prototype.clearCursorRow :/
1070 */
1071hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001072 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001073 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001074 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1075 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001076};
1077
1078/**
1079 * Erase a given number of characters to the right of the cursor, shifting
1080 * remaining characters to the left.
1081 *
1082 * The cursor position is unchanged.
1083 *
1084 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1085 * the end of the text.
1086 *
1087 * TODO(rginda): This likely has text-attribute related troubles similar to the
1088 * todo on hterm.Screen.prototype.clearCursorRow.
1089 */
1090hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001091 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001092
rginda87b86462011-12-14 13:48:03 -08001093 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001094 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1095 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001096 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001097};
1098
1099/**
1100 * Erase the current line.
1101 *
1102 * The cursor position is unchanged.
1103 *
1104 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1105 * has a text-attribute related TODO.
1106 */
1107hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001108 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001109 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001110 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001111};
1112
1113/**
1114 * Erase all characters from the start of the scroll region to the current
1115 * cursor position.
1116 *
1117 * The cursor position is unchanged.
1118 *
1119 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1120 * has a text-attribute related TODO.
1121 */
1122hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001123 var cursor = this.saveCursor();
1124
1125 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001126
1127 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001128 for (var i = top; i < cursor.row; i++) {
1129 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001130 this.screen_.clearCursorRow();
1131 }
1132
rginda87b86462011-12-14 13:48:03 -08001133 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001134};
1135
1136/**
1137 * Erase all characters from the current cursor position to the end of the
1138 * scroll region.
1139 *
1140 * The cursor position is unchanged.
1141 *
1142 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1143 * has a text-attribute related TODO.
1144 */
1145hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001146 var cursor = this.saveCursor();
1147
1148 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001149
1150 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001151 for (var i = cursor.row + 1; i <= bottom; i++) {
1152 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001153 this.screen_.clearCursorRow();
1154 }
1155
rginda87b86462011-12-14 13:48:03 -08001156 this.restoreCursor(cursor);
1157};
1158
1159/**
1160 * Fill the terminal with a given character.
1161 *
1162 * This methods does not respect the VT scroll region.
1163 *
1164 * @param {string} ch The character to use for the fill.
1165 */
1166hterm.Terminal.prototype.fill = function(ch) {
1167 var cursor = this.saveCursor();
1168
1169 this.setAbsoluteCursorPosition(0, 0);
1170 for (var row = 0; row < this.screenSize.height; row++) {
1171 for (var col = 0; col < this.screenSize.width; col++) {
1172 this.setAbsoluteCursorPosition(row, col);
1173 this.screen_.overwriteString(ch);
1174 }
1175 }
1176
1177 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001178};
1179
1180/**
rginda9ea433c2012-03-16 11:57:00 -07001181 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001182 *
rginda9ea433c2012-03-16 11:57:00 -07001183 * This does not respect the scroll region.
1184 *
1185 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1186 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001187 *
1188 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1189 * has a text-attribute related TODO.
1190 */
rginda9ea433c2012-03-16 11:57:00 -07001191hterm.Terminal.prototype.clearHome = function(opt_screen) {
1192 var screen = opt_screen || this.screen_;
1193 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001194
rgindae4d29232012-01-19 10:47:13 -08001195 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001196 screen.setCursorPosition(i, 0);
1197 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001198 }
1199
rginda9ea433c2012-03-16 11:57:00 -07001200 screen.setCursorPosition(0, 0);
1201};
1202
1203/**
1204 * Erase the entire display without changing the cursor position.
1205 *
1206 * The cursor position is unchanged. This does not respect the scroll
1207 * region.
1208 *
1209 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1210 * to the current screen.
1211 *
1212 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1213 * has a text-attribute related TODO.
1214 */
1215hterm.Terminal.prototype.clear = function(opt_screen) {
1216 var screen = opt_screen || this.screen_;
1217 var cursor = screen.cursorPosition.clone();
1218 this.clearHome(screen);
1219 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001220};
1221
1222/**
1223 * VT command to insert lines at the current cursor row.
1224 *
1225 * This respects the current scroll region. Rows pushed off the bottom are
1226 * lost (they won't show up in the scrollback buffer).
1227 *
1228 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1229 * has a text-attribute related TODO.
1230 *
1231 * @param {integer} count The number of lines to insert.
1232 */
1233hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001234 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001235
1236 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001237 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001238
rgindae4d29232012-01-19 10:47:13 -08001239 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001240 if (start != cursor.row)
1241 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001242
1243 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001244 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001245 this.screen_.clearCursorRow();
1246 }
1247
rginda87b86462011-12-14 13:48:03 -08001248 cursor.column = 0;
1249 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001250};
1251
1252/**
1253 * VT command to delete lines at the current cursor row.
1254 *
1255 * New rows are added to the bottom of scroll region to take their place. New
1256 * rows are strictly there to take up space and have no content or style.
1257 */
1258hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001259 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001260
rginda87b86462011-12-14 13:48:03 -08001261 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001262 var bottom = this.getVTScrollBottom();
1263
rginda87b86462011-12-14 13:48:03 -08001264 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001265 count = Math.min(count, maxCount);
1266
rginda87b86462011-12-14 13:48:03 -08001267 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001268 if (count != maxCount)
1269 this.moveRows_(top, count, moveStart);
1270
1271 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001272 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001273 this.screen_.clearCursorRow();
1274 }
1275
rginda87b86462011-12-14 13:48:03 -08001276 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001277};
1278
1279/**
1280 * Inserts the given number of spaces at the current cursor position.
1281 *
rginda87b86462011-12-14 13:48:03 -08001282 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001283 */
1284hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001285 var cursor = this.saveCursor();
1286
rginda0f5c0292012-01-13 11:00:13 -08001287 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001288 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001289 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001290
1291 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001292};
1293
1294/**
1295 * Forward-delete the specified number of characters starting at the cursor
1296 * position.
1297 *
1298 * @param {integer} count The number of characters to delete.
1299 */
1300hterm.Terminal.prototype.deleteChars = function(count) {
1301 this.screen_.deleteChars(count);
1302};
1303
1304/**
1305 * Shift rows in the scroll region upwards by a given number of lines.
1306 *
1307 * New rows are inserted at the bottom of the scroll region to fill the
1308 * vacated rows. The new rows not filled out with the current text attributes.
1309 *
1310 * This function does not affect the scrollback rows at all. Rows shifted
1311 * off the top are lost.
1312 *
rginda87b86462011-12-14 13:48:03 -08001313 * The cursor position is not altered.
1314 *
rginda8ba33642011-12-14 12:31:31 -08001315 * @param {integer} count The number of rows to scroll.
1316 */
1317hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001318 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001319
rginda87b86462011-12-14 13:48:03 -08001320 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001321 this.deleteLines(count);
1322
rginda87b86462011-12-14 13:48:03 -08001323 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001324};
1325
1326/**
1327 * Shift rows below the cursor down by a given number of lines.
1328 *
1329 * This function respects the current scroll region.
1330 *
1331 * New rows are inserted at the top of the scroll region to fill the
1332 * vacated rows. The new rows not filled out with the current text attributes.
1333 *
1334 * This function does not affect the scrollback rows at all. Rows shifted
1335 * off the bottom are lost.
1336 *
1337 * @param {integer} count The number of rows to scroll.
1338 */
1339hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001340 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001341
rginda87b86462011-12-14 13:48:03 -08001342 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001343 this.insertLines(opt_count);
1344
rginda87b86462011-12-14 13:48:03 -08001345 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001346};
1347
rginda87b86462011-12-14 13:48:03 -08001348
rginda8ba33642011-12-14 12:31:31 -08001349/**
1350 * Set the cursor position.
1351 *
1352 * The cursor row is relative to the scroll region if the terminal has
1353 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1354 *
1355 * @param {integer} row The new zero-based cursor row.
1356 * @param {integer} row The new zero-based cursor column.
1357 */
1358hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1359 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001360 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001361 } else {
rginda87b86462011-12-14 13:48:03 -08001362 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001363 }
rginda87b86462011-12-14 13:48:03 -08001364};
rginda8ba33642011-12-14 12:31:31 -08001365
rginda87b86462011-12-14 13:48:03 -08001366hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1367 var scrollTop = this.getVTScrollTop();
1368 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001369 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001370 this.screen_.setCursorPosition(row, column);
1371};
1372
1373hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001374 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1375 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001376 this.screen_.setCursorPosition(row, column);
1377};
1378
1379/**
1380 * Set the cursor column.
1381 *
1382 * @param {integer} column The new zero-based cursor column.
1383 */
1384hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001385 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001386};
1387
1388/**
1389 * Return the cursor column.
1390 *
1391 * @return {integer} The zero-based cursor column.
1392 */
1393hterm.Terminal.prototype.getCursorColumn = function() {
1394 return this.screen_.cursorPosition.column;
1395};
1396
1397/**
1398 * Set the cursor row.
1399 *
1400 * The cursor row is relative to the scroll region if the terminal has
1401 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1402 *
1403 * @param {integer} row The new cursor row.
1404 */
rginda87b86462011-12-14 13:48:03 -08001405hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1406 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001407};
1408
1409/**
1410 * Return the cursor row.
1411 *
1412 * @return {integer} The zero-based cursor row.
1413 */
1414hterm.Terminal.prototype.getCursorRow = function(row) {
1415 return this.screen_.cursorPosition.row;
1416};
1417
1418/**
1419 * Request that the ScrollPort redraw itself soon.
1420 *
1421 * The redraw will happen asynchronously, soon after the call stack winds down.
1422 * Multiple calls will be coalesced into a single redraw.
1423 */
1424hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001425 if (this.timeouts_.redraw)
1426 return;
rginda8ba33642011-12-14 12:31:31 -08001427
1428 var self = this;
rginda87b86462011-12-14 13:48:03 -08001429 this.timeouts_.redraw = setTimeout(function() {
1430 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001431 self.scrollPort_.redraw_();
1432 }, 0);
1433};
1434
1435/**
1436 * Request that the ScrollPort be scrolled to the bottom.
1437 *
1438 * The scroll will happen asynchronously, soon after the call stack winds down.
1439 * Multiple calls will be coalesced into a single scroll.
1440 *
1441 * This affects the scrollbar position of the ScrollPort, and has nothing to
1442 * do with the VT scroll commands.
1443 */
1444hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1445 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001446 return;
rginda8ba33642011-12-14 12:31:31 -08001447
1448 var self = this;
1449 this.timeouts_.scrollDown = setTimeout(function() {
1450 delete self.timeouts_.scrollDown;
1451 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1452 }, 10);
1453};
1454
1455/**
1456 * Move the cursor up a specified number of rows.
1457 *
1458 * @param {integer} count The number of rows to move the cursor.
1459 */
1460hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001461 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001462};
1463
1464/**
1465 * Move the cursor down a specified number of rows.
1466 *
1467 * @param {integer} count The number of rows to move the cursor.
1468 */
1469hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001470 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001471 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1472 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1473 this.screenSize.height - 1);
1474
1475 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1476 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001477 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001478};
1479
1480/**
1481 * Move the cursor left a specified number of columns.
1482 *
1483 * @param {integer} count The number of columns to move the cursor.
1484 */
1485hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001486 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001487};
1488
1489/**
1490 * Move the cursor right a specified number of columns.
1491 *
1492 * @param {integer} count The number of columns to move the cursor.
1493 */
1494hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001495 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001496 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001497 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001498 this.setCursorColumn(column);
1499};
1500
1501/**
1502 * Reverse the foreground and background colors of the terminal.
1503 *
1504 * This only affects text that was drawn with no attributes.
1505 *
1506 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1507 * been drawn with attributes that happen to coincide with the default
1508 * 'no-attribute' colors. My guess is probably not.
1509 */
1510hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001511 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001512 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001513 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1514 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001515 } else {
rginda9f5222b2012-03-05 11:53:28 -08001516 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1517 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001518 }
1519};
1520
1521/**
rginda87b86462011-12-14 13:48:03 -08001522 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001523 */
1524hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001525 if (this.bellAudio_.getAttribute('src'))
1526 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001527
rginda6d397402012-01-17 10:58:29 -08001528 this.cursorNode_.style.backgroundColor =
1529 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001530
1531 var self = this;
1532 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001533 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001534 }, 200);
rginda87b86462011-12-14 13:48:03 -08001535};
1536
1537/**
rginda8ba33642011-12-14 12:31:31 -08001538 * Set the origin mode bit.
1539 *
1540 * If origin mode is on, certain VT cursor and scrolling commands measure their
1541 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1542 * to the top of the addressable screen.
1543 *
1544 * Defaults to off.
1545 *
1546 * @param {boolean} state True to set origin mode, false to unset.
1547 */
1548hterm.Terminal.prototype.setOriginMode = function(state) {
1549 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001550 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001551};
1552
1553/**
1554 * Set the insert mode bit.
1555 *
1556 * If insert mode is on, existing text beyond the cursor position will be
1557 * shifted right to make room for new text. Otherwise, new text overwrites
1558 * any existing text.
1559 *
1560 * Defaults to off.
1561 *
1562 * @param {boolean} state True to set insert mode, false to unset.
1563 */
1564hterm.Terminal.prototype.setInsertMode = function(state) {
1565 this.options_.insertMode = state;
1566};
1567
1568/**
rginda87b86462011-12-14 13:48:03 -08001569 * Set the auto carriage return bit.
1570 *
1571 * If auto carriage return is on then a formfeed character is interpreted
1572 * as a newline, otherwise it's the same as a linefeed. The difference boils
1573 * down to whether or not the cursor column is reset.
1574 */
1575hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1576 this.options_.autoCarriageReturn = state;
1577};
1578
1579/**
rginda8ba33642011-12-14 12:31:31 -08001580 * Set the wraparound mode bit.
1581 *
1582 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1583 * to the start of the following row. Otherwise, the cursor is clamped to the
1584 * end of the screen and attempts to write past it are ignored.
1585 *
1586 * Defaults to on.
1587 *
1588 * @param {boolean} state True to set wraparound mode, false to unset.
1589 */
1590hterm.Terminal.prototype.setWraparound = function(state) {
1591 this.options_.wraparound = state;
1592};
1593
1594/**
1595 * Set the reverse-wraparound mode bit.
1596 *
1597 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1598 * to the end of the previous row. Otherwise, the cursor is clamped to column
1599 * 0.
1600 *
1601 * Defaults to off.
1602 *
1603 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1604 */
1605hterm.Terminal.prototype.setReverseWraparound = function(state) {
1606 this.options_.reverseWraparound = state;
1607};
1608
1609/**
1610 * Selects between the primary and alternate screens.
1611 *
1612 * If alternate mode is on, the alternate screen is active. Otherwise the
1613 * primary screen is active.
1614 *
1615 * Swapping screens has no effect on the scrollback buffer.
1616 *
1617 * Each screen maintains its own cursor position.
1618 *
1619 * Defaults to off.
1620 *
1621 * @param {boolean} state True to set alternate mode, false to unset.
1622 */
1623hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001624 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001625 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1626
rginda35c456b2012-02-09 17:29:05 -08001627 if (this.screen_.rowsArray.length &&
1628 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1629 // If the screen changed sizes while we were away, our rowIndexes may
1630 // be incorrect.
1631 var offset = this.scrollbackRows_.length;
1632 var ary = this.screen_.rowsArray;
1633 for (i = 0; i < ary.length; i++) {
1634 ary[i].rowIndex = offset + i;
1635 }
1636 }
rginda8ba33642011-12-14 12:31:31 -08001637
rginda35c456b2012-02-09 17:29:05 -08001638 this.realizeWidth_(this.screenSize.width);
1639 this.realizeHeight_(this.screenSize.height);
1640 this.scrollPort_.syncScrollHeight();
1641 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001642
rginda6d397402012-01-17 10:58:29 -08001643 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001644 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001645};
1646
1647/**
1648 * Set the cursor-blink mode bit.
1649 *
1650 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1651 * a visible cursor does not blink.
1652 *
1653 * You should make sure to turn blinking off if you're going to dispose of a
1654 * terminal, otherwise you'll leak a timeout.
1655 *
1656 * Defaults to on.
1657 *
1658 * @param {boolean} state True to set cursor-blink mode, false to unset.
1659 */
1660hterm.Terminal.prototype.setCursorBlink = function(state) {
1661 this.options_.cursorBlink = state;
1662
1663 if (!state && this.timeouts_.cursorBlink) {
1664 clearTimeout(this.timeouts_.cursorBlink);
1665 delete this.timeouts_.cursorBlink;
1666 }
1667
1668 if (this.options_.cursorVisible)
1669 this.setCursorVisible(true);
1670};
1671
1672/**
1673 * Set the cursor-visible mode bit.
1674 *
1675 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1676 *
1677 * Defaults to on.
1678 *
1679 * @param {boolean} state True to set cursor-visible mode, false to unset.
1680 */
1681hterm.Terminal.prototype.setCursorVisible = function(state) {
1682 this.options_.cursorVisible = state;
1683
1684 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001685 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001686 return;
1687 }
1688
rginda87b86462011-12-14 13:48:03 -08001689 this.syncCursorPosition_();
1690
1691 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001692
1693 if (this.options_.cursorBlink) {
1694 if (this.timeouts_.cursorBlink)
1695 return;
1696
1697 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1698 500);
1699 } else {
1700 if (this.timeouts_.cursorBlink) {
1701 clearTimeout(this.timeouts_.cursorBlink);
1702 delete this.timeouts_.cursorBlink;
1703 }
1704 }
1705};
1706
1707/**
rginda87b86462011-12-14 13:48:03 -08001708 * Synchronizes the visible cursor and document selection with the current
1709 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001710 */
1711hterm.Terminal.prototype.syncCursorPosition_ = function() {
1712 var topRowIndex = this.scrollPort_.getTopRowIndex();
1713 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1714 var cursorRowIndex = this.scrollbackRows_.length +
1715 this.screen_.cursorPosition.row;
1716
1717 if (cursorRowIndex > bottomRowIndex) {
1718 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001719 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001720 return;
1721 }
1722
rginda35c456b2012-02-09 17:29:05 -08001723 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1724 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1725
rginda8ba33642011-12-14 12:31:31 -08001726 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001727 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1728 'px';
1729 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1730 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001731
1732 this.cursorNode_.setAttribute('title',
1733 '(' + this.screen_.cursorPosition.row +
1734 ', ' + this.screen_.cursorPosition.column +
1735 ')');
1736
1737 // Update the caret for a11y purposes.
1738 var selection = this.document_.getSelection();
1739 if (selection && selection.isCollapsed)
1740 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001741};
1742
1743/**
1744 * Synchronizes the visible cursor with the current cursor coordinates.
1745 *
1746 * The sync will happen asynchronously, soon after the call stack winds down.
1747 * Multiple calls will be coalesced into a single sync.
1748 */
1749hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1750 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001751 return;
rginda8ba33642011-12-14 12:31:31 -08001752
1753 var self = this;
1754 this.timeouts_.syncCursor = setTimeout(function() {
1755 self.syncCursorPosition_();
1756 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001757 }, 0);
1758};
1759
rgindacc2996c2012-02-24 14:59:31 -08001760/**
1761 * Show the terminal overlay for a given amount of time.
1762 *
1763 * The terminal overlay appears in inverse video in a large font, centered
1764 * over the terminal. You should probably keep the overlay message brief,
1765 * since it's in a large font and you probably aren't going to check the size
1766 * of the terminal first.
1767 *
1768 * @param {string} msg The text (not HTML) message to display in the overlay.
1769 * @param {number} opt_timeout The amount of time to wait before fading out
1770 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1771 * stay up forever (or until the next overlay).
1772 */
1773hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001774 if (!this.overlayNode_) {
1775 if (!this.div_)
1776 return;
1777
1778 this.overlayNode_ = this.document_.createElement('div');
1779 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001780 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001781 'font-size: xx-large;' +
1782 'opacity: 0.75;' +
1783 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1784 'position: absolute;' +
1785 '-webkit-user-select: none;' +
1786 '-webkit-transition: opacity 180ms ease-in;');
1787 }
1788
rginda9f5222b2012-03-05 11:53:28 -08001789 this.overlayNode_.style.color = this.prefs_.get('background-color');
1790 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1791 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1792
rgindaf0090c92012-02-10 14:58:52 -08001793 this.overlayNode_.textContent = msg;
1794 this.overlayNode_.style.opacity = '0.75';
1795
1796 if (!this.overlayNode_.parentNode)
1797 this.div_.appendChild(this.overlayNode_);
1798
1799 this.overlayNode_.style.top = (
1800 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1801 this.overlayNode_.style.left = (
1802 this.div_.clientWidth - this.overlayNode_.clientWidth -
1803 this.scrollbarWidthPx) / 2;
1804
1805 var self = this;
1806
1807 if (this.overlayTimeout_)
1808 clearTimeout(this.overlayTimeout_);
1809
rgindacc2996c2012-02-24 14:59:31 -08001810 if (opt_timeout === null)
1811 return;
1812
rgindaf0090c92012-02-10 14:58:52 -08001813 this.overlayTimeout_ = setTimeout(function() {
1814 self.overlayNode_.style.opacity = '0';
1815 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001816 if (self.overlayNode_.parentNode)
1817 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001818 self.overlayTimeout_ = null;
1819 self.overlayNode_.style.opacity = '0.75';
1820 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001821 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001822};
1823
1824hterm.Terminal.prototype.overlaySize = function() {
1825 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1826};
1827
rginda87b86462011-12-14 13:48:03 -08001828/**
1829 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1830 *
1831 * @param {string} string The VT string representing the keystroke.
1832 */
1833hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001834 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001835 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1836
1837 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001838};
1839
1840/**
1841 * React when the ScrollPort is scrolled.
1842 */
1843hterm.Terminal.prototype.onScroll_ = function() {
1844 this.scheduleSyncCursorPosition_();
1845};
1846
1847/**
rginda9846e2f2012-01-27 13:53:33 -08001848 * React when text is pasted into the scrollPort.
1849 */
1850hterm.Terminal.prototype.onPaste_ = function(e) {
1851 this.io.onVTKeystroke(e.text);
1852};
1853
1854/**
rginda8ba33642011-12-14 12:31:31 -08001855 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001856 *
1857 * Note: This function should not directly contain code that alters the internal
1858 * state of the terminal. That kind of code belongs in realizeWidth or
1859 * realizeHeight, so that it can be executed synchronously in the case of a
1860 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001861 */
1862hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001863 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001864 this.scrollPort_.characterSize.width);
1865 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1866 this.scrollPort_.characterSize.height);
1867
1868 if (!(columnCount || rowCount)) {
1869 // We avoid these situations since they happen sometimes when the terminal
1870 // gets removed from the document, and we can't deal with that.
1871 return;
1872 }
1873
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001874 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001875 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001876 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001877};
1878
1879/**
1880 * Service the cursor blink timeout.
1881 */
1882hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001883 if (this.cursorNode_.style.opacity == '0') {
1884 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001885 } else {
rginda87b86462011-12-14 13:48:03 -08001886 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001887 }
1888};
David Reveman8f552492012-03-28 12:18:41 -04001889
1890/**
1891 * Set the scrollbar-visible mode bit.
1892 *
1893 * If scrollbar-visible is on, the vertical scrollbar will be visible.
1894 * Otherwise it will not.
1895 *
1896 * Defaults to on.
1897 *
1898 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
1899 */
1900hterm.Terminal.prototype.setScrollbarVisible = function(state) {
1901 this.scrollPort_.setScrollbarVisible(state);
1902};