blob: 1b843ad3607934d7bc42e9e2ccc26e251c68073b [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 ' +
330 'from normal. Font family is: ' + str);
331 }
rginda9f5222b2012-03-05 11:53:28 -0800332
333 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800334};
335
336/**
rginda87b86462011-12-14 13:48:03 -0800337 * Return a copy of the current cursor position.
338 *
339 * @return {hterm.RowCol} The RowCol object representing the current position.
340 */
341hterm.Terminal.prototype.saveCursor = function() {
342 return this.screen_.cursorPosition.clone();
343};
344
rgindaa19afe22012-01-25 15:40:22 -0800345hterm.Terminal.prototype.getTextAttributes = function() {
346 return this.screen_.textAttributes;
347};
348
rginda87b86462011-12-14 13:48:03 -0800349/**
rginda9846e2f2012-01-27 13:53:33 -0800350 * Change the title of this terminal's window.
351 */
352hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800353 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800354};
355
356/**
rginda87b86462011-12-14 13:48:03 -0800357 * Restore a previously saved cursor position.
358 *
359 * @param {hterm.RowCol} cursor The position to restore.
360 */
361hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800362 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
363 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
364 this.screen_.setCursorPosition(row, column);
365 if (cursor.column > column ||
366 cursor.column == column && cursor.overflow) {
367 this.screen_.cursorPosition.overflow = true;
368 }
rginda87b86462011-12-14 13:48:03 -0800369};
370
371/**
372 * Set the width of the terminal, resizing the UI to match.
373 */
374hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800375 if (columnCount == null) {
376 this.div_.style.width = '100%';
377 return;
378 }
379
rginda35c456b2012-02-09 17:29:05 -0800380 this.div_.style.width = this.scrollPort_.characterSize.width *
381 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400382 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800383 this.scheduleSyncCursorPosition_();
384};
rginda87b86462011-12-14 13:48:03 -0800385
rgindac9bc5502012-01-18 11:48:44 -0800386/**
rginda35c456b2012-02-09 17:29:05 -0800387 * Set the height of the terminal, resizing the UI to match.
388 */
389hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800390 if (rowCount == null) {
391 this.div_.style.height = '100%';
392 return;
393 }
394
rginda35c456b2012-02-09 17:29:05 -0800395 this.div_.style.height =
rginda9f5222b2012-03-05 11:53:28 -0800396 this.scrollPort_.characterSize.height * rowCount + 1 + 'px';
rginda35c456b2012-02-09 17:29:05 -0800397 this.realizeSize_(this.screenSize.width, rowCount);
398 this.scheduleSyncCursorPosition_();
399};
400
401/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400402 * Deal with terminal size changes.
403 *
404 */
405hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
406 if (columnCount != this.screenSize.width)
407 this.realizeWidth_(columnCount);
408
409 if (rowCount != this.screenSize.height)
410 this.realizeHeight_(rowCount);
411
412 // Send new terminal size to plugin.
413 this.io.onTerminalResize(columnCount, rowCount);
414};
415
416/**
rgindac9bc5502012-01-18 11:48:44 -0800417 * Deal with terminal width changes.
418 *
419 * This function does what needs to be done when the terminal width changes
420 * out from under us. It happens here rather than in onResize_() because this
421 * code may need to run synchronously to handle programmatic changes of
422 * terminal width.
423 *
424 * Relying on the browser to send us an async resize event means we may not be
425 * in the correct state yet when the next escape sequence hits.
426 */
427hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
428 var deltaColumns = columnCount - this.screen_.getWidth();
429
rginda87b86462011-12-14 13:48:03 -0800430 this.screenSize.width = columnCount;
431 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800432
433 if (deltaColumns > 0) {
434 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
435 } else {
436 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
437 if (this.tabStops_[i] <= columnCount)
438 break;
439
440 this.tabStops_.pop();
441 }
442 }
443
444 this.screen_.setColumnCount(this.screenSize.width);
445};
446
447/**
448 * Deal with terminal height changes.
449 *
450 * This function does what needs to be done when the terminal height changes
451 * out from under us. It happens here rather than in onResize_() because this
452 * code may need to run synchronously to handle programmatic changes of
453 * terminal height.
454 *
455 * Relying on the browser to send us an async resize event means we may not be
456 * in the correct state yet when the next escape sequence hits.
457 */
458hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
459 var deltaRows = rowCount - this.screen_.getHeight();
460
461 this.screenSize.height = rowCount;
462
463 var cursor = this.saveCursor();
464
465 if (deltaRows < 0) {
466 // Screen got smaller.
467 deltaRows *= -1;
468 while (deltaRows) {
469 var lastRow = this.getRowCount() - 1;
470 if (lastRow - this.scrollbackRows_.length == cursor.row)
471 break;
472
473 if (this.getRowText(lastRow))
474 break;
475
476 this.screen_.popRow();
477 deltaRows--;
478 }
479
480 var ary = this.screen_.shiftRows(deltaRows);
481 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
482
483 // We just removed rows from the top of the screen, we need to update
484 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800485 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800486 } else if (deltaRows > 0) {
487 // Screen got larger.
488
489 if (deltaRows <= this.scrollbackRows_.length) {
490 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
491 var rows = this.scrollbackRows_.splice(
492 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
493 this.screen_.unshiftRows(rows);
494 deltaRows -= scrollbackCount;
495 cursor.row += scrollbackCount;
496 }
497
498 if (deltaRows)
499 this.appendRows_(deltaRows);
500 }
501
rginda35c456b2012-02-09 17:29:05 -0800502 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800503 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800504};
505
506/**
507 * Scroll the terminal to the top of the scrollback buffer.
508 */
509hterm.Terminal.prototype.scrollHome = function() {
510 this.scrollPort_.scrollRowToTop(0);
511};
512
513/**
514 * Scroll the terminal to the end.
515 */
516hterm.Terminal.prototype.scrollEnd = function() {
517 this.scrollPort_.scrollRowToBottom(this.getRowCount());
518};
519
520/**
521 * Scroll the terminal one page up (minus one line) relative to the current
522 * position.
523 */
524hterm.Terminal.prototype.scrollPageUp = function() {
525 var i = this.scrollPort_.getTopRowIndex();
526 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
527};
528
529/**
530 * Scroll the terminal one page down (minus one line) relative to the current
531 * position.
532 */
533hterm.Terminal.prototype.scrollPageDown = function() {
534 var i = this.scrollPort_.getTopRowIndex();
535 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800536};
537
rgindac9bc5502012-01-18 11:48:44 -0800538/**
539 * Full terminal reset.
540 */
rginda87b86462011-12-14 13:48:03 -0800541hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800542 this.clearAllTabStops();
543 this.setDefaultTabStops();
rgindac9bc5502012-01-18 11:48:44 -0800544 this.setVTScrollRegion(null, null);
rginda9ea433c2012-03-16 11:57:00 -0700545
546 this.clearHome(this.primaryScreen_);
547 this.primaryScreen_.textAttributes.reset();
548
549 this.clearHome(this.alternateScreen_);
550 this.alternateScreen_.textAttributes.reset();
551
rgindac9bc5502012-01-18 11:48:44 -0800552 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800553};
554
rgindac9bc5502012-01-18 11:48:44 -0800555/**
556 * Soft terminal reset.
557 */
rginda0f5c0292012-01-13 11:00:13 -0800558hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800559 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800560 this.setCursorVisible(true);
561 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800562};
563
rgindac9bc5502012-01-18 11:48:44 -0800564/**
565 * Move the cursor forward to the next tab stop, or to the last column
566 * if no more tab stops are set.
567 */
568hterm.Terminal.prototype.forwardTabStop = function() {
569 var column = this.screen_.cursorPosition.column;
570
571 for (var i = 0; i < this.tabStops_.length; i++) {
572 if (this.tabStops_[i] > column) {
573 this.setCursorColumn(this.tabStops_[i]);
574 return;
575 }
576 }
577
578 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800579};
580
rgindac9bc5502012-01-18 11:48:44 -0800581/**
582 * Move the cursor backward to the previous tab stop, or to the first column
583 * if no previous tab stops are set.
584 */
585hterm.Terminal.prototype.backwardTabStop = function() {
586 var column = this.screen_.cursorPosition.column;
587
588 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
589 if (this.tabStops_[i] < column) {
590 this.setCursorColumn(this.tabStops_[i]);
591 return;
592 }
593 }
594
595 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800596};
597
rgindac9bc5502012-01-18 11:48:44 -0800598/**
599 * Set a tab stop at the given column.
600 *
601 * @param {int} column Zero based column.
602 */
603hterm.Terminal.prototype.setTabStop = function(column) {
604 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
605 if (this.tabStops_[i] == column)
606 return;
607
608 if (this.tabStops_[i] < column) {
609 this.tabStops_.splice(i + 1, 0, column);
610 return;
611 }
612 }
613
614 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800615};
616
rgindac9bc5502012-01-18 11:48:44 -0800617/**
618 * Clear the tab stop at the current cursor position.
619 *
620 * No effect if there is no tab stop at the current cursor position.
621 */
622hterm.Terminal.prototype.clearTabStopAtCursor = function() {
623 var column = this.screen_.cursorPosition.column;
624
625 var i = this.tabStops_.indexOf(column);
626 if (i == -1)
627 return;
628
629 this.tabStops_.splice(i, 1);
630};
631
632/**
633 * Clear all tab stops.
634 */
635hterm.Terminal.prototype.clearAllTabStops = function() {
636 this.tabStops_.length = 0;
637};
638
639/**
640 * Set up the default tab stops, starting from a given column.
641 *
642 * This sets a tabstop every (column % this.tabWidth) column, starting
643 * from the specified column, or 0 if no column is provided.
644 *
645 * This does not clear the existing tab stops first, use clearAllTabStops
646 * for that.
647 *
648 * @param {int} opt_start Optional starting zero based starting column, useful
649 * for filling out missing tab stops when the terminal is resized.
650 */
651hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
652 var start = opt_start || 0;
653 var w = this.tabWidth;
654 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
655 for (var i = 0; i < stopCount; i++) {
656 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
657 }
rginda87b86462011-12-14 13:48:03 -0800658};
659
rginda6d397402012-01-17 10:58:29 -0800660/**
661 * Save cursor position and attributes.
662 *
663 * TODO(rginda): Save attributes once we support them.
664 */
rginda87b86462011-12-14 13:48:03 -0800665hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800666 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800667 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800668};
669
rginda6d397402012-01-17 10:58:29 -0800670/**
671 * Restore cursor position and attributes.
672 *
673 * TODO(rginda): Restore attributes once we support them.
674 */
rginda87b86462011-12-14 13:48:03 -0800675hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800676 if (this.savedOptions_.cursor)
677 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800678 if (this.savedOptions_.textAttributes)
679 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800680};
681
682/**
683 * Interpret a sequence of characters.
684 *
685 * Incomplete escape sequences are buffered until the next call.
686 *
687 * @param {string} str Sequence of characters to interpret or pass through.
688 */
689hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800690 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800691 this.scheduleSyncCursorPosition_();
692};
693
694/**
695 * Take over the given DIV for use as the terminal display.
696 *
697 * @param {HTMLDivElement} div The div to use as the terminal display.
698 */
699hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800700 this.div_ = div;
701
rginda8ba33642011-12-14 12:31:31 -0800702 this.scrollPort_.decorate(div);
rgindaf7521392012-02-28 17:20:34 -0800703
rginda9f5222b2012-03-05 11:53:28 -0800704 this.setFontSize(this.prefs_.get('font-size'));
705 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800706
rginda8ba33642011-12-14 12:31:31 -0800707 this.document_ = this.scrollPort_.getDocument();
708
rginda8ba33642011-12-14 12:31:31 -0800709 this.cursorNode_ = this.document_.createElement('div');
710 this.cursorNode_.style.cssText =
711 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800712 'top: -99px;' +
713 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800714 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
715 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800716 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800717 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800718 this.document_.body.appendChild(this.cursorNode_);
719
720 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800721
rginda87b86462011-12-14 13:48:03 -0800722 var link = this.document_.createElement('link');
723 link.setAttribute('href', '../css/dialogs.css');
724 link.setAttribute('rel', 'stylesheet');
725 this.document_.head.appendChild(link);
726
727 this.alertDialog = new AlertDialog(this.document_.body);
728 this.promptDialog = new PromptDialog(this.document_.body);
729 this.confirmDialog = new ConfirmDialog(this.document_.body);
730
731 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800732 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800733};
734
735hterm.Terminal.prototype.getDocument = function() {
736 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800737};
738
739/**
740 * Return the HTML Element for a given row index.
741 *
742 * This is a method from the RowProvider interface. The ScrollPort uses
743 * it to fetch rows on demand as they are scrolled into view.
744 *
745 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
746 * pairs to conserve memory.
747 *
748 * @param {integer} index The zero-based row index, measured relative to the
749 * start of the scrollback buffer. On-screen rows will always have the
750 * largest indicies.
751 * @return {HTMLElement} The 'x-row' element containing for the requested row.
752 */
753hterm.Terminal.prototype.getRowNode = function(index) {
754 if (index < this.scrollbackRows_.length)
755 return this.scrollbackRows_[index];
756
757 var screenIndex = index - this.scrollbackRows_.length;
758 return this.screen_.rowsArray[screenIndex];
759};
760
761/**
762 * Return the text content for a given range of rows.
763 *
764 * This is a method from the RowProvider interface. The ScrollPort uses
765 * it to fetch text content on demand when the user attempts to copy their
766 * selection to the clipboard.
767 *
768 * @param {integer} start The zero-based row index to start from, measured
769 * relative to the start of the scrollback buffer. On-screen rows will
770 * always have the largest indicies.
771 * @param {integer} end The zero-based row index to end on, measured
772 * relative to the start of the scrollback buffer.
773 * @return {string} A single string containing the text value of the range of
774 * rows. Lines will be newline delimited, with no trailing newline.
775 */
776hterm.Terminal.prototype.getRowsText = function(start, end) {
777 var ary = [];
778 for (var i = start; i < end; i++) {
779 var node = this.getRowNode(i);
780 ary.push(node.textContent);
781 }
782
783 return ary.join('\n');
784};
785
786/**
787 * Return the text content for a given row.
788 *
789 * This is a method from the RowProvider interface. The ScrollPort uses
790 * it to fetch text content on demand when the user attempts to copy their
791 * selection to the clipboard.
792 *
793 * @param {integer} index The zero-based row index to return, measured
794 * relative to the start of the scrollback buffer. On-screen rows will
795 * always have the largest indicies.
796 * @return {string} A string containing the text value of the selected row.
797 */
798hterm.Terminal.prototype.getRowText = function(index) {
799 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800800 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800801};
802
803/**
804 * Return the total number of rows in the addressable screen and in the
805 * scrollback buffer of this terminal.
806 *
807 * This is a method from the RowProvider interface. The ScrollPort uses
808 * it to compute the size of the scrollbar.
809 *
810 * @return {integer} The number of rows in this terminal.
811 */
812hterm.Terminal.prototype.getRowCount = function() {
813 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
814};
815
816/**
817 * Create DOM nodes for new rows and append them to the end of the terminal.
818 *
819 * This is the only correct way to add a new DOM node for a row. Notice that
820 * the new row is appended to the bottom of the list of rows, and does not
821 * require renumbering (of the rowIndex property) of previous rows.
822 *
823 * If you think you want a new blank row somewhere in the middle of the
824 * terminal, look into moveRows_().
825 *
826 * This method does not pay attention to vtScrollTop/Bottom, since you should
827 * be using moveRows() in cases where they would matter.
828 *
829 * The cursor will be positioned at column 0 of the first inserted line.
830 */
831hterm.Terminal.prototype.appendRows_ = function(count) {
832 var cursorRow = this.screen_.rowsArray.length;
833 var offset = this.scrollbackRows_.length + cursorRow;
834 for (var i = 0; i < count; i++) {
835 var row = this.document_.createElement('x-row');
836 row.appendChild(this.document_.createTextNode(''));
837 row.rowIndex = offset + i;
838 this.screen_.pushRow(row);
839 }
840
841 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
842 if (extraRows > 0) {
843 var ary = this.screen_.shiftRows(extraRows);
844 Array.prototype.push.apply(this.scrollbackRows_, ary);
845 this.scheduleScrollDown_();
846 }
847
848 if (cursorRow >= this.screen_.rowsArray.length)
849 cursorRow = this.screen_.rowsArray.length - 1;
850
rginda87b86462011-12-14 13:48:03 -0800851 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800852};
853
854/**
855 * Relocate rows from one part of the addressable screen to another.
856 *
857 * This is used to recycle rows during VT scrolls (those which are driven
858 * by VT commands, rather than by the user manipulating the scrollbar.)
859 *
860 * In this case, the blank lines scrolled into the scroll region are made of
861 * the nodes we scrolled off. These have their rowIndex properties carefully
862 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800863 */
864hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
865 var ary = this.screen_.removeRows(fromIndex, count);
866 this.screen_.insertRows(toIndex, ary);
867
868 var start, end;
869 if (fromIndex < toIndex) {
870 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800871 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800872 } else {
873 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800874 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800875 }
876
877 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800878 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800879};
880
881/**
882 * Renumber the rowIndex property of the given range of rows.
883 *
884 * The start and end indicies are relative to the screen, not the scrollback.
885 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800886 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800887 * no need to renumber scrollback rows.
888 */
889hterm.Terminal.prototype.renumberRows_ = function(start, end) {
890 var offset = this.scrollbackRows_.length;
891 for (var i = start; i < end; i++) {
892 this.screen_.rowsArray[i].rowIndex = offset + i;
893 }
894};
895
896/**
897 * Print a string to the terminal.
898 *
899 * This respects the current insert and wraparound modes. It will add new lines
900 * to the end of the terminal, scrolling off the top into the scrollback buffer
901 * if necessary.
902 *
903 * The string is *not* parsed for escape codes. Use the interpret() method if
904 * that's what you're after.
905 *
906 * @param{string} str The string to print.
907 */
908hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800909 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
910 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800911
rgindaa19afe22012-01-25 15:40:22 -0800912 if (this.options_.insertMode) {
913 this.screen_.insertString(str);
914 } else {
915 this.screen_.overwriteString(str);
916 }
917
918 var overflow = this.screen_.maybeClipCurrentRow();
919
920 if (this.options_.wraparound && overflow) {
921 var lastColumn;
922
923 do {
rginda35c456b2012-02-09 17:29:05 -0800924 this.newLine();
925 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800926
927 if (!this.options_.insertMode)
928 this.screen_.deleteChars(overflow.characterLength);
929
930 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800931
932 overflow = this.screen_.maybeClipCurrentRow();
933 } while (overflow);
934
935 this.setCursorColumn(lastColumn);
936 }
rginda8ba33642011-12-14 12:31:31 -0800937
938 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800939
rginda9f5222b2012-03-05 11:53:28 -0800940 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -0800941 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800942};
943
944/**
rginda87b86462011-12-14 13:48:03 -0800945 * Set the VT scroll region.
946 *
rginda87b86462011-12-14 13:48:03 -0800947 * This also resets the cursor position to the absolute (0, 0) position, since
948 * that's what xterm appears to do.
949 *
950 * @param {integer} scrollTop The zero-based top of the scroll region.
951 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
952 * inclusive.
953 */
954hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
955 this.vtScrollTop_ = scrollTop;
956 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800957};
958
959/**
rginda8ba33642011-12-14 12:31:31 -0800960 * Return the top row index according to the VT.
961 *
962 * This will return 0 unless the terminal has been told to restrict scrolling
963 * to some lower row. It is used for some VT cursor positioning and scrolling
964 * commands.
965 *
966 * @return {integer} The topmost row in the terminal's scroll region.
967 */
968hterm.Terminal.prototype.getVTScrollTop = function() {
969 if (this.vtScrollTop_ != null)
970 return this.vtScrollTop_;
971
972 return 0;
rginda87b86462011-12-14 13:48:03 -0800973};
rginda8ba33642011-12-14 12:31:31 -0800974
975/**
976 * Return the bottom row index according to the VT.
977 *
978 * This will return the height of the terminal unless the it has been told to
979 * restrict scrolling to some higher row. It is used for some VT cursor
980 * positioning and scrolling commands.
981 *
982 * @return {integer} The bottommost row in the terminal's scroll region.
983 */
984hterm.Terminal.prototype.getVTScrollBottom = function() {
985 if (this.vtScrollBottom_ != null)
986 return this.vtScrollBottom_;
987
rginda87b86462011-12-14 13:48:03 -0800988 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800989}
990
991/**
992 * Process a '\n' character.
993 *
994 * If the cursor is on the final row of the terminal this will append a new
995 * blank row to the screen and scroll the topmost row into the scrollback
996 * buffer.
997 *
998 * Otherwise, this moves the cursor to column zero of the next row.
999 */
1000hterm.Terminal.prototype.newLine = function() {
1001 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001002 // If we're at the end of the screen we need to append a new line and
1003 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001004 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001005 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1006 // End of the scroll region does not affect the scrollback buffer.
1007 this.vtScrollUp(1);
1008 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001009 } else {
rginda87b86462011-12-14 13:48:03 -08001010 // Anywhere else in the screen just moves the cursor.
1011 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001012 }
1013};
1014
1015/**
1016 * Like newLine(), except maintain the cursor column.
1017 */
1018hterm.Terminal.prototype.lineFeed = function() {
1019 var column = this.screen_.cursorPosition.column;
1020 this.newLine();
1021 this.setCursorColumn(column);
1022};
1023
1024/**
rginda87b86462011-12-14 13:48:03 -08001025 * If autoCarriageReturn is set then newLine(), else lineFeed().
1026 */
1027hterm.Terminal.prototype.formFeed = function() {
1028 if (this.options_.autoCarriageReturn) {
1029 this.newLine();
1030 } else {
1031 this.lineFeed();
1032 }
1033};
1034
1035/**
1036 * Move the cursor up one row, possibly inserting a blank line.
1037 *
1038 * The cursor column is not changed.
1039 */
1040hterm.Terminal.prototype.reverseLineFeed = function() {
1041 var scrollTop = this.getVTScrollTop();
1042 var currentRow = this.screen_.cursorPosition.row;
1043
1044 if (currentRow == scrollTop) {
1045 this.insertLines(1);
1046 } else {
1047 this.setAbsoluteCursorRow(currentRow - 1);
1048 }
1049};
1050
1051/**
rginda8ba33642011-12-14 12:31:31 -08001052 * Replace all characters to the left of the current cursor with the space
1053 * character.
1054 *
1055 * TODO(rginda): This should probably *remove* the characters (not just replace
1056 * with a space) if there are no characters at or beyond the current cursor
1057 * position. Once it does that, it'll have the same text-attribute related
1058 * issues as hterm.Screen.prototype.clearCursorRow :/
1059 */
1060hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001061 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001062 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001063 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1064 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001065};
1066
1067/**
1068 * Erase a given number of characters to the right of the cursor, shifting
1069 * remaining characters to the left.
1070 *
1071 * The cursor position is unchanged.
1072 *
1073 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1074 * the end of the text.
1075 *
1076 * TODO(rginda): This likely has text-attribute related troubles similar to the
1077 * todo on hterm.Screen.prototype.clearCursorRow.
1078 */
1079hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001080 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001081
rginda87b86462011-12-14 13:48:03 -08001082 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001083 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1084 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001085 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001086};
1087
1088/**
1089 * Erase the current line.
1090 *
1091 * The cursor position is unchanged.
1092 *
1093 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1094 * has a text-attribute related TODO.
1095 */
1096hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001097 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001098 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001099 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001100};
1101
1102/**
1103 * Erase all characters from the start of the scroll region to the current
1104 * cursor position.
1105 *
1106 * The cursor position is unchanged.
1107 *
1108 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1109 * has a text-attribute related TODO.
1110 */
1111hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001112 var cursor = this.saveCursor();
1113
1114 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001115
1116 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001117 for (var i = top; i < cursor.row; i++) {
1118 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001119 this.screen_.clearCursorRow();
1120 }
1121
rginda87b86462011-12-14 13:48:03 -08001122 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001123};
1124
1125/**
1126 * Erase all characters from the current cursor position to the end of the
1127 * scroll region.
1128 *
1129 * The cursor position is unchanged.
1130 *
1131 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1132 * has a text-attribute related TODO.
1133 */
1134hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001135 var cursor = this.saveCursor();
1136
1137 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001138
1139 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001140 for (var i = cursor.row + 1; i <= bottom; i++) {
1141 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001142 this.screen_.clearCursorRow();
1143 }
1144
rginda87b86462011-12-14 13:48:03 -08001145 this.restoreCursor(cursor);
1146};
1147
1148/**
1149 * Fill the terminal with a given character.
1150 *
1151 * This methods does not respect the VT scroll region.
1152 *
1153 * @param {string} ch The character to use for the fill.
1154 */
1155hterm.Terminal.prototype.fill = function(ch) {
1156 var cursor = this.saveCursor();
1157
1158 this.setAbsoluteCursorPosition(0, 0);
1159 for (var row = 0; row < this.screenSize.height; row++) {
1160 for (var col = 0; col < this.screenSize.width; col++) {
1161 this.setAbsoluteCursorPosition(row, col);
1162 this.screen_.overwriteString(ch);
1163 }
1164 }
1165
1166 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001167};
1168
1169/**
rginda9ea433c2012-03-16 11:57:00 -07001170 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001171 *
rginda9ea433c2012-03-16 11:57:00 -07001172 * This does not respect the scroll region.
1173 *
1174 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1175 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001176 *
1177 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1178 * has a text-attribute related TODO.
1179 */
rginda9ea433c2012-03-16 11:57:00 -07001180hterm.Terminal.prototype.clearHome = function(opt_screen) {
1181 var screen = opt_screen || this.screen_;
1182 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001183
rgindae4d29232012-01-19 10:47:13 -08001184 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001185 screen.setCursorPosition(i, 0);
1186 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001187 }
1188
rginda9ea433c2012-03-16 11:57:00 -07001189 screen.setCursorPosition(0, 0);
1190};
1191
1192/**
1193 * Erase the entire display without changing the cursor position.
1194 *
1195 * The cursor position is unchanged. This does not respect the scroll
1196 * region.
1197 *
1198 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1199 * to the current screen.
1200 *
1201 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1202 * has a text-attribute related TODO.
1203 */
1204hterm.Terminal.prototype.clear = function(opt_screen) {
1205 var screen = opt_screen || this.screen_;
1206 var cursor = screen.cursorPosition.clone();
1207 this.clearHome(screen);
1208 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001209};
1210
1211/**
1212 * VT command to insert lines at the current cursor row.
1213 *
1214 * This respects the current scroll region. Rows pushed off the bottom are
1215 * lost (they won't show up in the scrollback buffer).
1216 *
1217 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1218 * has a text-attribute related TODO.
1219 *
1220 * @param {integer} count The number of lines to insert.
1221 */
1222hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001223 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001224
1225 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001226 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001227
rgindae4d29232012-01-19 10:47:13 -08001228 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001229 if (start != cursor.row)
1230 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001231
1232 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001233 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001234 this.screen_.clearCursorRow();
1235 }
1236
rginda87b86462011-12-14 13:48:03 -08001237 cursor.column = 0;
1238 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001239};
1240
1241/**
1242 * VT command to delete lines at the current cursor row.
1243 *
1244 * New rows are added to the bottom of scroll region to take their place. New
1245 * rows are strictly there to take up space and have no content or style.
1246 */
1247hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001248 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001249
rginda87b86462011-12-14 13:48:03 -08001250 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001251 var bottom = this.getVTScrollBottom();
1252
rginda87b86462011-12-14 13:48:03 -08001253 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001254 count = Math.min(count, maxCount);
1255
rginda87b86462011-12-14 13:48:03 -08001256 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001257 if (count != maxCount)
1258 this.moveRows_(top, count, moveStart);
1259
1260 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001261 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001262 this.screen_.clearCursorRow();
1263 }
1264
rginda87b86462011-12-14 13:48:03 -08001265 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001266};
1267
1268/**
1269 * Inserts the given number of spaces at the current cursor position.
1270 *
rginda87b86462011-12-14 13:48:03 -08001271 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001272 */
1273hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001274 var cursor = this.saveCursor();
1275
rginda0f5c0292012-01-13 11:00:13 -08001276 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001277 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001278 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001279
1280 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001281};
1282
1283/**
1284 * Forward-delete the specified number of characters starting at the cursor
1285 * position.
1286 *
1287 * @param {integer} count The number of characters to delete.
1288 */
1289hterm.Terminal.prototype.deleteChars = function(count) {
1290 this.screen_.deleteChars(count);
1291};
1292
1293/**
1294 * Shift rows in the scroll region upwards by a given number of lines.
1295 *
1296 * New rows are inserted at the bottom of the scroll region to fill the
1297 * vacated rows. The new rows not filled out with the current text attributes.
1298 *
1299 * This function does not affect the scrollback rows at all. Rows shifted
1300 * off the top are lost.
1301 *
rginda87b86462011-12-14 13:48:03 -08001302 * The cursor position is not altered.
1303 *
rginda8ba33642011-12-14 12:31:31 -08001304 * @param {integer} count The number of rows to scroll.
1305 */
1306hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001307 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001308
rginda87b86462011-12-14 13:48:03 -08001309 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001310 this.deleteLines(count);
1311
rginda87b86462011-12-14 13:48:03 -08001312 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001313};
1314
1315/**
1316 * Shift rows below the cursor down by a given number of lines.
1317 *
1318 * This function respects the current scroll region.
1319 *
1320 * New rows are inserted at the top of the scroll region to fill the
1321 * vacated rows. The new rows not filled out with the current text attributes.
1322 *
1323 * This function does not affect the scrollback rows at all. Rows shifted
1324 * off the bottom are lost.
1325 *
1326 * @param {integer} count The number of rows to scroll.
1327 */
1328hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001329 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001330
rginda87b86462011-12-14 13:48:03 -08001331 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001332 this.insertLines(opt_count);
1333
rginda87b86462011-12-14 13:48:03 -08001334 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001335};
1336
rginda87b86462011-12-14 13:48:03 -08001337
rginda8ba33642011-12-14 12:31:31 -08001338/**
1339 * Set the cursor position.
1340 *
1341 * The cursor row is relative to the scroll region if the terminal has
1342 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1343 *
1344 * @param {integer} row The new zero-based cursor row.
1345 * @param {integer} row The new zero-based cursor column.
1346 */
1347hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1348 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001349 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001350 } else {
rginda87b86462011-12-14 13:48:03 -08001351 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001352 }
rginda87b86462011-12-14 13:48:03 -08001353};
rginda8ba33642011-12-14 12:31:31 -08001354
rginda87b86462011-12-14 13:48:03 -08001355hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1356 var scrollTop = this.getVTScrollTop();
1357 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001358 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001359 this.screen_.setCursorPosition(row, column);
1360};
1361
1362hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001363 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1364 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001365 this.screen_.setCursorPosition(row, column);
1366};
1367
1368/**
1369 * Set the cursor column.
1370 *
1371 * @param {integer} column The new zero-based cursor column.
1372 */
1373hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001374 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001375};
1376
1377/**
1378 * Return the cursor column.
1379 *
1380 * @return {integer} The zero-based cursor column.
1381 */
1382hterm.Terminal.prototype.getCursorColumn = function() {
1383 return this.screen_.cursorPosition.column;
1384};
1385
1386/**
1387 * Set the cursor row.
1388 *
1389 * The cursor row is relative to the scroll region if the terminal has
1390 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1391 *
1392 * @param {integer} row The new cursor row.
1393 */
rginda87b86462011-12-14 13:48:03 -08001394hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1395 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001396};
1397
1398/**
1399 * Return the cursor row.
1400 *
1401 * @return {integer} The zero-based cursor row.
1402 */
1403hterm.Terminal.prototype.getCursorRow = function(row) {
1404 return this.screen_.cursorPosition.row;
1405};
1406
1407/**
1408 * Request that the ScrollPort redraw itself soon.
1409 *
1410 * The redraw will happen asynchronously, soon after the call stack winds down.
1411 * Multiple calls will be coalesced into a single redraw.
1412 */
1413hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001414 if (this.timeouts_.redraw)
1415 return;
rginda8ba33642011-12-14 12:31:31 -08001416
1417 var self = this;
rginda87b86462011-12-14 13:48:03 -08001418 this.timeouts_.redraw = setTimeout(function() {
1419 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001420 self.scrollPort_.redraw_();
1421 }, 0);
1422};
1423
1424/**
1425 * Request that the ScrollPort be scrolled to the bottom.
1426 *
1427 * The scroll will happen asynchronously, soon after the call stack winds down.
1428 * Multiple calls will be coalesced into a single scroll.
1429 *
1430 * This affects the scrollbar position of the ScrollPort, and has nothing to
1431 * do with the VT scroll commands.
1432 */
1433hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1434 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001435 return;
rginda8ba33642011-12-14 12:31:31 -08001436
1437 var self = this;
1438 this.timeouts_.scrollDown = setTimeout(function() {
1439 delete self.timeouts_.scrollDown;
1440 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1441 }, 10);
1442};
1443
1444/**
1445 * Move the cursor up a specified number of rows.
1446 *
1447 * @param {integer} count The number of rows to move the cursor.
1448 */
1449hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001450 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001451};
1452
1453/**
1454 * Move the cursor down a specified number of rows.
1455 *
1456 * @param {integer} count The number of rows to move the cursor.
1457 */
1458hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001459 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001460 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1461 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1462 this.screenSize.height - 1);
1463
1464 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1465 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001466 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001467};
1468
1469/**
1470 * Move the cursor left a specified number of columns.
1471 *
1472 * @param {integer} count The number of columns to move the cursor.
1473 */
1474hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001475 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001476};
1477
1478/**
1479 * Move the cursor right a specified number of columns.
1480 *
1481 * @param {integer} count The number of columns to move the cursor.
1482 */
1483hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001484 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001485 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001486 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001487 this.setCursorColumn(column);
1488};
1489
1490/**
1491 * Reverse the foreground and background colors of the terminal.
1492 *
1493 * This only affects text that was drawn with no attributes.
1494 *
1495 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1496 * been drawn with attributes that happen to coincide with the default
1497 * 'no-attribute' colors. My guess is probably not.
1498 */
1499hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001500 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001501 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001502 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1503 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001504 } else {
rginda9f5222b2012-03-05 11:53:28 -08001505 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1506 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001507 }
1508};
1509
1510/**
rginda87b86462011-12-14 13:48:03 -08001511 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001512 */
1513hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001514 if (this.bellAudio_.getAttribute('src'))
1515 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001516
rginda6d397402012-01-17 10:58:29 -08001517 this.cursorNode_.style.backgroundColor =
1518 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001519
1520 var self = this;
1521 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001522 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001523 }, 200);
rginda87b86462011-12-14 13:48:03 -08001524};
1525
1526/**
rginda8ba33642011-12-14 12:31:31 -08001527 * Set the origin mode bit.
1528 *
1529 * If origin mode is on, certain VT cursor and scrolling commands measure their
1530 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1531 * to the top of the addressable screen.
1532 *
1533 * Defaults to off.
1534 *
1535 * @param {boolean} state True to set origin mode, false to unset.
1536 */
1537hterm.Terminal.prototype.setOriginMode = function(state) {
1538 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001539 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001540};
1541
1542/**
1543 * Set the insert mode bit.
1544 *
1545 * If insert mode is on, existing text beyond the cursor position will be
1546 * shifted right to make room for new text. Otherwise, new text overwrites
1547 * any existing text.
1548 *
1549 * Defaults to off.
1550 *
1551 * @param {boolean} state True to set insert mode, false to unset.
1552 */
1553hterm.Terminal.prototype.setInsertMode = function(state) {
1554 this.options_.insertMode = state;
1555};
1556
1557/**
rginda87b86462011-12-14 13:48:03 -08001558 * Set the auto carriage return bit.
1559 *
1560 * If auto carriage return is on then a formfeed character is interpreted
1561 * as a newline, otherwise it's the same as a linefeed. The difference boils
1562 * down to whether or not the cursor column is reset.
1563 */
1564hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1565 this.options_.autoCarriageReturn = state;
1566};
1567
1568/**
rginda8ba33642011-12-14 12:31:31 -08001569 * Set the wraparound mode bit.
1570 *
1571 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1572 * to the start of the following row. Otherwise, the cursor is clamped to the
1573 * end of the screen and attempts to write past it are ignored.
1574 *
1575 * Defaults to on.
1576 *
1577 * @param {boolean} state True to set wraparound mode, false to unset.
1578 */
1579hterm.Terminal.prototype.setWraparound = function(state) {
1580 this.options_.wraparound = state;
1581};
1582
1583/**
1584 * Set the reverse-wraparound mode bit.
1585 *
1586 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1587 * to the end of the previous row. Otherwise, the cursor is clamped to column
1588 * 0.
1589 *
1590 * Defaults to off.
1591 *
1592 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1593 */
1594hterm.Terminal.prototype.setReverseWraparound = function(state) {
1595 this.options_.reverseWraparound = state;
1596};
1597
1598/**
1599 * Selects between the primary and alternate screens.
1600 *
1601 * If alternate mode is on, the alternate screen is active. Otherwise the
1602 * primary screen is active.
1603 *
1604 * Swapping screens has no effect on the scrollback buffer.
1605 *
1606 * Each screen maintains its own cursor position.
1607 *
1608 * Defaults to off.
1609 *
1610 * @param {boolean} state True to set alternate mode, false to unset.
1611 */
1612hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001613 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001614 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1615
rginda35c456b2012-02-09 17:29:05 -08001616 if (this.screen_.rowsArray.length &&
1617 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1618 // If the screen changed sizes while we were away, our rowIndexes may
1619 // be incorrect.
1620 var offset = this.scrollbackRows_.length;
1621 var ary = this.screen_.rowsArray;
1622 for (i = 0; i < ary.length; i++) {
1623 ary[i].rowIndex = offset + i;
1624 }
1625 }
rginda8ba33642011-12-14 12:31:31 -08001626
rginda35c456b2012-02-09 17:29:05 -08001627 this.realizeWidth_(this.screenSize.width);
1628 this.realizeHeight_(this.screenSize.height);
1629 this.scrollPort_.syncScrollHeight();
1630 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001631
rginda6d397402012-01-17 10:58:29 -08001632 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001633 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001634};
1635
1636/**
1637 * Set the cursor-blink mode bit.
1638 *
1639 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1640 * a visible cursor does not blink.
1641 *
1642 * You should make sure to turn blinking off if you're going to dispose of a
1643 * terminal, otherwise you'll leak a timeout.
1644 *
1645 * Defaults to on.
1646 *
1647 * @param {boolean} state True to set cursor-blink mode, false to unset.
1648 */
1649hterm.Terminal.prototype.setCursorBlink = function(state) {
1650 this.options_.cursorBlink = state;
1651
1652 if (!state && this.timeouts_.cursorBlink) {
1653 clearTimeout(this.timeouts_.cursorBlink);
1654 delete this.timeouts_.cursorBlink;
1655 }
1656
1657 if (this.options_.cursorVisible)
1658 this.setCursorVisible(true);
1659};
1660
1661/**
1662 * Set the cursor-visible mode bit.
1663 *
1664 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1665 *
1666 * Defaults to on.
1667 *
1668 * @param {boolean} state True to set cursor-visible mode, false to unset.
1669 */
1670hterm.Terminal.prototype.setCursorVisible = function(state) {
1671 this.options_.cursorVisible = state;
1672
1673 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001674 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001675 return;
1676 }
1677
rginda87b86462011-12-14 13:48:03 -08001678 this.syncCursorPosition_();
1679
1680 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001681
1682 if (this.options_.cursorBlink) {
1683 if (this.timeouts_.cursorBlink)
1684 return;
1685
1686 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1687 500);
1688 } else {
1689 if (this.timeouts_.cursorBlink) {
1690 clearTimeout(this.timeouts_.cursorBlink);
1691 delete this.timeouts_.cursorBlink;
1692 }
1693 }
1694};
1695
1696/**
rginda87b86462011-12-14 13:48:03 -08001697 * Synchronizes the visible cursor and document selection with the current
1698 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001699 */
1700hterm.Terminal.prototype.syncCursorPosition_ = function() {
1701 var topRowIndex = this.scrollPort_.getTopRowIndex();
1702 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1703 var cursorRowIndex = this.scrollbackRows_.length +
1704 this.screen_.cursorPosition.row;
1705
1706 if (cursorRowIndex > bottomRowIndex) {
1707 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001708 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001709 return;
1710 }
1711
rginda35c456b2012-02-09 17:29:05 -08001712 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1713 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1714
rginda8ba33642011-12-14 12:31:31 -08001715 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001716 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1717 'px';
1718 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1719 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001720
1721 this.cursorNode_.setAttribute('title',
1722 '(' + this.screen_.cursorPosition.row +
1723 ', ' + this.screen_.cursorPosition.column +
1724 ')');
1725
1726 // Update the caret for a11y purposes.
1727 var selection = this.document_.getSelection();
1728 if (selection && selection.isCollapsed)
1729 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001730};
1731
1732/**
1733 * Synchronizes the visible cursor with the current cursor coordinates.
1734 *
1735 * The sync will happen asynchronously, soon after the call stack winds down.
1736 * Multiple calls will be coalesced into a single sync.
1737 */
1738hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1739 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001740 return;
rginda8ba33642011-12-14 12:31:31 -08001741
1742 var self = this;
1743 this.timeouts_.syncCursor = setTimeout(function() {
1744 self.syncCursorPosition_();
1745 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001746 }, 0);
1747};
1748
rgindacc2996c2012-02-24 14:59:31 -08001749/**
1750 * Show the terminal overlay for a given amount of time.
1751 *
1752 * The terminal overlay appears in inverse video in a large font, centered
1753 * over the terminal. You should probably keep the overlay message brief,
1754 * since it's in a large font and you probably aren't going to check the size
1755 * of the terminal first.
1756 *
1757 * @param {string} msg The text (not HTML) message to display in the overlay.
1758 * @param {number} opt_timeout The amount of time to wait before fading out
1759 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1760 * stay up forever (or until the next overlay).
1761 */
1762hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001763 if (!this.overlayNode_) {
1764 if (!this.div_)
1765 return;
1766
1767 this.overlayNode_ = this.document_.createElement('div');
1768 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001769 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001770 'font-size: xx-large;' +
1771 'opacity: 0.75;' +
1772 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1773 'position: absolute;' +
1774 '-webkit-user-select: none;' +
1775 '-webkit-transition: opacity 180ms ease-in;');
1776 }
1777
rginda9f5222b2012-03-05 11:53:28 -08001778 this.overlayNode_.style.color = this.prefs_.get('background-color');
1779 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1780 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1781
rgindaf0090c92012-02-10 14:58:52 -08001782 this.overlayNode_.textContent = msg;
1783 this.overlayNode_.style.opacity = '0.75';
1784
1785 if (!this.overlayNode_.parentNode)
1786 this.div_.appendChild(this.overlayNode_);
1787
1788 this.overlayNode_.style.top = (
1789 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1790 this.overlayNode_.style.left = (
1791 this.div_.clientWidth - this.overlayNode_.clientWidth -
1792 this.scrollbarWidthPx) / 2;
1793
1794 var self = this;
1795
1796 if (this.overlayTimeout_)
1797 clearTimeout(this.overlayTimeout_);
1798
rgindacc2996c2012-02-24 14:59:31 -08001799 if (opt_timeout === null)
1800 return;
1801
rgindaf0090c92012-02-10 14:58:52 -08001802 this.overlayTimeout_ = setTimeout(function() {
1803 self.overlayNode_.style.opacity = '0';
1804 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001805 if (self.overlayNode_.parentNode)
1806 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001807 self.overlayTimeout_ = null;
1808 self.overlayNode_.style.opacity = '0.75';
1809 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001810 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001811};
1812
1813hterm.Terminal.prototype.overlaySize = function() {
1814 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1815};
1816
rginda87b86462011-12-14 13:48:03 -08001817/**
1818 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1819 *
1820 * @param {string} string The VT string representing the keystroke.
1821 */
1822hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001823 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001824 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1825
1826 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001827};
1828
1829/**
1830 * React when the ScrollPort is scrolled.
1831 */
1832hterm.Terminal.prototype.onScroll_ = function() {
1833 this.scheduleSyncCursorPosition_();
1834};
1835
1836/**
rginda9846e2f2012-01-27 13:53:33 -08001837 * React when text is pasted into the scrollPort.
1838 */
1839hterm.Terminal.prototype.onPaste_ = function(e) {
1840 this.io.onVTKeystroke(e.text);
1841};
1842
1843/**
rginda8ba33642011-12-14 12:31:31 -08001844 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001845 *
1846 * Note: This function should not directly contain code that alters the internal
1847 * state of the terminal. That kind of code belongs in realizeWidth or
1848 * realizeHeight, so that it can be executed synchronously in the case of a
1849 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001850 */
1851hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001852 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001853 this.scrollPort_.characterSize.width);
1854 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1855 this.scrollPort_.characterSize.height);
1856
1857 if (!(columnCount || rowCount)) {
1858 // We avoid these situations since they happen sometimes when the terminal
1859 // gets removed from the document, and we can't deal with that.
1860 return;
1861 }
1862
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001863 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001864 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001865 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001866};
1867
1868/**
1869 * Service the cursor blink timeout.
1870 */
1871hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001872 if (this.cursorNode_.style.opacity == '0') {
1873 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001874 } else {
rginda87b86462011-12-14 13:48:03 -08001875 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001876 }
1877};