]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/InputHandler.ts
Merge pull request #746 from Tyriar/745_colon_word_sep
[mirror_xterm.js.git] / src / InputHandler.ts
index d13ad4270eddf6c272384dcb5be574a4a8331178..34131a15a1ff296453fc2ecc9884fedd2fa840cd 100644 (file)
@@ -1,7 +1,18 @@
+/**
+ * @license MIT
+ */
+
 import { IInputHandler, ITerminal } from './Interfaces';
 import { C0 } from './EscapeSequences';
-import { CHARSETS } from './Charsets';
-
+import { DEFAULT_CHARSET } from './Charsets';
+
+/**
+ * The terminal's standard implementation of IInputHandler, this handles all
+ * input from the Parser.
+ *
+ * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand
+ * each function's header comment.
+ */
 export class InputHandler implements IInputHandler {
   // TODO: We want to type _terminal when it's pulled into TS
   constructor(private _terminal: any) { }
@@ -46,10 +57,9 @@ export class InputHandler implements IInputHandler {
           this._terminal.y++;
           if (this._terminal.y > this._terminal.scrollBottom) {
             this._terminal.y--;
-            this._terminal.scroll();
+            this._terminal.scroll(true);
           }
         } else {
-          this._terminal.x = this._terminal.cols - 1;
           if (ch_width === 2)  // FIXME: check for xterm behavior
             return;
         }
@@ -65,8 +75,9 @@ export class InputHandler implements IInputHandler {
           const removed = this._terminal.lines.get(this._terminal.y + this._terminal.ybase).pop();
           if (removed[2] === 0
               && this._terminal.lines.get(row)[this._terminal.cols - 2]
-          && this._terminal.lines.get(row)[this._terminal.cols - 2][2] === 2)
+              && this._terminal.lines.get(row)[this._terminal.cols - 2][2] === 2) {
             this._terminal.lines.get(row)[this._terminal.cols - 2] = [this._terminal.curAttr, ' ', 1];
+          }
 
           // insert empty cell at cursor
           this._terminal.lines.get(row).splice(this._terminal.x, 0, [this._terminal.curAttr, ' ', 1]);
@@ -113,6 +124,10 @@ export class InputHandler implements IInputHandler {
       this._terminal.y--;
       this._terminal.scroll();
     }
+    // If the end of the line is hit, prevent this action from wrapping around to the next line.
+    if (this._terminal.x >= this._terminal.cols) {
+      this._terminal.x--;
+    }
   }
 
   /**
@@ -207,6 +222,10 @@ export class InputHandler implements IInputHandler {
     if (this._terminal.y >= this._terminal.rows) {
       this._terminal.y = this._terminal.rows - 1;
     }
+    // If the end of the line is hit, prevent this action from wrapping around to the next line.
+    if (this._terminal.x >= this._terminal.cols) {
+      this._terminal.x--;
+    }
   }
 
   /**
@@ -233,6 +252,10 @@ export class InputHandler implements IInputHandler {
     if (param < 1) {
       param = 1;
     }
+    // If the end of the line is hit, prevent this action from wrapping around to the next line.
+    if (this._terminal.x >= this._terminal.cols) {
+      this._terminal.x--;
+    }
     this._terminal.x -= param;
     if (this._terminal.x < 0) {
       this._terminal.x = 0;
@@ -363,7 +386,13 @@ export class InputHandler implements IInputHandler {
         while (j--) this._terminal.eraseLine(j);
         break;
       case 3:
-        ; // no saved lines
+        // Clear scrollback (everything not in viewport)
+        const scrollBackSize = this._terminal.lines.length - this._terminal.rows;
+        if (scrollBackSize > 0) {
+          this._terminal.lines.trimStart(scrollBackSize);
+          this._terminal.ybase = Math.max(this._terminal.ybase - scrollBackSize, 0);
+          this._terminal.ydisp = Math.max(this._terminal.ydisp - scrollBackSize, 0);
+        }
         break;
     }
   }
@@ -682,6 +711,10 @@ export class InputHandler implements IInputHandler {
     if (this._terminal.y >= this._terminal.rows) {
       this._terminal.y = this._terminal.rows - 1;
     }
+    // If the end of the line is hit, prevent this action from wrapping around to the next line.
+    if (this._terminal.x >= this._terminal.cols) {
+      this._terminal.x--;
+    }
   }
 
   /**
@@ -810,7 +843,7 @@ export class InputHandler implements IInputHandler {
   public setMode(params: number[]): void {
     if (params.length > 1) {
       for (let i = 0; i < params.length; i++) {
-        this._terminal.setMode(params[i]);
+        this.setMode([params[i]]);
       }
 
       return;
@@ -831,10 +864,10 @@ export class InputHandler implements IInputHandler {
           this._terminal.applicationCursor = true;
           break;
         case 2:
-          this._terminal.setgCharset(0, CHARSETS.US);
-          this._terminal.setgCharset(1, CHARSETS.US);
-          this._terminal.setgCharset(2, CHARSETS.US);
-          this._terminal.setgCharset(3, CHARSETS.US);
+          this._terminal.setgCharset(0, DEFAULT_CHARSET);
+          this._terminal.setgCharset(1, DEFAULT_CHARSET);
+          this._terminal.setgCharset(2, DEFAULT_CHARSET);
+          this._terminal.setgCharset(3, DEFAULT_CHARSET);
           // set VT100 mode here
           break;
         case 3: // 132 col mode
@@ -871,7 +904,8 @@ export class InputHandler implements IInputHandler {
           this._terminal.vt200Mouse = params[0] === 1000;
           this._terminal.normalMouse = params[0] > 1000;
           this._terminal.mouseEvents = true;
-          this._terminal.element.style.cursor = 'default';
+          this._terminal.element.classList.add('enable-mouse-events');
+          this._terminal.selectionManager.disable();
           this._terminal.log('Binding to mouse events.');
           break;
         case 1004: // send focusin/focusout events
@@ -1016,7 +1050,7 @@ export class InputHandler implements IInputHandler {
   public resetMode(params: number[]): void {
     if (params.length > 1) {
       for (let i = 0; i < params.length; i++) {
-        this._terminal.resetMode(params[i]);
+        this.resetMode([params[i]]);
       }
 
       return;
@@ -1064,7 +1098,8 @@ export class InputHandler implements IInputHandler {
           this._terminal.vt200Mouse = false;
           this._terminal.normalMouse = false;
           this._terminal.mouseEvents = false;
-          this._terminal.element.style.cursor = '';
+          this._terminal.element.classList.remove('enable-mouse-events');
+          this._terminal.selectionManager.enable();
           break;
         case 1004: // send focusin/focusout events
           this._terminal.sendFocus = false;
@@ -1095,11 +1130,13 @@ export class InputHandler implements IInputHandler {
             this._terminal.scrollBottom = this._terminal.normal.scrollBottom;
             this._terminal.tabs = this._terminal.normal.tabs;
             this._terminal.normal = null;
+            // Ensure the selection manager has the correct buffer
+            this._terminal.selectionManager.setBuffer(this._terminal.lines);
             // if (params === 1049) {
             //   this.x = this.savedX;
             //   this.y = this.savedY;
             // }
-            this._terminal.queueRefresh(0, this._terminal.rows - 1);
+            this._terminal.refresh(0, this._terminal.rows - 1);
             this._terminal.viewport.syncScrollArea();
             this._terminal.showCursor();
           }
@@ -1368,7 +1405,7 @@ export class InputHandler implements IInputHandler {
     this._terminal.cursorHidden = false;
     this._terminal.insertMode = false;
     this._terminal.originMode = false;
-    this._terminal.wraparoundMode = false; // autowrap
+    this._terminal.wraparoundMode = true;  // defaults: xterm - true, vt100 - false
     this._terminal.applicationKeypad = false; // ?
     this._terminal.viewport.syncScrollArea();
     this._terminal.applicationCursor = false;
@@ -1381,6 +1418,36 @@ export class InputHandler implements IInputHandler {
     this._terminal.charsets = [null]; // ??
   }
 
+  /**
+   * CSI Ps SP q  Set cursor style (DECSCUSR, VT520).
+   *   Ps = 0  -> blinking block.
+   *   Ps = 1  -> blinking block (default).
+   *   Ps = 2  -> steady block.
+   *   Ps = 3  -> blinking underline.
+   *   Ps = 4  -> steady underline.
+   *   Ps = 5  -> blinking bar (xterm).
+   *   Ps = 6  -> steady bar (xterm).
+   */
+  public setCursorStyle(params?: number[]): void {
+    const param = params[0] < 1 ? 1 : params[0];
+    switch (param) {
+      case 1:
+      case 2:
+        this._terminal.setOption('cursorStyle', 'block');
+        break;
+      case 3:
+      case 4:
+        this._terminal.setOption('cursorStyle', 'underline');
+        break;
+      case 5:
+      case 6:
+        this._terminal.setOption('cursorStyle', 'bar');
+        break;
+    }
+    const isBlinking = param % 2 === 1;
+    this._terminal.setOption('cursorBlink', isBlinking);
+  }
+
   /**
    * CSI Ps ; Ps r
    *   Set Scrolling Region [top;bottom] (default = full size of win-
@@ -1390,7 +1457,7 @@ export class InputHandler implements IInputHandler {
   public setScrollRegion(params: number[]): void {
     if (this._terminal.prefix) return;
     this._terminal.scrollTop = (params[0] || 1) - 1;
-    this._terminal.scrollBottom = (params[1] || this._terminal.rows) - 1;
+    this._terminal.scrollBottom = (params[1] && params[1] <= this._terminal.rows ? params[1] : this._terminal.rows) - 1;
     this._terminal.x = 0;
     this._terminal.y = 0;
   }