]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
Implemented Terminal.prototype.fit
authorparis <pariskasidiaris@gmail.com>
Tue, 25 Mar 2014 19:18:19 +0000 (19:18 +0000)
committerparis <pariskasidiaris@gmail.com>
Tue, 25 Mar 2014 19:18:19 +0000 (19:18 +0000)
Implemented fit method for Terminal objects, which:
  - Fits the terminal's columns to its parent element width
  - Fits the terminal's rows to its parent element height

demo/main.js
src/xterm.js

index 51d86ce313eab70f7ea1a88407804e9a10d1726b..b03edf7dbd7cdb442ecf5104ebbbb4729aad892c 100644 (file)
@@ -1,13 +1,13 @@
 var terminalContainer = document.getElementById('terminal-container'),
-    term = new Terminal({
-      geometry: [95, 37]
-    });
+    term = new Terminal();
+
+term.open(terminalContainer);
+term.fit();
 
 term.prompt = function () {
   term.write('\r\n> ');
 }
 
-term.open(terminalContainer);
 term.writeln('Welcome to xterm.js');
 term.writeln('Just type some keys in the prompt below.');
 term.writeln('');
index 88b85020c31ed13e7808196266d68579fcbd51ed..0926be4ae921fa7a5f171704594582b5185be751 100644 (file)
@@ -2712,8 +2712,37 @@ Terminal.prototype.resize = function(x, y) {
 
   this.normal = null;
   
-  this.emit('resize', {terminal: this, columns: x, rows: y});
-};
+  this.emit('resize', {terminal: this, cols: x, rows: y});
+};
+
+/*
+*  Fit terminal columns and rows to the dimensions of its
+*  DOM element.
+*
+*  Approach:
+*    - Rows: Truncate the division of the terminal parent element height
+*            by the terminal row height
+*
+*    - Columns: Truncate the division of the terminal parent element width by
+*               the terminal character width (apply display: inline at the
+*               terminal row and truncate its width with the current number
+*               of columns)
+*/
+Terminal.prototype.fit = function () {
+  var container = this.element.parentElement,
+      subjectRow = this.element.firstElementChild,
+      rows = parseInt(container.offsetHeight / subjectRow.offsetHeight),
+      characterWidth,
+      cols;
+  
+  subjectRow.style.display = 'inline';
+  characterWidth = parseInt(subjectRow.offsetWidth / this.cols);
+  subjectRow.style.display = '';
+  
+  cols = parseInt(container.offsetWidth / characterWidth);
+      
+  this.resize(cols, rows);
+}
 
 Terminal.prototype.updateRange = function(y) {
   if (y < this.refreshStart) this.refreshStart = y;