rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 5 | 'use strict'; |
| 6 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 7 | /** |
| 8 | * @fileoverview This class represents a single terminal screen full of text. |
| 9 | * |
| 10 | * It maintains the current cursor position and has basic methods for text |
| 11 | * insert and overwrite, and adding or removing rows from the screen. |
| 12 | * |
| 13 | * This class has no knowledge of the scrollback buffer. |
| 14 | * |
| 15 | * The number of rows on the screen is determined only by the number of rows |
| 16 | * that the caller inserts into the screen. If a caller wants to ensure a |
| 17 | * constant number of rows on the screen, it's their responsibility to remove a |
| 18 | * row for each row inserted. |
| 19 | * |
| 20 | * The screen width, in contrast, is enforced locally. |
| 21 | * |
| 22 | * |
| 23 | * In practice... |
| 24 | * - The hterm.Terminal class holds two hterm.Screen instances. One for the |
| 25 | * primary screen and one for the alternate screen. |
| 26 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 27 | * - The html.Screen class only cares that rows are HTML Elements. In the |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 28 | * larger context of hterm, however, the rows happen to be displayed by an |
| 29 | * hterm.ScrollPort and have to follow a few rules as a result. Each |
| 30 | * row must be rooted by the custom HTML tag 'x-row', and each must have a |
| 31 | * rowIndex property that corresponds to the index of the row in the context |
| 32 | * of the scrollback buffer. These invariants are enforced by hterm.Terminal |
| 33 | * because that is the class using the hterm.Screen in the context of an |
| 34 | * hterm.ScrollPort. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Create a new screen instance. |
| 39 | * |
| 40 | * The screen initially has no rows and a maximum column count of 0. |
| 41 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 42 | * @param {number=} columnCount The maximum number of columns for this |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 43 | * screen. See insertString() and overwriteString() for information about |
| 44 | * what happens when too many characters are added too a row. Defaults to |
| 45 | * 0 if not provided. |
| 46 | */ |
Mike Frysinger | 60a156d | 2019-06-13 10:15:45 -0400 | [diff] [blame] | 47 | hterm.Screen = function(columnCount=0) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Public, read-only access to the rows in this screen. |
| 50 | */ |
| 51 | this.rowsArray = []; |
| 52 | |
| 53 | // The max column width for this screen. |
Mike Frysinger | 60a156d | 2019-06-13 10:15:45 -0400 | [diff] [blame] | 54 | this.columnCount_ = columnCount; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 55 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 56 | // The current color, bold, underline and blink attributes. |
| 57 | this.textAttributes = new hterm.TextAttributes(window.document); |
| 58 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 59 | // Current zero-based cursor coordinates. |
| 60 | this.cursorPosition = new hterm.RowCol(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 61 | |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 62 | // Saved state used by DECSC and related settings. This is only for saving |
| 63 | // and restoring specific state, not for the current/active state. |
| 64 | this.cursorState_ = new hterm.Screen.CursorState(this); |
| 65 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 66 | // The node containing the row that the cursor is positioned on. |
| 67 | this.cursorRowNode_ = null; |
| 68 | |
| 69 | // The node containing the span of text that the cursor is positioned on. |
| 70 | this.cursorNode_ = null; |
| 71 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 72 | // The offset in column width into cursorNode_ where the cursor is positioned. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 73 | this.cursorOffset_ = null; |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 74 | |
| 75 | // Regexes for expanding word selections. |
| 76 | this.wordBreakMatchLeft = null; |
| 77 | this.wordBreakMatchRight = null; |
| 78 | this.wordBreakMatchMiddle = null; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * Return the screen size as an hterm.Size object. |
| 83 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 84 | * @return {!hterm.Size} hterm.Size object representing the current number |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 85 | * of rows and columns in this screen. |
| 86 | */ |
| 87 | hterm.Screen.prototype.getSize = function() { |
| 88 | return new hterm.Size(this.columnCount_, this.rowsArray.length); |
| 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * Return the current number of rows in this screen. |
| 93 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 94 | * @return {number} The number of rows in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 95 | */ |
| 96 | hterm.Screen.prototype.getHeight = function() { |
| 97 | return this.rowsArray.length; |
| 98 | }; |
| 99 | |
| 100 | /** |
| 101 | * Return the current number of columns in this screen. |
| 102 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 103 | * @return {number} The number of columns in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 104 | */ |
| 105 | hterm.Screen.prototype.getWidth = function() { |
| 106 | return this.columnCount_; |
| 107 | }; |
| 108 | |
| 109 | /** |
| 110 | * Set the maximum number of columns per row. |
| 111 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 112 | * @param {number} count The maximum number of columns per row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 113 | */ |
| 114 | hterm.Screen.prototype.setColumnCount = function(count) { |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 115 | this.columnCount_ = count; |
| 116 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 117 | if (this.cursorPosition.column >= count) |
| 118 | this.setCursorPosition(this.cursorPosition.row, count - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * Remove the first row from the screen and return it. |
| 123 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 124 | * @return {!Element} The first row in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 125 | */ |
| 126 | hterm.Screen.prototype.shiftRow = function() { |
| 127 | return this.shiftRows(1)[0]; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 128 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Remove rows from the top of the screen and return them as an array. |
| 132 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 133 | * @param {number} count The number of rows to remove. |
| 134 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 135 | */ |
| 136 | hterm.Screen.prototype.shiftRows = function(count) { |
| 137 | return this.rowsArray.splice(0, count); |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * Insert a row at the top of the screen. |
| 142 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 143 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 144 | */ |
| 145 | hterm.Screen.prototype.unshiftRow = function(row) { |
| 146 | this.rowsArray.splice(0, 0, row); |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * Insert rows at the top of the screen. |
| 151 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 152 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 153 | */ |
| 154 | hterm.Screen.prototype.unshiftRows = function(rows) { |
| 155 | this.rowsArray.unshift.apply(this.rowsArray, rows); |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * Remove the last row from the screen and return it. |
| 160 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 161 | * @return {!Element} The last row in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 162 | */ |
| 163 | hterm.Screen.prototype.popRow = function() { |
| 164 | return this.popRows(1)[0]; |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * Remove rows from the bottom of the screen and return them as an array. |
| 169 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 170 | * @param {number} count The number of rows to remove. |
| 171 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 172 | */ |
| 173 | hterm.Screen.prototype.popRows = function(count) { |
| 174 | return this.rowsArray.splice(this.rowsArray.length - count, count); |
| 175 | }; |
| 176 | |
| 177 | /** |
| 178 | * Insert a row at the bottom of the screen. |
| 179 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 180 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 181 | */ |
| 182 | hterm.Screen.prototype.pushRow = function(row) { |
| 183 | this.rowsArray.push(row); |
| 184 | }; |
| 185 | |
| 186 | /** |
| 187 | * Insert rows at the bottom of the screen. |
| 188 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 189 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 190 | */ |
| 191 | hterm.Screen.prototype.pushRows = function(rows) { |
| 192 | rows.push.apply(this.rowsArray, rows); |
| 193 | }; |
| 194 | |
| 195 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 196 | * Insert a row at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 197 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 198 | * @param {number} index The index to insert the row. |
| 199 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 200 | */ |
| 201 | hterm.Screen.prototype.insertRow = function(index, row) { |
| 202 | this.rowsArray.splice(index, 0, row); |
| 203 | }; |
| 204 | |
| 205 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 206 | * Insert rows at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 207 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 208 | * @param {number} index The index to insert the rows. |
| 209 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 210 | */ |
| 211 | hterm.Screen.prototype.insertRows = function(index, rows) { |
| 212 | for (var i = 0; i < rows.length; i++) { |
| 213 | this.rowsArray.splice(index + i, 0, rows[i]); |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 218 | * Remove a row from the screen and return it. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 219 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 220 | * @param {number} index The index of the row to remove. |
| 221 | * @return {!Element} The selected row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 222 | */ |
| 223 | hterm.Screen.prototype.removeRow = function(index) { |
| 224 | return this.rowsArray.splice(index, 1)[0]; |
| 225 | }; |
| 226 | |
| 227 | /** |
| 228 | * Remove rows from the bottom of the screen and return them as an array. |
| 229 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 230 | * @param {number} index The index to start removing rows. |
| 231 | * @param {number} count The number of rows to remove. |
| 232 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 233 | */ |
| 234 | hterm.Screen.prototype.removeRows = function(index, count) { |
| 235 | return this.rowsArray.splice(index, count); |
| 236 | }; |
| 237 | |
| 238 | /** |
| 239 | * Invalidate the current cursor position. |
| 240 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 241 | * This sets this.cursorPosition to (0, 0) and clears out some internal |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 242 | * data. |
| 243 | * |
| 244 | * Attempting to insert or overwrite text while the cursor position is invalid |
| 245 | * will raise an obscure exception. |
| 246 | */ |
| 247 | hterm.Screen.prototype.invalidateCursorPosition = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 248 | this.cursorPosition.move(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 249 | this.cursorRowNode_ = null; |
| 250 | this.cursorNode_ = null; |
| 251 | this.cursorOffset_ = null; |
| 252 | }; |
| 253 | |
| 254 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 255 | * Clear the contents of the cursor row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 256 | */ |
| 257 | hterm.Screen.prototype.clearCursorRow = function() { |
| 258 | this.cursorRowNode_.innerHTML = ''; |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 259 | this.cursorRowNode_.removeAttribute('line-overflow'); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 260 | this.cursorOffset_ = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 261 | this.cursorPosition.column = 0; |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 262 | this.cursorPosition.overflow = false; |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 263 | |
| 264 | var text; |
| 265 | if (this.textAttributes.isDefault()) { |
| 266 | text = ''; |
| 267 | } else { |
Mike Frysinger | 73e5646 | 2019-07-17 00:23:46 -0500 | [diff] [blame] | 268 | text = ' '.repeat(this.columnCount_); |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 271 | // We shouldn't honor inverse colors when clearing an area, to match |
| 272 | // xterm's back color erase behavior. |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 273 | var inverse = this.textAttributes.inverse; |
| 274 | this.textAttributes.inverse = false; |
| 275 | this.textAttributes.syncColors(); |
| 276 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 277 | var node = this.textAttributes.createContainer(text); |
| 278 | this.cursorRowNode_.appendChild(node); |
| 279 | this.cursorNode_ = node; |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 280 | |
| 281 | this.textAttributes.inverse = inverse; |
| 282 | this.textAttributes.syncColors(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | /** |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 286 | * Mark the current row as having overflowed to the next line. |
| 287 | * |
| 288 | * The line overflow state is used when converting a range of rows into text. |
| 289 | * It makes it possible to recombine two or more overflow terminal rows into |
| 290 | * a single line. |
| 291 | * |
| 292 | * This is distinct from the cursor being in the overflow state. Cursor |
| 293 | * overflow indicates that printing at the cursor position will commit a |
| 294 | * line overflow, unless it is preceded by a repositioning of the cursor |
| 295 | * to a non-overflow state. |
| 296 | */ |
| 297 | hterm.Screen.prototype.commitLineOverflow = function() { |
| 298 | this.cursorRowNode_.setAttribute('line-overflow', true); |
| 299 | }; |
| 300 | |
| 301 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 302 | * Relocate the cursor to a give row and column. |
| 303 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 304 | * @param {number} row The zero based row. |
| 305 | * @param {number} column The zero based column. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 306 | */ |
| 307 | hterm.Screen.prototype.setCursorPosition = function(row, column) { |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 308 | if (!this.rowsArray.length) { |
| 309 | console.warn('Attempt to set cursor position on empty screen.'); |
| 310 | return; |
| 311 | } |
| 312 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 313 | if (row >= this.rowsArray.length) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 314 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 315 | row = this.rowsArray.length - 1; |
| 316 | } else if (row < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 317 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 318 | row = 0; |
| 319 | } |
| 320 | |
| 321 | if (column >= this.columnCount_) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 322 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 323 | column = this.columnCount_ - 1; |
| 324 | } else if (column < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 325 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 326 | column = 0; |
| 327 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 328 | |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 329 | this.cursorPosition.overflow = false; |
| 330 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 331 | var rowNode = this.rowsArray[row]; |
| 332 | var node = rowNode.firstChild; |
| 333 | |
| 334 | if (!node) { |
| 335 | node = rowNode.ownerDocument.createTextNode(''); |
| 336 | rowNode.appendChild(node); |
| 337 | } |
| 338 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 339 | var currentColumn = 0; |
| 340 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 341 | if (rowNode == this.cursorRowNode_) { |
| 342 | if (column >= this.cursorPosition.column - this.cursorOffset_) { |
| 343 | node = this.cursorNode_; |
| 344 | currentColumn = this.cursorPosition.column - this.cursorOffset_; |
| 345 | } |
| 346 | } else { |
| 347 | this.cursorRowNode_ = rowNode; |
| 348 | } |
| 349 | |
| 350 | this.cursorPosition.move(row, column); |
| 351 | |
| 352 | while (node) { |
| 353 | var offset = column - currentColumn; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 354 | var width = hterm.TextAttributes.nodeWidth(node); |
| 355 | if (!node.nextSibling || width > offset) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 356 | this.cursorNode_ = node; |
| 357 | this.cursorOffset_ = offset; |
| 358 | return; |
| 359 | } |
| 360 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 361 | currentColumn += width; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 362 | node = node.nextSibling; |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 367 | * Set the provided selection object to be a caret selection at the current |
| 368 | * cursor position. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 369 | * |
| 370 | * @param {!Selection} selection |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 371 | */ |
| 372 | hterm.Screen.prototype.syncSelectionCaret = function(selection) { |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 373 | try { |
| 374 | selection.collapse(this.cursorNode_, this.cursorOffset_); |
| 375 | } catch (firefoxIgnoredException) { |
| 376 | // FF can throw an exception if the range is off, rather than just not |
| 377 | // performing the collapse. |
| 378 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | /** |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 382 | * Split a single node into two nodes at the given offset. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 383 | * |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 384 | * For example: |
| 385 | * Given the DOM fragment '<div><span>Hello World</span></div>', call splitNode_ |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 386 | * passing the span and an offset of 6. This would modify the fragment to |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 387 | * become: '<div><span>Hello </span><span>World</span></div>'. If the span |
| 388 | * had any attributes they would have been copied to the new span as well. |
| 389 | * |
| 390 | * The to-be-split node must have a container, so that the new node can be |
| 391 | * placed next to it. |
| 392 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 393 | * @param {!Node} node The node to split. |
| 394 | * @param {number} offset The offset into the node where the split should |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 395 | * occur. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 396 | */ |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 397 | hterm.Screen.prototype.splitNode_ = function(node, offset) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 398 | var afterNode = node.cloneNode(false); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 399 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 400 | var textContent = node.textContent; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 401 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset); |
| 402 | afterNode.textContent = lib.wc.substr(textContent, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 403 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 404 | if (afterNode.textContent) |
| 405 | node.parentNode.insertBefore(afterNode, node.nextSibling); |
| 406 | if (!node.textContent) |
| 407 | node.parentNode.removeChild(node); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | /** |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 411 | * Ensure that text is clipped and the cursor is clamped to the column count. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 412 | */ |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 413 | hterm.Screen.prototype.maybeClipCurrentRow = function() { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 414 | var width = hterm.TextAttributes.nodeWidth(this.cursorRowNode_); |
| 415 | |
| 416 | if (width <= this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 417 | // Current row does not need clipping, but may need clamping. |
| 418 | if (this.cursorPosition.column >= this.columnCount_) { |
| 419 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 420 | this.cursorPosition.overflow = true; |
| 421 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 422 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
| 426 | // Save off the current column so we can maybe restore it later. |
| 427 | var currentColumn = this.cursorPosition.column; |
| 428 | |
| 429 | // Move the cursor to the final column. |
| 430 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 431 | |
| 432 | // Remove any text that partially overflows. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 433 | width = hterm.TextAttributes.nodeWidth(this.cursorNode_); |
| 434 | |
| 435 | if (this.cursorOffset_ < width - 1) { |
| 436 | this.cursorNode_.textContent = hterm.TextAttributes.nodeSubstr( |
| 437 | this.cursorNode_, 0, this.cursorOffset_ + 1); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Remove all nodes after the cursor. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 441 | var rowNode = this.cursorRowNode_; |
| 442 | var node = this.cursorNode_.nextSibling; |
| 443 | |
| 444 | while (node) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 445 | rowNode.removeChild(node); |
| 446 | node = this.cursorNode_.nextSibling; |
| 447 | } |
| 448 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 449 | if (currentColumn < this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 450 | // If the cursor was within the screen before we started then restore its |
| 451 | // position. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 452 | this.setCursorPosition(this.cursorPosition.row, currentColumn); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 453 | } else { |
| 454 | // Otherwise leave it at the the last column in the overflow state. |
| 455 | this.cursorPosition.overflow = true; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 456 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | /** |
| 460 | * Insert a string at the current character position using the current |
| 461 | * text attributes. |
| 462 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 463 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 464 | * text and clamp the cursor. |
| 465 | * |
| 466 | * It is also up to the caller to properly maintain the line overflow state |
| 467 | * using hterm.Screen..commitLineOverflow(). |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 468 | * |
| 469 | * @param {string} str The string to insert. |
| 470 | * @param {number=} wcwidth The cached lib.wc.strWidth value for |str|. Will be |
| 471 | * calculated on demand if need be. Passing in a cached value helps speed |
| 472 | * up processing as this is a hot codepath. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 473 | */ |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 474 | hterm.Screen.prototype.insertString = function(str, wcwidth=undefined) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 475 | var cursorNode = this.cursorNode_; |
| 476 | var cursorNodeText = cursorNode.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 477 | |
Robert Ginda | a21dfb3 | 2013-10-31 14:17:45 -0700 | [diff] [blame] | 478 | this.cursorRowNode_.removeAttribute('line-overflow'); |
| 479 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 480 | // We may alter the width of the string by prepending some missing |
| 481 | // whitespaces, so we need to record the string width ahead of time. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 482 | if (wcwidth === undefined) |
| 483 | wcwidth = lib.wc.strWidth(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 484 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 485 | // No matter what, before this function exits the cursor column will have |
| 486 | // moved this much. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 487 | this.cursorPosition.column += wcwidth; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 488 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 489 | // Local cache of the cursor offset. |
| 490 | var offset = this.cursorOffset_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 491 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 492 | // Reverse offset is the offset measured from the end of the string. |
| 493 | // Zero implies that the cursor is at the end of the cursor node. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 494 | var reverseOffset = hterm.TextAttributes.nodeWidth(cursorNode) - offset; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 495 | |
| 496 | if (reverseOffset < 0) { |
| 497 | // A negative reverse offset means the cursor is positioned past the end |
| 498 | // of the characters on this line. We'll need to insert the missing |
| 499 | // whitespace. |
Mike Frysinger | 73e5646 | 2019-07-17 00:23:46 -0500 | [diff] [blame] | 500 | const ws = ' '.repeat(-reverseOffset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 501 | |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 502 | // This whitespace should be completely unstyled. Underline, background |
| 503 | // color, and strikethrough would be visible on whitespace, so we can't use |
| 504 | // one of those spans to hold the text. |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 505 | if (!(this.textAttributes.underline || |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 506 | this.textAttributes.strikethrough || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 507 | this.textAttributes.background || |
| 508 | this.textAttributes.wcNode || |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 509 | !this.textAttributes.asciiNode || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 510 | this.textAttributes.tileData != null)) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 511 | // Best case scenario, we can just pretend the spaces were part of the |
| 512 | // original string. |
| 513 | str = ws + str; |
Mike Frysinger | 6a4f241 | 2017-08-31 01:11:25 -0400 | [diff] [blame] | 514 | } else if (cursorNode.nodeType == Node.TEXT_NODE || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 515 | !(cursorNode.wcNode || |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 516 | !cursorNode.asciiNode || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 517 | cursorNode.tileNode || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 518 | cursorNode.style.textDecoration || |
Mike Frysinger | 09c54f4 | 2017-12-15 01:12:30 -0500 | [diff] [blame] | 519 | cursorNode.style.textDecorationStyle || |
| 520 | cursorNode.style.textDecorationLine || |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 521 | cursorNode.style.backgroundColor)) { |
| 522 | // Second best case, the current node is able to hold the whitespace. |
| 523 | cursorNode.textContent = (cursorNodeText += ws); |
| 524 | } else { |
| 525 | // Worst case, we have to create a new node to hold the whitespace. |
| 526 | var wsNode = cursorNode.ownerDocument.createTextNode(ws); |
| 527 | this.cursorRowNode_.insertBefore(wsNode, cursorNode.nextSibling); |
| 528 | this.cursorNode_ = cursorNode = wsNode; |
| 529 | this.cursorOffset_ = offset = -reverseOffset; |
| 530 | cursorNodeText = ws; |
| 531 | } |
| 532 | |
| 533 | // We now know for sure that we're at the last character of the cursor node. |
| 534 | reverseOffset = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 535 | } |
| 536 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 537 | if (this.textAttributes.matchesContainer(cursorNode)) { |
| 538 | // The new text can be placed directly in the cursor node. |
| 539 | if (reverseOffset == 0) { |
| 540 | cursorNode.textContent = cursorNodeText + str; |
| 541 | } else if (offset == 0) { |
| 542 | cursorNode.textContent = str + cursorNodeText; |
| 543 | } else { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 544 | cursorNode.textContent = |
| 545 | hterm.TextAttributes.nodeSubstr(cursorNode, 0, offset) + |
| 546 | str + hterm.TextAttributes.nodeSubstr(cursorNode, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 547 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 548 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 549 | this.cursorOffset_ += wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 550 | return; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 551 | } |
| 552 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 553 | // The cursor node is the wrong style for the new text. If we're at the |
| 554 | // beginning or end of the cursor node, then the adjacent node is also a |
| 555 | // potential candidate. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 556 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 557 | if (offset == 0) { |
| 558 | // At the beginning of the cursor node, the check the previous sibling. |
| 559 | var previousSibling = cursorNode.previousSibling; |
| 560 | if (previousSibling && |
| 561 | this.textAttributes.matchesContainer(previousSibling)) { |
| 562 | previousSibling.textContent += str; |
| 563 | this.cursorNode_ = previousSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 564 | this.cursorOffset_ = lib.wc.strWidth(previousSibling.textContent); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
| 568 | var newNode = this.textAttributes.createContainer(str); |
| 569 | this.cursorRowNode_.insertBefore(newNode, cursorNode); |
| 570 | this.cursorNode_ = newNode; |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 571 | this.cursorOffset_ = wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 572 | return; |
| 573 | } |
| 574 | |
| 575 | if (reverseOffset == 0) { |
| 576 | // At the end of the cursor node, the check the next sibling. |
| 577 | var nextSibling = cursorNode.nextSibling; |
| 578 | if (nextSibling && |
| 579 | this.textAttributes.matchesContainer(nextSibling)) { |
| 580 | nextSibling.textContent = str + nextSibling.textContent; |
| 581 | this.cursorNode_ = nextSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 582 | this.cursorOffset_ = lib.wc.strWidth(str); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
| 586 | var newNode = this.textAttributes.createContainer(str); |
| 587 | this.cursorRowNode_.insertBefore(newNode, nextSibling); |
| 588 | this.cursorNode_ = newNode; |
| 589 | // We specifically need to include any missing whitespace here, since it's |
| 590 | // going in a new node. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 591 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth(newNode); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 592 | return; |
| 593 | } |
| 594 | |
| 595 | // Worst case, we're somewhere in the middle of the cursor node. We'll |
| 596 | // have to split it into two nodes and insert our new container in between. |
| 597 | this.splitNode_(cursorNode, offset); |
| 598 | var newNode = this.textAttributes.createContainer(str); |
| 599 | this.cursorRowNode_.insertBefore(newNode, cursorNode.nextSibling); |
| 600 | this.cursorNode_ = newNode; |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 601 | this.cursorOffset_ = wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 605 | * Overwrite the text at the current cursor position. |
| 606 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 607 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 608 | * text and clamp the cursor. |
| 609 | * |
| 610 | * It is also up to the caller to properly maintain the line overflow state |
| 611 | * using hterm.Screen..commitLineOverflow(). |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 612 | * |
| 613 | * @param {string} str The source string for overwriting existing content. |
| 614 | * @param {number=} wcwidth The cached lib.wc.strWidth value for |str|. Will be |
| 615 | * calculated on demand if need be. Passing in a cached value helps speed |
| 616 | * up processing as this is a hot codepath. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 617 | */ |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 618 | hterm.Screen.prototype.overwriteString = function(str, wcwidth=undefined) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 619 | var maxLength = this.columnCount_ - this.cursorPosition.column; |
| 620 | if (!maxLength) |
Mike Frysinger | 159b739 | 2019-03-26 11:08:32 -0700 | [diff] [blame] | 621 | return; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 622 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 623 | if (wcwidth === undefined) |
| 624 | wcwidth = lib.wc.strWidth(str); |
| 625 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 626 | if (this.textAttributes.matchesContainer(this.cursorNode_) && |
| 627 | this.cursorNode_.textContent.substr(this.cursorOffset_) == str) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 628 | // This overwrite would be a no-op, just move the cursor and return. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 629 | this.cursorOffset_ += wcwidth; |
| 630 | this.cursorPosition.column += wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 631 | return; |
| 632 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 633 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 634 | this.deleteChars(Math.min(wcwidth, maxLength)); |
| 635 | this.insertString(str, wcwidth); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | /** |
| 639 | * Forward-delete one or more characters at the current cursor position. |
| 640 | * |
| 641 | * Text to the right of the deleted characters is shifted left. Only affects |
| 642 | * characters on the same row as the cursor. |
| 643 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 644 | * @param {number} count The column width of characters to delete. This is |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 645 | * clamped to the column width minus the cursor column. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 646 | * @return {number} The column width of the characters actually deleted. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 647 | */ |
| 648 | hterm.Screen.prototype.deleteChars = function(count) { |
| 649 | var node = this.cursorNode_; |
| 650 | var offset = this.cursorOffset_; |
| 651 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 652 | var currentCursorColumn = this.cursorPosition.column; |
| 653 | count = Math.min(count, this.columnCount_ - currentCursorColumn); |
| 654 | if (!count) |
| 655 | return 0; |
| 656 | |
| 657 | var rv = count; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 658 | var startLength, endLength; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 659 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 660 | while (node && count) { |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 661 | // Sanity check so we don't loop forever, but we don't also go quietly. |
| 662 | if (count < 0) { |
| 663 | console.error(`Deleting ${rv} chars went negative: ${count}`); |
| 664 | break; |
| 665 | } |
| 666 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 667 | startLength = hterm.TextAttributes.nodeWidth(node); |
| 668 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset) + |
| 669 | hterm.TextAttributes.nodeSubstr(node, offset + count); |
| 670 | endLength = hterm.TextAttributes.nodeWidth(node); |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 671 | |
| 672 | // Deal with splitting wide characters. There are two ways: we could delete |
| 673 | // the first column or the second column. In both cases, we delete the wide |
| 674 | // character and replace one of the columns with a space (since the other |
| 675 | // was deleted). If there are more chars to delete, the next loop will pick |
| 676 | // up the slack. |
| 677 | if (node.wcNode && offset < startLength && |
| 678 | ((endLength && startLength == endLength) || (!endLength && offset == 1))) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 679 | // No characters were deleted when there should be. We're probably trying |
| 680 | // to delete one column width from a wide character node. We remove the |
| 681 | // wide character node here and replace it with a single space. |
| 682 | var spaceNode = this.textAttributes.createContainer(' '); |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 683 | node.parentNode.insertBefore(spaceNode, offset ? node : node.nextSibling); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 684 | node.textContent = ''; |
| 685 | endLength = 0; |
| 686 | count -= 1; |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 687 | } else |
| 688 | count -= startLength - endLength; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 689 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 690 | var nextNode = node.nextSibling; |
| 691 | if (endLength == 0 && node != this.cursorNode_) { |
| 692 | node.parentNode.removeChild(node); |
| 693 | } |
| 694 | node = nextNode; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 695 | offset = 0; |
| 696 | } |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 697 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 698 | // Remove this.cursorNode_ if it is an empty non-text node. |
Mike Frysinger | 6a4f241 | 2017-08-31 01:11:25 -0400 | [diff] [blame] | 699 | if (this.cursorNode_.nodeType != Node.TEXT_NODE && |
| 700 | !this.cursorNode_.textContent) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 701 | var cursorNode = this.cursorNode_; |
| 702 | if (cursorNode.previousSibling) { |
| 703 | this.cursorNode_ = cursorNode.previousSibling; |
| 704 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth( |
| 705 | cursorNode.previousSibling); |
| 706 | } else if (cursorNode.nextSibling) { |
| 707 | this.cursorNode_ = cursorNode.nextSibling; |
| 708 | this.cursorOffset_ = 0; |
| 709 | } else { |
| 710 | var emptyNode = this.cursorRowNode_.ownerDocument.createTextNode(''); |
| 711 | this.cursorRowNode_.appendChild(emptyNode); |
| 712 | this.cursorNode_ = emptyNode; |
| 713 | this.cursorOffset_ = 0; |
| 714 | } |
| 715 | this.cursorRowNode_.removeChild(cursorNode); |
| 716 | } |
| 717 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 718 | return rv; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 719 | }; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 720 | |
| 721 | /** |
| 722 | * Finds first X-ROW of a line containing specified X-ROW. |
| 723 | * Used to support line overflow. |
| 724 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 725 | * @param {!Node} row X-ROW to begin search for first row of line. |
| 726 | * @return {!Node} The X-ROW that is at the beginning of the line. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 727 | **/ |
| 728 | hterm.Screen.prototype.getLineStartRow_ = function(row) { |
| 729 | while (row.previousSibling && |
| 730 | row.previousSibling.hasAttribute('line-overflow')) { |
| 731 | row = row.previousSibling; |
| 732 | } |
| 733 | return row; |
| 734 | }; |
| 735 | |
| 736 | /** |
| 737 | * Gets text of a line beginning with row. |
| 738 | * Supports line overflow. |
| 739 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 740 | * @param {!Node} row First X-ROW of line. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 741 | * @return {string} Text content of line. |
| 742 | **/ |
| 743 | hterm.Screen.prototype.getLineText_ = function(row) { |
| 744 | var rowText = ""; |
| 745 | while (row) { |
| 746 | rowText += row.textContent; |
| 747 | if (row.hasAttribute('line-overflow')) { |
| 748 | row = row.nextSibling; |
| 749 | } else { |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | return rowText; |
| 754 | }; |
| 755 | |
| 756 | /** |
| 757 | * Returns X-ROW that is ancestor of the node. |
| 758 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 759 | * @param {!Node} node Node to get X-ROW ancestor for. |
| 760 | * @return {!Node} X-ROW ancestor of node, or null if not found. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 761 | **/ |
| 762 | hterm.Screen.prototype.getXRowAncestor_ = function(node) { |
| 763 | while (node) { |
| 764 | if (node.nodeName === 'X-ROW') |
| 765 | break; |
| 766 | node = node.parentNode; |
| 767 | } |
| 768 | return node; |
| 769 | }; |
| 770 | |
| 771 | /** |
| 772 | * Returns position within line of character at offset within node. |
| 773 | * Supports line overflow. |
| 774 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 775 | * @param {!Node} row X-ROW at beginning of line. |
| 776 | * @param {!Node} node Node to get position of. |
| 777 | * @param {number} offset Offset into node. |
| 778 | * @return {number} Position within line of character at offset within node. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 779 | **/ |
| 780 | hterm.Screen.prototype.getPositionWithOverflow_ = function(row, node, offset) { |
| 781 | if (!node) |
| 782 | return -1; |
| 783 | var ancestorRow = this.getXRowAncestor_(node); |
| 784 | if (!ancestorRow) |
| 785 | return -1; |
| 786 | var position = 0; |
| 787 | while (ancestorRow != row) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 788 | position += hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 789 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
| 790 | row = row.nextSibling; |
| 791 | } else { |
| 792 | return -1; |
| 793 | } |
| 794 | } |
| 795 | return position + this.getPositionWithinRow_(row, node, offset); |
| 796 | }; |
| 797 | |
| 798 | /** |
| 799 | * Returns position within row of character at offset within node. |
| 800 | * Does not support line overflow. |
| 801 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 802 | * @param {!Node} row X-ROW to get position within. |
| 803 | * @param {!Node} node Node to get position for. |
| 804 | * @param {number} offset Offset within node to get position for. |
| 805 | * @return {number} Position within row of character at offset within node. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 806 | **/ |
| 807 | hterm.Screen.prototype.getPositionWithinRow_ = function(row, node, offset) { |
| 808 | if (node.parentNode != row) { |
Mike Frysinger | 498192d | 2017-06-26 18:23:31 -0400 | [diff] [blame] | 809 | // If we traversed to the top node, then there's nothing to find here. |
| 810 | if (node.parentNode == null) |
| 811 | return -1; |
| 812 | |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 813 | return this.getPositionWithinRow_(node.parentNode, node, offset) + |
| 814 | this.getPositionWithinRow_(row, node.parentNode, 0); |
| 815 | } |
| 816 | var position = 0; |
| 817 | for (var i = 0; i < row.childNodes.length; i++) { |
| 818 | var currentNode = row.childNodes[i]; |
| 819 | if (currentNode == node) |
| 820 | return position + offset; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 821 | position += hterm.TextAttributes.nodeWidth(currentNode); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 822 | } |
| 823 | return -1; |
| 824 | }; |
| 825 | |
| 826 | /** |
| 827 | * Returns the node and offset corresponding to position within line. |
| 828 | * Supports line overflow. |
| 829 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 830 | * @param {!Node} row X-ROW at beginning of line. |
| 831 | * @param {number} position Position within line to retrieve node and offset. |
| 832 | * @return {!Array} Two element array containing node and offset respectively. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 833 | **/ |
| 834 | hterm.Screen.prototype.getNodeAndOffsetWithOverflow_ = function(row, position) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 835 | while (row && position > hterm.TextAttributes.nodeWidth(row)) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 836 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 837 | position -= hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 838 | row = row.nextSibling; |
| 839 | } else { |
| 840 | return -1; |
| 841 | } |
| 842 | } |
| 843 | return this.getNodeAndOffsetWithinRow_(row, position); |
| 844 | }; |
| 845 | |
| 846 | /** |
| 847 | * Returns the node and offset corresponding to position within row. |
| 848 | * Does not support line overflow. |
| 849 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 850 | * @param {!Node} row X-ROW to get position within. |
| 851 | * @param {number} position Position within row to retrieve node and offset. |
| 852 | * @return {!Array} Two element array containing node and offset respectively. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 853 | **/ |
| 854 | hterm.Screen.prototype.getNodeAndOffsetWithinRow_ = function(row, position) { |
| 855 | for (var i = 0; i < row.childNodes.length; i++) { |
| 856 | var node = row.childNodes[i]; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 857 | var nodeTextWidth = hterm.TextAttributes.nodeWidth(node); |
| 858 | if (position <= nodeTextWidth) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 859 | if (node.nodeName === 'SPAN') { |
| 860 | /** Drill down to node contained by SPAN. **/ |
| 861 | return this.getNodeAndOffsetWithinRow_(node, position); |
| 862 | } else { |
| 863 | return [node, position]; |
| 864 | } |
| 865 | } |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 866 | position -= nodeTextWidth; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 867 | } |
| 868 | return null; |
| 869 | }; |
| 870 | |
| 871 | /** |
| 872 | * Returns the node and offset corresponding to position within line. |
| 873 | * Supports line overflow. |
| 874 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 875 | * @param {!Node} row X-ROW at beginning of line. |
| 876 | * @param {number} start Start position of range within line. |
| 877 | * @param {number} end End position of range within line. |
| 878 | * @param {!Range} range Range to modify. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 879 | **/ |
| 880 | hterm.Screen.prototype.setRange_ = function(row, start, end, range) { |
| 881 | var startNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, start); |
| 882 | if (startNodeAndOffset == null) |
| 883 | return; |
| 884 | var endNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, end); |
| 885 | if (endNodeAndOffset == null) |
| 886 | return; |
| 887 | range.setStart(startNodeAndOffset[0], startNodeAndOffset[1]); |
| 888 | range.setEnd(endNodeAndOffset[0], endNodeAndOffset[1]); |
| 889 | }; |
| 890 | |
| 891 | /** |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 892 | * Expands selection to surrounding string with word break matches. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 893 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 894 | * @param {!Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 895 | * @param {string} leftMatch left word break match. |
| 896 | * @param {string} rightMatch right word break match. |
| 897 | * @param {string} insideMatch inside word break match. |
| 898 | */ |
| 899 | hterm.Screen.prototype.expandSelectionWithWordBreakMatches_ = |
| 900 | function(selection, leftMatch, rightMatch, insideMatch) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 901 | if (!selection) |
| 902 | return; |
| 903 | |
| 904 | var range = selection.getRangeAt(0); |
| 905 | if (!range || range.toString().match(/\s/)) |
| 906 | return; |
| 907 | |
Raymes Khoury | 334625a | 2018-06-25 10:29:40 +1000 | [diff] [blame] | 908 | const rowElement = this.getXRowAncestor_(range.startContainer); |
| 909 | if (!rowElement) |
| 910 | return; |
| 911 | const row = this.getLineStartRow_(rowElement); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 912 | if (!row) |
| 913 | return; |
| 914 | |
| 915 | var startPosition = this.getPositionWithOverflow_(row, |
| 916 | range.startContainer, |
| 917 | range.startOffset); |
| 918 | if (startPosition == -1) |
| 919 | return; |
| 920 | var endPosition = this.getPositionWithOverflow_(row, |
| 921 | range.endContainer, |
| 922 | range.endOffset); |
| 923 | if (endPosition == -1) |
| 924 | return; |
| 925 | |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 926 | //Move start to the left. |
| 927 | var rowText = this.getLineText_(row); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 928 | var lineUpToRange = lib.wc.substring(rowText, 0, endPosition); |
Robert Ginda | 5eba456 | 2014-08-11 11:05:54 -0700 | [diff] [blame] | 929 | var leftRegularExpression = new RegExp(leftMatch + insideMatch + "$"); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 930 | var expandedStart = lineUpToRange.search(leftRegularExpression); |
| 931 | if (expandedStart == -1 || expandedStart > startPosition) |
| 932 | return; |
| 933 | |
| 934 | //Move end to the right. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 935 | var lineFromRange = lib.wc.substring(rowText, startPosition, |
| 936 | lib.wc.strWidth(rowText)); |
Robert Ginda | 5eba456 | 2014-08-11 11:05:54 -0700 | [diff] [blame] | 937 | var rightRegularExpression = new RegExp("^" + insideMatch + rightMatch); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 938 | var found = lineFromRange.match(rightRegularExpression); |
| 939 | if (!found) |
| 940 | return; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 941 | var expandedEnd = startPosition + lib.wc.strWidth(found[0]); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 942 | if (expandedEnd == -1 || expandedEnd < endPosition) |
| 943 | return; |
| 944 | |
| 945 | this.setRange_(row, expandedStart, expandedEnd, range); |
| 946 | selection.addRange(range); |
| 947 | }; |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 948 | |
| 949 | /** |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 950 | * Expands selection to surrounding string using the user's settings. |
| 951 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 952 | * @param {!Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 953 | */ |
| 954 | hterm.Screen.prototype.expandSelection = function(selection) { |
| 955 | this.expandSelectionWithWordBreakMatches_( |
| 956 | selection, |
| 957 | this.wordBreakMatchLeft, |
| 958 | this.wordBreakMatchRight, |
| 959 | this.wordBreakMatchMiddle); |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 960 | }; |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 961 | |
| 962 | /** |
| 963 | * Expands selection to surrounding URL using a set of fixed match settings. |
| 964 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 965 | * @param {!Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 966 | */ |
| 967 | hterm.Screen.prototype.expandSelectionForUrl = function(selection) { |
| 968 | this.expandSelectionWithWordBreakMatches_( |
| 969 | selection, |
| 970 | "[^\\s\\[\\](){}<>\"'\\^!@#$%&*,;:`]", |
| 971 | "[^\\s\\[\\](){}<>\"'\\^!@#$%&*,;:~.`]", |
| 972 | "[^\\s\\[\\](){}<>\"'\\^]*"); |
| 973 | }; |
| 974 | |
| 975 | /** |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 976 | * Save the current cursor state to the corresponding screens. |
| 977 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 978 | * @param {!hterm.VT} vt The VT object to read graphic codeset details from. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 979 | */ |
| 980 | hterm.Screen.prototype.saveCursorAndState = function(vt) { |
| 981 | this.cursorState_.save(vt); |
| 982 | }; |
| 983 | |
| 984 | /** |
| 985 | * Restore the saved cursor state in the corresponding screens. |
| 986 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 987 | * @param {!hterm.VT} vt The VT object to write graphic codeset details to. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 988 | */ |
| 989 | hterm.Screen.prototype.restoreCursorAndState = function(vt) { |
| 990 | this.cursorState_.restore(vt); |
| 991 | }; |
| 992 | |
| 993 | /** |
| 994 | * Track all the things related to the current "cursor". |
| 995 | * |
| 996 | * The set of things saved & restored here is defined by DEC: |
| 997 | * https://vt100.net/docs/vt510-rm/DECSC.html |
| 998 | * - Cursor position |
| 999 | * - Character attributes set by the SGR command |
| 1000 | * - Character sets (G0, G1, G2, or G3) currently in GL and GR |
| 1001 | * - Wrap flag (autowrap or no autowrap) |
| 1002 | * - State of origin mode (DECOM) |
| 1003 | * - Selective erase attribute |
| 1004 | * - Any single shift 2 (SS2) or single shift 3 (SS3) functions sent |
| 1005 | * |
| 1006 | * These are done on a per-screen basis. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 1007 | * |
| 1008 | * @param {!hterm.Screen} screen The screen this cursor is tied to. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1009 | */ |
| 1010 | hterm.Screen.CursorState = function(screen) { |
| 1011 | this.screen_ = screen; |
| 1012 | this.cursor = null; |
| 1013 | this.textAttributes = null; |
| 1014 | this.GL = this.GR = this.G0 = this.G1 = this.G2 = this.G3 = null; |
| 1015 | }; |
| 1016 | |
| 1017 | /** |
| 1018 | * Save all the cursor state. |
| 1019 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 1020 | * @param {!hterm.VT} vt The VT object to read graphic codeset details from. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1021 | */ |
| 1022 | hterm.Screen.CursorState.prototype.save = function(vt) { |
| 1023 | this.cursor = vt.terminal.saveCursor(); |
| 1024 | |
| 1025 | this.textAttributes = this.screen_.textAttributes.clone(); |
| 1026 | |
| 1027 | this.GL = vt.GL; |
| 1028 | this.GR = vt.GR; |
| 1029 | |
| 1030 | this.G0 = vt.G0; |
| 1031 | this.G1 = vt.G1; |
| 1032 | this.G2 = vt.G2; |
| 1033 | this.G3 = vt.G3; |
| 1034 | }; |
| 1035 | |
| 1036 | /** |
| 1037 | * Restore the previously saved cursor state. |
| 1038 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame^] | 1039 | * @param {!hterm.VT} vt The VT object to write graphic codeset details to. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1040 | */ |
| 1041 | hterm.Screen.CursorState.prototype.restore = function(vt) { |
| 1042 | vt.terminal.restoreCursor(this.cursor); |
| 1043 | |
| 1044 | // Cursor restore includes char attributes (bold/etc...), but does not change |
| 1045 | // the color palette (which are a terminal setting). |
| 1046 | const tattrs = this.textAttributes.clone(); |
| 1047 | tattrs.colorPalette = this.screen_.textAttributes.colorPalette; |
| 1048 | tattrs.syncColors(); |
| 1049 | |
| 1050 | this.screen_.textAttributes = tattrs; |
| 1051 | |
| 1052 | vt.GL = this.GL; |
| 1053 | vt.GR = this.GR; |
| 1054 | |
| 1055 | vt.G0 = this.G0; |
| 1056 | vt.G1 = this.G1; |
| 1057 | vt.G2 = this.G2; |
| 1058 | vt.G3 = this.G3; |
| 1059 | }; |