blob: 4b4b85aecd7c4bd1bd7f7a55d1ef27c62748f1e2 [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 ],
211 ]);
212
213 if (needSync)
214 this.prefs_.notifyAll();
215};
216
217/**
218 * Return the current terminal background color.
219 *
220 * Intended for use by other classes, so we don't have to expose the entire
221 * prefs_ object.
222 */
223hterm.Terminal.prototype.getBackgroundColor = function() {
224 return this.prefs_.get('background-color');
225};
226
227/**
228 * Return the current terminal foreground color.
229 *
230 * Intended for use by other classes, so we don't have to expose the entire
231 * prefs_ object.
232 */
233hterm.Terminal.prototype.getForegroundColor = function() {
234 return this.prefs_.get('foreground-color');
235};
236
237/**
rginda87b86462011-12-14 13:48:03 -0800238 * Create a new instance of a terminal command and run it with a given
239 * argument string.
240 *
241 * @param {function} commandClass The constructor for a terminal command.
242 * @param {string} argString The argument string to pass to the command.
243 */
244hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
245 var self = this;
246 this.command = new commandClass(
247 { argString: argString || '',
248 io: this.io.push(),
249 onExit: function(code) {
250 self.io.pop();
251 self.io.println(hterm.msg('COMMAND_COMPLETE',
252 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800253 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800254 }
255 });
256
rgindafeaf3142012-01-31 15:14:20 -0800257 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800258 this.command.run();
259};
260
261/**
rgindafeaf3142012-01-31 15:14:20 -0800262 * Returns true if the current screen is the primary screen, false otherwise.
263 */
264hterm.Terminal.prototype.isPrimaryScreen = function() {
265 return this.screen_ = this.primaryScreen_;
266};
267
268/**
269 * Install the keyboard handler for this terminal.
270 *
271 * This will prevent the browser from seeing any keystrokes sent to the
272 * terminal.
273 */
274hterm.Terminal.prototype.installKeyboard = function() {
275 this.keyboard.installKeyboard(this.document_.body.firstChild);
276}
277
278/**
279 * Uninstall the keyboard handler for this terminal.
280 */
281hterm.Terminal.prototype.uninstallKeyboard = function() {
282 this.keyboard.installKeyboard(null);
283}
284
285/**
rginda35c456b2012-02-09 17:29:05 -0800286 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800287 *
288 * Call setFontSize(0) to reset to the default font size.
289 *
290 * This function does not modify the font-size preference.
291 *
292 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800293 */
294hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800295 if (px === 0)
296 px = this.prefs_.get('font-size');
297
rginda35c456b2012-02-09 17:29:05 -0800298 this.scrollPort_.setFontSize(px);
299};
300
301/**
302 * Get the current font size.
303 */
304hterm.Terminal.prototype.getFontSize = function() {
305 return this.scrollPort_.getFontSize();
306};
307
308/**
309 * Set the CSS "font-family" for this terminal.
310 */
rginda9f5222b2012-03-05 11:53:28 -0800311hterm.Terminal.prototype.syncFontFamily = function() {
312 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
313 this.prefs_.get('font-smoothing'));
314 this.syncBoldSafeState();
315};
316
317hterm.Terminal.prototype.syncBoldSafeState = function() {
318 var enableBold = this.prefs_.get('enable-bold');
319 if (enableBold !== null) {
320 this.screen_.textAttributes.enableBold = enableBold;
321 return;
322 }
323
rgindaf7521392012-02-28 17:20:34 -0800324 var normalSize = this.scrollPort_.measureCharacterSize();
325 var boldSize = this.scrollPort_.measureCharacterSize('bold');
326
327 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800328 if (!isBoldSafe) {
329 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700330 'from normal. Font family is: ' +
331 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800332 }
rginda9f5222b2012-03-05 11:53:28 -0800333
334 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800335};
336
337/**
rginda87b86462011-12-14 13:48:03 -0800338 * Return a copy of the current cursor position.
339 *
340 * @return {hterm.RowCol} The RowCol object representing the current position.
341 */
342hterm.Terminal.prototype.saveCursor = function() {
343 return this.screen_.cursorPosition.clone();
344};
345
rgindaa19afe22012-01-25 15:40:22 -0800346hterm.Terminal.prototype.getTextAttributes = function() {
347 return this.screen_.textAttributes;
348};
349
rginda87b86462011-12-14 13:48:03 -0800350/**
rginda9846e2f2012-01-27 13:53:33 -0800351 * Change the title of this terminal's window.
352 */
353hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800354 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800355};
356
357/**
rginda87b86462011-12-14 13:48:03 -0800358 * Restore a previously saved cursor position.
359 *
360 * @param {hterm.RowCol} cursor The position to restore.
361 */
362hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800363 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
364 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
365 this.screen_.setCursorPosition(row, column);
366 if (cursor.column > column ||
367 cursor.column == column && cursor.overflow) {
368 this.screen_.cursorPosition.overflow = true;
369 }
rginda87b86462011-12-14 13:48:03 -0800370};
371
372/**
373 * Set the width of the terminal, resizing the UI to match.
374 */
375hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800376 if (columnCount == null) {
377 this.div_.style.width = '100%';
378 return;
379 }
380
rginda35c456b2012-02-09 17:29:05 -0800381 this.div_.style.width = this.scrollPort_.characterSize.width *
382 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400383 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800384 this.scheduleSyncCursorPosition_();
385};
rginda87b86462011-12-14 13:48:03 -0800386
rgindac9bc5502012-01-18 11:48:44 -0800387/**
rginda35c456b2012-02-09 17:29:05 -0800388 * Set the height of the terminal, resizing the UI to match.
389 */
390hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800391 if (rowCount == null) {
392 this.div_.style.height = '100%';
393 return;
394 }
395
rginda35c456b2012-02-09 17:29:05 -0800396 this.div_.style.height =
rginda9f5222b2012-03-05 11:53:28 -0800397 this.scrollPort_.characterSize.height * rowCount + 1 + 'px';
rginda35c456b2012-02-09 17:29:05 -0800398 this.realizeSize_(this.screenSize.width, rowCount);
399 this.scheduleSyncCursorPosition_();
400};
401
402/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400403 * Deal with terminal size changes.
404 *
405 */
406hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
407 if (columnCount != this.screenSize.width)
408 this.realizeWidth_(columnCount);
409
410 if (rowCount != this.screenSize.height)
411 this.realizeHeight_(rowCount);
412
413 // Send new terminal size to plugin.
414 this.io.onTerminalResize(columnCount, rowCount);
415};
416
417/**
rgindac9bc5502012-01-18 11:48:44 -0800418 * Deal with terminal width changes.
419 *
420 * This function does what needs to be done when the terminal width changes
421 * out from under us. It happens here rather than in onResize_() because this
422 * code may need to run synchronously to handle programmatic changes of
423 * terminal width.
424 *
425 * Relying on the browser to send us an async resize event means we may not be
426 * in the correct state yet when the next escape sequence hits.
427 */
428hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
429 var deltaColumns = columnCount - this.screen_.getWidth();
430
rginda87b86462011-12-14 13:48:03 -0800431 this.screenSize.width = columnCount;
432 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800433
434 if (deltaColumns > 0) {
435 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
436 } else {
437 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
438 if (this.tabStops_[i] <= columnCount)
439 break;
440
441 this.tabStops_.pop();
442 }
443 }
444
445 this.screen_.setColumnCount(this.screenSize.width);
446};
447
448/**
449 * Deal with terminal height changes.
450 *
451 * This function does what needs to be done when the terminal height changes
452 * out from under us. It happens here rather than in onResize_() because this
453 * code may need to run synchronously to handle programmatic changes of
454 * terminal height.
455 *
456 * Relying on the browser to send us an async resize event means we may not be
457 * in the correct state yet when the next escape sequence hits.
458 */
459hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
460 var deltaRows = rowCount - this.screen_.getHeight();
461
462 this.screenSize.height = rowCount;
463
464 var cursor = this.saveCursor();
465
466 if (deltaRows < 0) {
467 // Screen got smaller.
468 deltaRows *= -1;
469 while (deltaRows) {
470 var lastRow = this.getRowCount() - 1;
471 if (lastRow - this.scrollbackRows_.length == cursor.row)
472 break;
473
474 if (this.getRowText(lastRow))
475 break;
476
477 this.screen_.popRow();
478 deltaRows--;
479 }
480
481 var ary = this.screen_.shiftRows(deltaRows);
482 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
483
484 // We just removed rows from the top of the screen, we need to update
485 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800486 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800487 } else if (deltaRows > 0) {
488 // Screen got larger.
489
490 if (deltaRows <= this.scrollbackRows_.length) {
491 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
492 var rows = this.scrollbackRows_.splice(
493 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
494 this.screen_.unshiftRows(rows);
495 deltaRows -= scrollbackCount;
496 cursor.row += scrollbackCount;
497 }
498
499 if (deltaRows)
500 this.appendRows_(deltaRows);
501 }
502
rginda35c456b2012-02-09 17:29:05 -0800503 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800504 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800505};
506
507/**
508 * Scroll the terminal to the top of the scrollback buffer.
509 */
510hterm.Terminal.prototype.scrollHome = function() {
511 this.scrollPort_.scrollRowToTop(0);
512};
513
514/**
515 * Scroll the terminal to the end.
516 */
517hterm.Terminal.prototype.scrollEnd = function() {
518 this.scrollPort_.scrollRowToBottom(this.getRowCount());
519};
520
521/**
522 * Scroll the terminal one page up (minus one line) relative to the current
523 * position.
524 */
525hterm.Terminal.prototype.scrollPageUp = function() {
526 var i = this.scrollPort_.getTopRowIndex();
527 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
528};
529
530/**
531 * Scroll the terminal one page down (minus one line) relative to the current
532 * position.
533 */
534hterm.Terminal.prototype.scrollPageDown = function() {
535 var i = this.scrollPort_.getTopRowIndex();
536 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800537};
538
rgindac9bc5502012-01-18 11:48:44 -0800539/**
540 * Full terminal reset.
541 */
rginda87b86462011-12-14 13:48:03 -0800542hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800543 this.clearAllTabStops();
544 this.setDefaultTabStops();
rgindac9bc5502012-01-18 11:48:44 -0800545 this.setVTScrollRegion(null, null);
rginda9ea433c2012-03-16 11:57:00 -0700546
547 this.clearHome(this.primaryScreen_);
548 this.primaryScreen_.textAttributes.reset();
549
550 this.clearHome(this.alternateScreen_);
551 this.alternateScreen_.textAttributes.reset();
552
rgindac9bc5502012-01-18 11:48:44 -0800553 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800554};
555
rgindac9bc5502012-01-18 11:48:44 -0800556/**
557 * Soft terminal reset.
558 */
rginda0f5c0292012-01-13 11:00:13 -0800559hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800560 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800561 this.setCursorVisible(true);
562 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800563};
564
rgindac9bc5502012-01-18 11:48:44 -0800565/**
566 * Move the cursor forward to the next tab stop, or to the last column
567 * if no more tab stops are set.
568 */
569hterm.Terminal.prototype.forwardTabStop = function() {
570 var column = this.screen_.cursorPosition.column;
571
572 for (var i = 0; i < this.tabStops_.length; i++) {
573 if (this.tabStops_[i] > column) {
574 this.setCursorColumn(this.tabStops_[i]);
575 return;
576 }
577 }
578
579 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800580};
581
rgindac9bc5502012-01-18 11:48:44 -0800582/**
583 * Move the cursor backward to the previous tab stop, or to the first column
584 * if no previous tab stops are set.
585 */
586hterm.Terminal.prototype.backwardTabStop = function() {
587 var column = this.screen_.cursorPosition.column;
588
589 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
590 if (this.tabStops_[i] < column) {
591 this.setCursorColumn(this.tabStops_[i]);
592 return;
593 }
594 }
595
596 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800597};
598
rgindac9bc5502012-01-18 11:48:44 -0800599/**
600 * Set a tab stop at the given column.
601 *
602 * @param {int} column Zero based column.
603 */
604hterm.Terminal.prototype.setTabStop = function(column) {
605 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
606 if (this.tabStops_[i] == column)
607 return;
608
609 if (this.tabStops_[i] < column) {
610 this.tabStops_.splice(i + 1, 0, column);
611 return;
612 }
613 }
614
615 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800616};
617
rgindac9bc5502012-01-18 11:48:44 -0800618/**
619 * Clear the tab stop at the current cursor position.
620 *
621 * No effect if there is no tab stop at the current cursor position.
622 */
623hterm.Terminal.prototype.clearTabStopAtCursor = function() {
624 var column = this.screen_.cursorPosition.column;
625
626 var i = this.tabStops_.indexOf(column);
627 if (i == -1)
628 return;
629
630 this.tabStops_.splice(i, 1);
631};
632
633/**
634 * Clear all tab stops.
635 */
636hterm.Terminal.prototype.clearAllTabStops = function() {
637 this.tabStops_.length = 0;
638};
639
640/**
641 * Set up the default tab stops, starting from a given column.
642 *
643 * This sets a tabstop every (column % this.tabWidth) column, starting
644 * from the specified column, or 0 if no column is provided.
645 *
646 * This does not clear the existing tab stops first, use clearAllTabStops
647 * for that.
648 *
649 * @param {int} opt_start Optional starting zero based starting column, useful
650 * for filling out missing tab stops when the terminal is resized.
651 */
652hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
653 var start = opt_start || 0;
654 var w = this.tabWidth;
655 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
656 for (var i = 0; i < stopCount; i++) {
657 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
658 }
rginda87b86462011-12-14 13:48:03 -0800659};
660
rginda6d397402012-01-17 10:58:29 -0800661/**
662 * Save cursor position and attributes.
663 *
664 * TODO(rginda): Save attributes once we support them.
665 */
rginda87b86462011-12-14 13:48:03 -0800666hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800667 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800668 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800669};
670
rginda6d397402012-01-17 10:58:29 -0800671/**
672 * Restore cursor position and attributes.
673 *
674 * TODO(rginda): Restore attributes once we support them.
675 */
rginda87b86462011-12-14 13:48:03 -0800676hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800677 if (this.savedOptions_.cursor)
678 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800679 if (this.savedOptions_.textAttributes)
680 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800681};
682
683/**
684 * Interpret a sequence of characters.
685 *
686 * Incomplete escape sequences are buffered until the next call.
687 *
688 * @param {string} str Sequence of characters to interpret or pass through.
689 */
690hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800691 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800692 this.scheduleSyncCursorPosition_();
693};
694
695/**
696 * Take over the given DIV for use as the terminal display.
697 *
698 * @param {HTMLDivElement} div The div to use as the terminal display.
699 */
700hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800701 this.div_ = div;
702
rginda8ba33642011-12-14 12:31:31 -0800703 this.scrollPort_.decorate(div);
rgindaf7521392012-02-28 17:20:34 -0800704
rginda9f5222b2012-03-05 11:53:28 -0800705 this.setFontSize(this.prefs_.get('font-size'));
706 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800707
rginda8ba33642011-12-14 12:31:31 -0800708 this.document_ = this.scrollPort_.getDocument();
709
rginda8ba33642011-12-14 12:31:31 -0800710 this.cursorNode_ = this.document_.createElement('div');
711 this.cursorNode_.style.cssText =
712 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800713 'top: -99px;' +
714 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800715 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
716 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800717 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800718 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800719 this.document_.body.appendChild(this.cursorNode_);
720
721 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800722
rginda87b86462011-12-14 13:48:03 -0800723 var link = this.document_.createElement('link');
724 link.setAttribute('href', '../css/dialogs.css');
725 link.setAttribute('rel', 'stylesheet');
726 this.document_.head.appendChild(link);
727
728 this.alertDialog = new AlertDialog(this.document_.body);
729 this.promptDialog = new PromptDialog(this.document_.body);
730 this.confirmDialog = new ConfirmDialog(this.document_.body);
731
732 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800733 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800734};
735
736hterm.Terminal.prototype.getDocument = function() {
737 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800738};
739
740/**
741 * Return the HTML Element for a given row index.
742 *
743 * This is a method from the RowProvider interface. The ScrollPort uses
744 * it to fetch rows on demand as they are scrolled into view.
745 *
746 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
747 * pairs to conserve memory.
748 *
749 * @param {integer} index The zero-based row index, measured relative to the
750 * start of the scrollback buffer. On-screen rows will always have the
751 * largest indicies.
752 * @return {HTMLElement} The 'x-row' element containing for the requested row.
753 */
754hterm.Terminal.prototype.getRowNode = function(index) {
755 if (index < this.scrollbackRows_.length)
756 return this.scrollbackRows_[index];
757
758 var screenIndex = index - this.scrollbackRows_.length;
759 return this.screen_.rowsArray[screenIndex];
760};
761
762/**
763 * Return the text content for a given range of rows.
764 *
765 * This is a method from the RowProvider interface. The ScrollPort uses
766 * it to fetch text content on demand when the user attempts to copy their
767 * selection to the clipboard.
768 *
769 * @param {integer} start The zero-based row index to start from, measured
770 * relative to the start of the scrollback buffer. On-screen rows will
771 * always have the largest indicies.
772 * @param {integer} end The zero-based row index to end on, measured
773 * relative to the start of the scrollback buffer.
774 * @return {string} A single string containing the text value of the range of
775 * rows. Lines will be newline delimited, with no trailing newline.
776 */
777hterm.Terminal.prototype.getRowsText = function(start, end) {
778 var ary = [];
779 for (var i = start; i < end; i++) {
780 var node = this.getRowNode(i);
781 ary.push(node.textContent);
782 }
783
784 return ary.join('\n');
785};
786
787/**
788 * Return the text content for a given row.
789 *
790 * This is a method from the RowProvider interface. The ScrollPort uses
791 * it to fetch text content on demand when the user attempts to copy their
792 * selection to the clipboard.
793 *
794 * @param {integer} index The zero-based row index to return, measured
795 * relative to the start of the scrollback buffer. On-screen rows will
796 * always have the largest indicies.
797 * @return {string} A string containing the text value of the selected row.
798 */
799hterm.Terminal.prototype.getRowText = function(index) {
800 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800801 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800802};
803
804/**
805 * Return the total number of rows in the addressable screen and in the
806 * scrollback buffer of this terminal.
807 *
808 * This is a method from the RowProvider interface. The ScrollPort uses
809 * it to compute the size of the scrollbar.
810 *
811 * @return {integer} The number of rows in this terminal.
812 */
813hterm.Terminal.prototype.getRowCount = function() {
814 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
815};
816
817/**
818 * Create DOM nodes for new rows and append them to the end of the terminal.
819 *
820 * This is the only correct way to add a new DOM node for a row. Notice that
821 * the new row is appended to the bottom of the list of rows, and does not
822 * require renumbering (of the rowIndex property) of previous rows.
823 *
824 * If you think you want a new blank row somewhere in the middle of the
825 * terminal, look into moveRows_().
826 *
827 * This method does not pay attention to vtScrollTop/Bottom, since you should
828 * be using moveRows() in cases where they would matter.
829 *
830 * The cursor will be positioned at column 0 of the first inserted line.
831 */
832hterm.Terminal.prototype.appendRows_ = function(count) {
833 var cursorRow = this.screen_.rowsArray.length;
834 var offset = this.scrollbackRows_.length + cursorRow;
835 for (var i = 0; i < count; i++) {
836 var row = this.document_.createElement('x-row');
837 row.appendChild(this.document_.createTextNode(''));
838 row.rowIndex = offset + i;
839 this.screen_.pushRow(row);
840 }
841
842 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
843 if (extraRows > 0) {
844 var ary = this.screen_.shiftRows(extraRows);
845 Array.prototype.push.apply(this.scrollbackRows_, ary);
846 this.scheduleScrollDown_();
847 }
848
849 if (cursorRow >= this.screen_.rowsArray.length)
850 cursorRow = this.screen_.rowsArray.length - 1;
851
rginda87b86462011-12-14 13:48:03 -0800852 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800853};
854
855/**
856 * Relocate rows from one part of the addressable screen to another.
857 *
858 * This is used to recycle rows during VT scrolls (those which are driven
859 * by VT commands, rather than by the user manipulating the scrollbar.)
860 *
861 * In this case, the blank lines scrolled into the scroll region are made of
862 * the nodes we scrolled off. These have their rowIndex properties carefully
863 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800864 */
865hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
866 var ary = this.screen_.removeRows(fromIndex, count);
867 this.screen_.insertRows(toIndex, ary);
868
869 var start, end;
870 if (fromIndex < toIndex) {
871 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800872 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800873 } else {
874 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800875 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800876 }
877
878 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800879 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800880};
881
882/**
883 * Renumber the rowIndex property of the given range of rows.
884 *
885 * The start and end indicies are relative to the screen, not the scrollback.
886 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800887 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800888 * no need to renumber scrollback rows.
889 */
890hterm.Terminal.prototype.renumberRows_ = function(start, end) {
891 var offset = this.scrollbackRows_.length;
892 for (var i = start; i < end; i++) {
893 this.screen_.rowsArray[i].rowIndex = offset + i;
894 }
895};
896
897/**
898 * Print a string to the terminal.
899 *
900 * This respects the current insert and wraparound modes. It will add new lines
901 * to the end of the terminal, scrolling off the top into the scrollback buffer
902 * if necessary.
903 *
904 * The string is *not* parsed for escape codes. Use the interpret() method if
905 * that's what you're after.
906 *
907 * @param{string} str The string to print.
908 */
909hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800910 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
911 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800912
rgindaa19afe22012-01-25 15:40:22 -0800913 if (this.options_.insertMode) {
914 this.screen_.insertString(str);
915 } else {
916 this.screen_.overwriteString(str);
917 }
918
919 var overflow = this.screen_.maybeClipCurrentRow();
920
921 if (this.options_.wraparound && overflow) {
922 var lastColumn;
923
924 do {
rginda35c456b2012-02-09 17:29:05 -0800925 this.newLine();
926 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800927
928 if (!this.options_.insertMode)
929 this.screen_.deleteChars(overflow.characterLength);
930
931 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800932
933 overflow = this.screen_.maybeClipCurrentRow();
934 } while (overflow);
935
936 this.setCursorColumn(lastColumn);
937 }
rginda8ba33642011-12-14 12:31:31 -0800938
939 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800940
rginda9f5222b2012-03-05 11:53:28 -0800941 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -0800942 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800943};
944
945/**
rginda87b86462011-12-14 13:48:03 -0800946 * Set the VT scroll region.
947 *
rginda87b86462011-12-14 13:48:03 -0800948 * This also resets the cursor position to the absolute (0, 0) position, since
949 * that's what xterm appears to do.
950 *
951 * @param {integer} scrollTop The zero-based top of the scroll region.
952 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
953 * inclusive.
954 */
955hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
956 this.vtScrollTop_ = scrollTop;
957 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800958};
959
960/**
rginda8ba33642011-12-14 12:31:31 -0800961 * Return the top row index according to the VT.
962 *
963 * This will return 0 unless the terminal has been told to restrict scrolling
964 * to some lower row. It is used for some VT cursor positioning and scrolling
965 * commands.
966 *
967 * @return {integer} The topmost row in the terminal's scroll region.
968 */
969hterm.Terminal.prototype.getVTScrollTop = function() {
970 if (this.vtScrollTop_ != null)
971 return this.vtScrollTop_;
972
973 return 0;
rginda87b86462011-12-14 13:48:03 -0800974};
rginda8ba33642011-12-14 12:31:31 -0800975
976/**
977 * Return the bottom row index according to the VT.
978 *
979 * This will return the height of the terminal unless the it has been told to
980 * restrict scrolling to some higher row. It is used for some VT cursor
981 * positioning and scrolling commands.
982 *
983 * @return {integer} The bottommost row in the terminal's scroll region.
984 */
985hterm.Terminal.prototype.getVTScrollBottom = function() {
986 if (this.vtScrollBottom_ != null)
987 return this.vtScrollBottom_;
988
rginda87b86462011-12-14 13:48:03 -0800989 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800990}
991
992/**
993 * Process a '\n' character.
994 *
995 * If the cursor is on the final row of the terminal this will append a new
996 * blank row to the screen and scroll the topmost row into the scrollback
997 * buffer.
998 *
999 * Otherwise, this moves the cursor to column zero of the next row.
1000 */
1001hterm.Terminal.prototype.newLine = function() {
1002 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001003 // If we're at the end of the screen we need to append a new line and
1004 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001005 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001006 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1007 // End of the scroll region does not affect the scrollback buffer.
1008 this.vtScrollUp(1);
1009 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001010 } else {
rginda87b86462011-12-14 13:48:03 -08001011 // Anywhere else in the screen just moves the cursor.
1012 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001013 }
1014};
1015
1016/**
1017 * Like newLine(), except maintain the cursor column.
1018 */
1019hterm.Terminal.prototype.lineFeed = function() {
1020 var column = this.screen_.cursorPosition.column;
1021 this.newLine();
1022 this.setCursorColumn(column);
1023};
1024
1025/**
rginda87b86462011-12-14 13:48:03 -08001026 * If autoCarriageReturn is set then newLine(), else lineFeed().
1027 */
1028hterm.Terminal.prototype.formFeed = function() {
1029 if (this.options_.autoCarriageReturn) {
1030 this.newLine();
1031 } else {
1032 this.lineFeed();
1033 }
1034};
1035
1036/**
1037 * Move the cursor up one row, possibly inserting a blank line.
1038 *
1039 * The cursor column is not changed.
1040 */
1041hterm.Terminal.prototype.reverseLineFeed = function() {
1042 var scrollTop = this.getVTScrollTop();
1043 var currentRow = this.screen_.cursorPosition.row;
1044
1045 if (currentRow == scrollTop) {
1046 this.insertLines(1);
1047 } else {
1048 this.setAbsoluteCursorRow(currentRow - 1);
1049 }
1050};
1051
1052/**
rginda8ba33642011-12-14 12:31:31 -08001053 * Replace all characters to the left of the current cursor with the space
1054 * character.
1055 *
1056 * TODO(rginda): This should probably *remove* the characters (not just replace
1057 * with a space) if there are no characters at or beyond the current cursor
1058 * position. Once it does that, it'll have the same text-attribute related
1059 * issues as hterm.Screen.prototype.clearCursorRow :/
1060 */
1061hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001062 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001063 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001064 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1065 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001066};
1067
1068/**
1069 * Erase a given number of characters to the right of the cursor, shifting
1070 * remaining characters to the left.
1071 *
1072 * The cursor position is unchanged.
1073 *
1074 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1075 * the end of the text.
1076 *
1077 * TODO(rginda): This likely has text-attribute related troubles similar to the
1078 * todo on hterm.Screen.prototype.clearCursorRow.
1079 */
1080hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001081 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001082
rginda87b86462011-12-14 13:48:03 -08001083 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001084 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1085 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001086 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001087};
1088
1089/**
1090 * Erase the current line.
1091 *
1092 * The cursor position is unchanged.
1093 *
1094 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1095 * has a text-attribute related TODO.
1096 */
1097hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001098 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001099 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001100 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001101};
1102
1103/**
1104 * Erase all characters from the start of the scroll region to the current
1105 * cursor position.
1106 *
1107 * The cursor position is unchanged.
1108 *
1109 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1110 * has a text-attribute related TODO.
1111 */
1112hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001113 var cursor = this.saveCursor();
1114
1115 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001116
1117 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001118 for (var i = top; i < cursor.row; i++) {
1119 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001120 this.screen_.clearCursorRow();
1121 }
1122
rginda87b86462011-12-14 13:48:03 -08001123 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001124};
1125
1126/**
1127 * Erase all characters from the current cursor position to the end of the
1128 * scroll region.
1129 *
1130 * The cursor position is unchanged.
1131 *
1132 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1133 * has a text-attribute related TODO.
1134 */
1135hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001136 var cursor = this.saveCursor();
1137
1138 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001139
1140 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001141 for (var i = cursor.row + 1; i <= bottom; i++) {
1142 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001143 this.screen_.clearCursorRow();
1144 }
1145
rginda87b86462011-12-14 13:48:03 -08001146 this.restoreCursor(cursor);
1147};
1148
1149/**
1150 * Fill the terminal with a given character.
1151 *
1152 * This methods does not respect the VT scroll region.
1153 *
1154 * @param {string} ch The character to use for the fill.
1155 */
1156hterm.Terminal.prototype.fill = function(ch) {
1157 var cursor = this.saveCursor();
1158
1159 this.setAbsoluteCursorPosition(0, 0);
1160 for (var row = 0; row < this.screenSize.height; row++) {
1161 for (var col = 0; col < this.screenSize.width; col++) {
1162 this.setAbsoluteCursorPosition(row, col);
1163 this.screen_.overwriteString(ch);
1164 }
1165 }
1166
1167 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001168};
1169
1170/**
rginda9ea433c2012-03-16 11:57:00 -07001171 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001172 *
rginda9ea433c2012-03-16 11:57:00 -07001173 * This does not respect the scroll region.
1174 *
1175 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1176 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001177 *
1178 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1179 * has a text-attribute related TODO.
1180 */
rginda9ea433c2012-03-16 11:57:00 -07001181hterm.Terminal.prototype.clearHome = function(opt_screen) {
1182 var screen = opt_screen || this.screen_;
1183 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001184
rgindae4d29232012-01-19 10:47:13 -08001185 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001186 screen.setCursorPosition(i, 0);
1187 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001188 }
1189
rginda9ea433c2012-03-16 11:57:00 -07001190 screen.setCursorPosition(0, 0);
1191};
1192
1193/**
1194 * Erase the entire display without changing the cursor position.
1195 *
1196 * The cursor position is unchanged. This does not respect the scroll
1197 * region.
1198 *
1199 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1200 * to the current screen.
1201 *
1202 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1203 * has a text-attribute related TODO.
1204 */
1205hterm.Terminal.prototype.clear = function(opt_screen) {
1206 var screen = opt_screen || this.screen_;
1207 var cursor = screen.cursorPosition.clone();
1208 this.clearHome(screen);
1209 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001210};
1211
1212/**
1213 * VT command to insert lines at the current cursor row.
1214 *
1215 * This respects the current scroll region. Rows pushed off the bottom are
1216 * lost (they won't show up in the scrollback buffer).
1217 *
1218 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1219 * has a text-attribute related TODO.
1220 *
1221 * @param {integer} count The number of lines to insert.
1222 */
1223hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001224 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001225
1226 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001227 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001228
rgindae4d29232012-01-19 10:47:13 -08001229 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001230 if (start != cursor.row)
1231 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001232
1233 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001234 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001235 this.screen_.clearCursorRow();
1236 }
1237
rginda87b86462011-12-14 13:48:03 -08001238 cursor.column = 0;
1239 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001240};
1241
1242/**
1243 * VT command to delete lines at the current cursor row.
1244 *
1245 * New rows are added to the bottom of scroll region to take their place. New
1246 * rows are strictly there to take up space and have no content or style.
1247 */
1248hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001249 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001250
rginda87b86462011-12-14 13:48:03 -08001251 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001252 var bottom = this.getVTScrollBottom();
1253
rginda87b86462011-12-14 13:48:03 -08001254 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001255 count = Math.min(count, maxCount);
1256
rginda87b86462011-12-14 13:48:03 -08001257 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001258 if (count != maxCount)
1259 this.moveRows_(top, count, moveStart);
1260
1261 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001262 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001263 this.screen_.clearCursorRow();
1264 }
1265
rginda87b86462011-12-14 13:48:03 -08001266 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001267};
1268
1269/**
1270 * Inserts the given number of spaces at the current cursor position.
1271 *
rginda87b86462011-12-14 13:48:03 -08001272 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001273 */
1274hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001275 var cursor = this.saveCursor();
1276
rginda0f5c0292012-01-13 11:00:13 -08001277 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001278 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001279 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001280
1281 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001282};
1283
1284/**
1285 * Forward-delete the specified number of characters starting at the cursor
1286 * position.
1287 *
1288 * @param {integer} count The number of characters to delete.
1289 */
1290hterm.Terminal.prototype.deleteChars = function(count) {
1291 this.screen_.deleteChars(count);
1292};
1293
1294/**
1295 * Shift rows in the scroll region upwards by a given number of lines.
1296 *
1297 * New rows are inserted at the bottom of the scroll region to fill the
1298 * vacated rows. The new rows not filled out with the current text attributes.
1299 *
1300 * This function does not affect the scrollback rows at all. Rows shifted
1301 * off the top are lost.
1302 *
rginda87b86462011-12-14 13:48:03 -08001303 * The cursor position is not altered.
1304 *
rginda8ba33642011-12-14 12:31:31 -08001305 * @param {integer} count The number of rows to scroll.
1306 */
1307hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001308 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001309
rginda87b86462011-12-14 13:48:03 -08001310 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001311 this.deleteLines(count);
1312
rginda87b86462011-12-14 13:48:03 -08001313 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001314};
1315
1316/**
1317 * Shift rows below the cursor down by a given number of lines.
1318 *
1319 * This function respects the current scroll region.
1320 *
1321 * New rows are inserted at the top of the scroll region to fill the
1322 * vacated rows. The new rows not filled out with the current text attributes.
1323 *
1324 * This function does not affect the scrollback rows at all. Rows shifted
1325 * off the bottom are lost.
1326 *
1327 * @param {integer} count The number of rows to scroll.
1328 */
1329hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001330 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001331
rginda87b86462011-12-14 13:48:03 -08001332 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001333 this.insertLines(opt_count);
1334
rginda87b86462011-12-14 13:48:03 -08001335 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001336};
1337
rginda87b86462011-12-14 13:48:03 -08001338
rginda8ba33642011-12-14 12:31:31 -08001339/**
1340 * Set the cursor position.
1341 *
1342 * The cursor row is relative to the scroll region if the terminal has
1343 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1344 *
1345 * @param {integer} row The new zero-based cursor row.
1346 * @param {integer} row The new zero-based cursor column.
1347 */
1348hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1349 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001350 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001351 } else {
rginda87b86462011-12-14 13:48:03 -08001352 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001353 }
rginda87b86462011-12-14 13:48:03 -08001354};
rginda8ba33642011-12-14 12:31:31 -08001355
rginda87b86462011-12-14 13:48:03 -08001356hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1357 var scrollTop = this.getVTScrollTop();
1358 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001359 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001360 this.screen_.setCursorPosition(row, column);
1361};
1362
1363hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001364 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1365 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001366 this.screen_.setCursorPosition(row, column);
1367};
1368
1369/**
1370 * Set the cursor column.
1371 *
1372 * @param {integer} column The new zero-based cursor column.
1373 */
1374hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001375 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001376};
1377
1378/**
1379 * Return the cursor column.
1380 *
1381 * @return {integer} The zero-based cursor column.
1382 */
1383hterm.Terminal.prototype.getCursorColumn = function() {
1384 return this.screen_.cursorPosition.column;
1385};
1386
1387/**
1388 * Set the cursor row.
1389 *
1390 * The cursor row is relative to the scroll region if the terminal has
1391 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1392 *
1393 * @param {integer} row The new cursor row.
1394 */
rginda87b86462011-12-14 13:48:03 -08001395hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1396 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001397};
1398
1399/**
1400 * Return the cursor row.
1401 *
1402 * @return {integer} The zero-based cursor row.
1403 */
1404hterm.Terminal.prototype.getCursorRow = function(row) {
1405 return this.screen_.cursorPosition.row;
1406};
1407
1408/**
1409 * Request that the ScrollPort redraw itself soon.
1410 *
1411 * The redraw will happen asynchronously, soon after the call stack winds down.
1412 * Multiple calls will be coalesced into a single redraw.
1413 */
1414hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001415 if (this.timeouts_.redraw)
1416 return;
rginda8ba33642011-12-14 12:31:31 -08001417
1418 var self = this;
rginda87b86462011-12-14 13:48:03 -08001419 this.timeouts_.redraw = setTimeout(function() {
1420 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001421 self.scrollPort_.redraw_();
1422 }, 0);
1423};
1424
1425/**
1426 * Request that the ScrollPort be scrolled to the bottom.
1427 *
1428 * The scroll will happen asynchronously, soon after the call stack winds down.
1429 * Multiple calls will be coalesced into a single scroll.
1430 *
1431 * This affects the scrollbar position of the ScrollPort, and has nothing to
1432 * do with the VT scroll commands.
1433 */
1434hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1435 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001436 return;
rginda8ba33642011-12-14 12:31:31 -08001437
1438 var self = this;
1439 this.timeouts_.scrollDown = setTimeout(function() {
1440 delete self.timeouts_.scrollDown;
1441 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1442 }, 10);
1443};
1444
1445/**
1446 * Move the cursor up a specified number of rows.
1447 *
1448 * @param {integer} count The number of rows to move the cursor.
1449 */
1450hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001451 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001452};
1453
1454/**
1455 * Move the cursor down a specified number of rows.
1456 *
1457 * @param {integer} count The number of rows to move the cursor.
1458 */
1459hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001460 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001461 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1462 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1463 this.screenSize.height - 1);
1464
1465 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1466 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001467 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001468};
1469
1470/**
1471 * Move the cursor left a specified number of columns.
1472 *
1473 * @param {integer} count The number of columns to move the cursor.
1474 */
1475hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001476 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001477};
1478
1479/**
1480 * Move the cursor right a specified number of columns.
1481 *
1482 * @param {integer} count The number of columns to move the cursor.
1483 */
1484hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001485 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001486 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001487 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001488 this.setCursorColumn(column);
1489};
1490
1491/**
1492 * Reverse the foreground and background colors of the terminal.
1493 *
1494 * This only affects text that was drawn with no attributes.
1495 *
1496 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1497 * been drawn with attributes that happen to coincide with the default
1498 * 'no-attribute' colors. My guess is probably not.
1499 */
1500hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001501 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001502 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001503 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1504 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001505 } else {
rginda9f5222b2012-03-05 11:53:28 -08001506 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1507 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001508 }
1509};
1510
1511/**
rginda87b86462011-12-14 13:48:03 -08001512 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001513 */
1514hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001515 if (this.bellAudio_.getAttribute('src'))
1516 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001517
rginda6d397402012-01-17 10:58:29 -08001518 this.cursorNode_.style.backgroundColor =
1519 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001520
1521 var self = this;
1522 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001523 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001524 }, 200);
rginda87b86462011-12-14 13:48:03 -08001525};
1526
1527/**
rginda8ba33642011-12-14 12:31:31 -08001528 * Set the origin mode bit.
1529 *
1530 * If origin mode is on, certain VT cursor and scrolling commands measure their
1531 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1532 * to the top of the addressable screen.
1533 *
1534 * Defaults to off.
1535 *
1536 * @param {boolean} state True to set origin mode, false to unset.
1537 */
1538hterm.Terminal.prototype.setOriginMode = function(state) {
1539 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001540 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001541};
1542
1543/**
1544 * Set the insert mode bit.
1545 *
1546 * If insert mode is on, existing text beyond the cursor position will be
1547 * shifted right to make room for new text. Otherwise, new text overwrites
1548 * any existing text.
1549 *
1550 * Defaults to off.
1551 *
1552 * @param {boolean} state True to set insert mode, false to unset.
1553 */
1554hterm.Terminal.prototype.setInsertMode = function(state) {
1555 this.options_.insertMode = state;
1556};
1557
1558/**
rginda87b86462011-12-14 13:48:03 -08001559 * Set the auto carriage return bit.
1560 *
1561 * If auto carriage return is on then a formfeed character is interpreted
1562 * as a newline, otherwise it's the same as a linefeed. The difference boils
1563 * down to whether or not the cursor column is reset.
1564 */
1565hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1566 this.options_.autoCarriageReturn = state;
1567};
1568
1569/**
rginda8ba33642011-12-14 12:31:31 -08001570 * Set the wraparound mode bit.
1571 *
1572 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1573 * to the start of the following row. Otherwise, the cursor is clamped to the
1574 * end of the screen and attempts to write past it are ignored.
1575 *
1576 * Defaults to on.
1577 *
1578 * @param {boolean} state True to set wraparound mode, false to unset.
1579 */
1580hterm.Terminal.prototype.setWraparound = function(state) {
1581 this.options_.wraparound = state;
1582};
1583
1584/**
1585 * Set the reverse-wraparound mode bit.
1586 *
1587 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1588 * to the end of the previous row. Otherwise, the cursor is clamped to column
1589 * 0.
1590 *
1591 * Defaults to off.
1592 *
1593 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1594 */
1595hterm.Terminal.prototype.setReverseWraparound = function(state) {
1596 this.options_.reverseWraparound = state;
1597};
1598
1599/**
1600 * Selects between the primary and alternate screens.
1601 *
1602 * If alternate mode is on, the alternate screen is active. Otherwise the
1603 * primary screen is active.
1604 *
1605 * Swapping screens has no effect on the scrollback buffer.
1606 *
1607 * Each screen maintains its own cursor position.
1608 *
1609 * Defaults to off.
1610 *
1611 * @param {boolean} state True to set alternate mode, false to unset.
1612 */
1613hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001614 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001615 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1616
rginda35c456b2012-02-09 17:29:05 -08001617 if (this.screen_.rowsArray.length &&
1618 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1619 // If the screen changed sizes while we were away, our rowIndexes may
1620 // be incorrect.
1621 var offset = this.scrollbackRows_.length;
1622 var ary = this.screen_.rowsArray;
1623 for (i = 0; i < ary.length; i++) {
1624 ary[i].rowIndex = offset + i;
1625 }
1626 }
rginda8ba33642011-12-14 12:31:31 -08001627
rginda35c456b2012-02-09 17:29:05 -08001628 this.realizeWidth_(this.screenSize.width);
1629 this.realizeHeight_(this.screenSize.height);
1630 this.scrollPort_.syncScrollHeight();
1631 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001632
rginda6d397402012-01-17 10:58:29 -08001633 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001634 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001635};
1636
1637/**
1638 * Set the cursor-blink mode bit.
1639 *
1640 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1641 * a visible cursor does not blink.
1642 *
1643 * You should make sure to turn blinking off if you're going to dispose of a
1644 * terminal, otherwise you'll leak a timeout.
1645 *
1646 * Defaults to on.
1647 *
1648 * @param {boolean} state True to set cursor-blink mode, false to unset.
1649 */
1650hterm.Terminal.prototype.setCursorBlink = function(state) {
1651 this.options_.cursorBlink = state;
1652
1653 if (!state && this.timeouts_.cursorBlink) {
1654 clearTimeout(this.timeouts_.cursorBlink);
1655 delete this.timeouts_.cursorBlink;
1656 }
1657
1658 if (this.options_.cursorVisible)
1659 this.setCursorVisible(true);
1660};
1661
1662/**
1663 * Set the cursor-visible mode bit.
1664 *
1665 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1666 *
1667 * Defaults to on.
1668 *
1669 * @param {boolean} state True to set cursor-visible mode, false to unset.
1670 */
1671hterm.Terminal.prototype.setCursorVisible = function(state) {
1672 this.options_.cursorVisible = state;
1673
1674 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001675 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001676 return;
1677 }
1678
rginda87b86462011-12-14 13:48:03 -08001679 this.syncCursorPosition_();
1680
1681 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001682
1683 if (this.options_.cursorBlink) {
1684 if (this.timeouts_.cursorBlink)
1685 return;
1686
1687 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1688 500);
1689 } else {
1690 if (this.timeouts_.cursorBlink) {
1691 clearTimeout(this.timeouts_.cursorBlink);
1692 delete this.timeouts_.cursorBlink;
1693 }
1694 }
1695};
1696
1697/**
rginda87b86462011-12-14 13:48:03 -08001698 * Synchronizes the visible cursor and document selection with the current
1699 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001700 */
1701hterm.Terminal.prototype.syncCursorPosition_ = function() {
1702 var topRowIndex = this.scrollPort_.getTopRowIndex();
1703 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1704 var cursorRowIndex = this.scrollbackRows_.length +
1705 this.screen_.cursorPosition.row;
1706
1707 if (cursorRowIndex > bottomRowIndex) {
1708 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001709 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001710 return;
1711 }
1712
rginda35c456b2012-02-09 17:29:05 -08001713 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1714 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1715
rginda8ba33642011-12-14 12:31:31 -08001716 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001717 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1718 'px';
1719 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1720 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001721
1722 this.cursorNode_.setAttribute('title',
1723 '(' + this.screen_.cursorPosition.row +
1724 ', ' + this.screen_.cursorPosition.column +
1725 ')');
1726
1727 // Update the caret for a11y purposes.
1728 var selection = this.document_.getSelection();
1729 if (selection && selection.isCollapsed)
1730 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001731};
1732
1733/**
1734 * Synchronizes the visible cursor with the current cursor coordinates.
1735 *
1736 * The sync will happen asynchronously, soon after the call stack winds down.
1737 * Multiple calls will be coalesced into a single sync.
1738 */
1739hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1740 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001741 return;
rginda8ba33642011-12-14 12:31:31 -08001742
1743 var self = this;
1744 this.timeouts_.syncCursor = setTimeout(function() {
1745 self.syncCursorPosition_();
1746 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001747 }, 0);
1748};
1749
rgindacc2996c2012-02-24 14:59:31 -08001750/**
1751 * Show the terminal overlay for a given amount of time.
1752 *
1753 * The terminal overlay appears in inverse video in a large font, centered
1754 * over the terminal. You should probably keep the overlay message brief,
1755 * since it's in a large font and you probably aren't going to check the size
1756 * of the terminal first.
1757 *
1758 * @param {string} msg The text (not HTML) message to display in the overlay.
1759 * @param {number} opt_timeout The amount of time to wait before fading out
1760 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1761 * stay up forever (or until the next overlay).
1762 */
1763hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001764 if (!this.overlayNode_) {
1765 if (!this.div_)
1766 return;
1767
1768 this.overlayNode_ = this.document_.createElement('div');
1769 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001770 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001771 'font-size: xx-large;' +
1772 'opacity: 0.75;' +
1773 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1774 'position: absolute;' +
1775 '-webkit-user-select: none;' +
1776 '-webkit-transition: opacity 180ms ease-in;');
1777 }
1778
rginda9f5222b2012-03-05 11:53:28 -08001779 this.overlayNode_.style.color = this.prefs_.get('background-color');
1780 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1781 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1782
rgindaf0090c92012-02-10 14:58:52 -08001783 this.overlayNode_.textContent = msg;
1784 this.overlayNode_.style.opacity = '0.75';
1785
1786 if (!this.overlayNode_.parentNode)
1787 this.div_.appendChild(this.overlayNode_);
1788
1789 this.overlayNode_.style.top = (
1790 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1791 this.overlayNode_.style.left = (
1792 this.div_.clientWidth - this.overlayNode_.clientWidth -
1793 this.scrollbarWidthPx) / 2;
1794
1795 var self = this;
1796
1797 if (this.overlayTimeout_)
1798 clearTimeout(this.overlayTimeout_);
1799
rgindacc2996c2012-02-24 14:59:31 -08001800 if (opt_timeout === null)
1801 return;
1802
rgindaf0090c92012-02-10 14:58:52 -08001803 this.overlayTimeout_ = setTimeout(function() {
1804 self.overlayNode_.style.opacity = '0';
1805 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001806 if (self.overlayNode_.parentNode)
1807 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001808 self.overlayTimeout_ = null;
1809 self.overlayNode_.style.opacity = '0.75';
1810 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001811 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001812};
1813
1814hterm.Terminal.prototype.overlaySize = function() {
1815 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1816};
1817
rginda87b86462011-12-14 13:48:03 -08001818/**
1819 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1820 *
1821 * @param {string} string The VT string representing the keystroke.
1822 */
1823hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001824 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001825 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1826
1827 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001828};
1829
1830/**
1831 * React when the ScrollPort is scrolled.
1832 */
1833hterm.Terminal.prototype.onScroll_ = function() {
1834 this.scheduleSyncCursorPosition_();
1835};
1836
1837/**
rginda9846e2f2012-01-27 13:53:33 -08001838 * React when text is pasted into the scrollPort.
1839 */
1840hterm.Terminal.prototype.onPaste_ = function(e) {
1841 this.io.onVTKeystroke(e.text);
1842};
1843
1844/**
rginda8ba33642011-12-14 12:31:31 -08001845 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001846 *
1847 * Note: This function should not directly contain code that alters the internal
1848 * state of the terminal. That kind of code belongs in realizeWidth or
1849 * realizeHeight, so that it can be executed synchronously in the case of a
1850 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001851 */
1852hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001853 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001854 this.scrollPort_.characterSize.width);
1855 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1856 this.scrollPort_.characterSize.height);
1857
1858 if (!(columnCount || rowCount)) {
1859 // We avoid these situations since they happen sometimes when the terminal
1860 // gets removed from the document, and we can't deal with that.
1861 return;
1862 }
1863
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001864 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001865 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001866 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001867};
1868
1869/**
1870 * Service the cursor blink timeout.
1871 */
1872hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001873 if (this.cursorNode_.style.opacity == '0') {
1874 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001875 } else {
rginda87b86462011-12-14 13:48:03 -08001876 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001877 }
1878};