diff termemu.js @ 4:ee22eb9ab009

Client: don't assume the terminal is 24x80.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 07 May 2012 11:09:14 -0700
parents bd412f63ce0d
children 826a7ced69f8
line wrap: on
line diff
--- a/termemu.js	Sun May 06 15:22:13 2012 -0700
+++ b/termemu.js	Mon May 07 11:09:14 2012 -0700
@@ -61,8 +61,13 @@
   normbuf: null,  // The normal screen buffer
   altbuf: null,   // The alternate screen buffer
   histbuf: null,  // The screen history buffer
+  /* Attributes */
+  w: 0, // Screen width
+  h: 0, // Screen height
   fgColor: "#b2b2b2", // Default color for text
   bgColor: "black", // Default background color
+  scrT: 0, // top and bottom of scrolling region
+  scrB: 0, // init() will set this properly
   c: null,   // Contains cursor position and text attributes
   offedge: false, // Going off the edge doesn't mean adding a new line
   clearAttrs: function () {
@@ -123,8 +128,6 @@
     else
       return fallback;
   },
-  scrT: 0, // top and bottom of scrolling region
-  scrB: 23,
   // These keyboard-related things don't really belong here.
   shift: false,
   shiftp: function () {
@@ -140,7 +143,7 @@
   togglectrl: function () {
     this.ctrl = !this.ctrl;
   },
-  init: function (divID) {
+  init: function (divID, h, w) {
     /* Makes a div full of character cells. */
     if (this.screen != null)
       return;
@@ -150,6 +153,19 @@
     while (owrap.firstChild != null)
       owrap.removeChild(owrap.firstChild);
     this.c = new Cursor(null);
+    /* Set up the sizes. */
+    var tn;
+    tn = Number(h);
+    if (tn > 0 && tn < 256)
+      this.h = tn;
+    else
+      this.h = 24;
+    tn = Number(w);
+    if (tn > 0 && tn < 256)
+      this.w = tn;
+    else
+      this.w = 80;
+    this.scrB = this.h - 1;
     /* Create the contents of the terminal div */
     this.inwrap = document.createElement("div");
     this.inwrap.id = "inwrap";
@@ -165,7 +181,7 @@
     this.normbuf = document.createElement("div");
     this.normbuf.id = "normbuf";
     termdiv.appendChild(this.normbuf);
-    for (var row = 0; row < 24; row++) {
+    for (var row = 0; row < this.h; row++) {
       this.normbuf.appendChild(this.makeRow());
     }
     this.altbuf = document.createElement("div");
@@ -227,7 +243,7 @@
       return;
     while (this.altbuf.firstChild != null)
       this.altbuf.removeChild(this.altbuf.firstChild);
-    for (var i = 0; i < 24; i++) {
+    for (var i = 0; i < this.h; i++) {
       this.altbuf.appendChild(this.makeRow());
     }
     this.normc = new Cursor(this.c);
@@ -266,8 +282,8 @@
       this.offedge = false;
       if (x < 0)
         x = 0;
-      else if (x > 79)
-        x = 79;
+      else if (x >= this.w)
+        x = this.w - 1;
     }
     if (y == null) {
       if (this.c.y != null)
@@ -277,8 +293,8 @@
     }
     else if (y < 0)
       y = 0;
-    else if (y > 23)
-      y = 23;
+    else if (y >= this.h)
+      y = this.h - 1;
     /* Un-reverse video the current location. */
     this.flipCursor();
     this.c.x = x;
@@ -314,7 +330,7 @@
       var blankrow = this.makeRow();
       /* Careful with the order */
       if (lines > 0) {
-        if (this.scrB == 23)
+        if (this.scrB == this.h - 1)
           this.screen.appendChild(blankrow);
         else
           this.screen.insertBefore(blankrow, this.screen.childNodes[this.scrB 
@@ -334,8 +350,8 @@
   },
   newline: function (doReturn) {
     if (this.c.y == this.scrB)
-      this.scroll(1)
-    else if (this.c.y < 23)
+      this.scroll(1);
+    else if (this.c.y < this.h - 1)
       this.cmove(this.c.y + 1, null);
     /* If the cursor is at the bottom but outside the scrolling region, 
      * nothing can be done. */
@@ -350,7 +366,7 @@
       this.cmove(this.c.y - 1, null);
   },
   advance: function () {
-    if (this.c.x < 79)
+    if (this.c.x < this.w - 1)
       this.cmove(null, this.c.x + 1);
     else {
       this.offedge = true;
@@ -378,11 +394,11 @@
     this.saved = null;
     this.normc = null;
     this.scrT = 0;
-    this.scrB = 23;
+    this.scrB = this.h - 1;
     while (this.histbuf.firstChild != null) {
       this.histbuf.removeChild(this.histbuf.firstChild);
     }
-    for (var i = 0; i < 24; i++) {
+    for (var i = 0; i < this.h; i++) {
       this.screen.replaceChild(this.makeRow(), this.screen.childNodes[i]);
     }
     this.flipCursor(); // make it appear in the new row
@@ -557,10 +573,10 @@
       else if (codes[i] == 9) {
         /* tab */
         var xnew;
-        if (this.c.x < 79) {
+        if (this.c.x < this.w - 1) {
           xnew = 8 * (Math.floor(this.c.x / 8) + 1);
-          if (xnew > 79)
-            xnew = 79;
+          if (xnew >= this.w)
+            xnew = this.w - 1;
           this.cmove(null, xnew);
         }
         else {
@@ -701,10 +717,10 @@
         y = params[0] - 1;
       if (params[1])
         x = params[1] - 1;
-      if (y > 23)
-        y = 23;
-      if (x > 79)
-        x = 79;
+      if (y >= this.h)
+        y = this.h - 1;
+      if (x >= this.w)
+        x = this.w - 1;
       debug(0, "Moving to row " + y + ", col " + x);
       this.cmove(y, x);
     }
@@ -717,8 +733,8 @@
       }
       while (count > 0) {
         x = 8 * (Math.floor(x / 8) + 1);
-        if (x > 79) {
-          x = 79;
+        if (x >= this.w) {
+          x = this.w - 1;
           break;
         }
         count--;
@@ -739,7 +755,7 @@
       if (!params[0]) {
         /* Either 0 or not given */
         start = this.c.y + 1;
-        end = 23;
+        end = this.h - 1;
         cols = 1;
       }
       else if (params[0] == 1) {
@@ -749,7 +765,7 @@
       }
       else if (params[0] == 2) {
         start = 0;
-        end = 23;
+        end = this.h - 1;
         cols = 0;
       }
       else {
@@ -763,7 +779,7 @@
         /* Otherwise, the whole screen was erased and the active row doesn't
          * need special treatment. */
         var cursrow = this.screen.childNodes[this.c.y];
-        for (var ncol = this.c.x; ncol >= 0 && ncol < 80; ncol += cols) {
+        for (var ncol = this.c.x; ncol >= 0 && ncol < this.w; ncol += cols) {
           cursrow.replaceChild(this.makeCell(' '), cursrow.childNodes[ncol]);
         }
       }
@@ -790,11 +806,11 @@
       }
       else if (params[0] == 2) {
         start = 0;
-        end = 79;
+        end = this.w - 1;
       }
       else {
         start = this.c.x;
-        end = 79;
+        end = this.w - 1;
       }
       var rowdiv = this.screen.childNodes[this.c.y];
       for (var i = start; i <= end; i++) {
@@ -824,7 +840,7 @@
           this.screen.insertBefore(blankrow, this.screen.childNodes[this.c.y]);
         }
         else {
-          if (this.scrB == 23)
+          if (this.scrB == this.h - 1)
             this.screen.appendChild(blankrow);
           else
             this.screen.insertBefore(blankrow, this.screen.childNodes[this.scrB
@@ -870,7 +886,7 @@
         return;
       }
       var row = this.screen.childNodes[this.c.y];
-      for (var i = 0; i < count && this.c.x + i < 80; i++) {
+      for (var i = 0; i < count && this.c.x + i < this.w; i++) {
         row.replaceChild(this.makeCell(' '), row.childNodes[this.c.x + i]);
       }
       this.flipCursor();
@@ -1012,10 +1028,10 @@
     else if (c == 114) {
       /* r - set scrolling region */
       var t = 0;
-      var b = 23;
-      if (params[0] && params[0] <= 23)
+      var b = this.h - 1;
+      if (params[0] && params[0] <= this.h - 1)
         t = params[0] - 1;
-      if (params[1] && params[1] <= 24)
+      if (params[1] && params[1] <= this.h)
         b = params[1] - 1;
       if (b <= t)
         return;
@@ -1076,7 +1092,7 @@
   makeRow: function() {
     var blankrow = document.createElement("div");
     blankrow.className = "termrow";
-    for (var i = 0; i < 80; i++)
+    for (var i = 0; i < this.w; i++)
       blankrow.appendChild(this.makeCell(' '));
     return blankrow;
   }