view console.js @ 52:a1e72e9c4bf7

The top window now supports styles, although due to some layout issues, as well as some aesthetic considerations, I've decided to ignore the reverse-video style and have anything in the top window always displayed in reverse video. This can be changed later via CSS and, I think, some code changes.
author Atul Varma <varmaa@toolness.com>
date Fri, 16 May 2008 02:47:31 -0700
parents 7435749633eb
children 7dc2c7bded08
line wrap: on
line source

function Console(width, height, element, observer) {
  this._width = width;
  this._height = height;
  this._element = element;
  this._pos = [0, 0];
  this._observer = observer;
  this.clear();
}

Console.prototype = {
  clear: function() {
    this._characters = [];
    this._styles = [];
    for (var y = 0; y < this._height; y++) {
      var charRow = [];
      var styleRow = [];
      for (var x = 0; x < this._width; x++) {
        charRow.push("&nbsp;");
        styleRow.push(null);
      }
      this._characters.push(charRow);
      this._styles.push(styleRow);
    }
    this.render();
  },

  moveTo: function(x, y) {
    this._pos = [x, y];
  },

  write: function(string, style) {
    var x = this._pos[0];
    var y = this._pos[1];
    for (var i = 0; i < string.length; i++) {
      var character = null;

      if (string[i] == " ")
        character = "&nbsp;";
      else if (string[i] == "\n") {
        x = 0;
        y += 1;
      } else
        character = string[i].entityify();

      if (character != null) {
        this._characters[y][x] = character;
        this._styles[y][x] = style;
        x += 1;
      }
    }
    this._pos = [x, y];
    this.render();
  },

  render: function() {
    var string = "";
    for (var y = 0; y < this._height; y++) {
      var currStyle = null;
      for (var x = 0; x < this._width; x++) {
        if (this._styles[y][x] !== currStyle) {
          if (currStyle !== null)
            string += "</span>";
          currStyle = this._styles[y][x];
          if (currStyle !== null)
            string += '<span class="' + currStyle + '">';
        }
        string += this._characters[y][x];
      }
      if (currStyle !== null)
        string += "</span>";
      string += "<br/>";
    }
    this._element.innerHTML = string;
    this._observer.onConsoleRender();
  },

  close: function() {
    this._element.innerHTML = "";
  }
}