termlib.js home | | | multiple terminal test | | | sample parser | | | faq | | | documentation

# frequently asked questions
Can I add chrome to the terminal? (e.g. a window header, a close box) How can I embed a terminal relative to my HTML layout? I pasted your sample code and just got an error. - ??? I can't get any input, but I don't get any erros too. How can I temporary disable the keyboard handlers? How can I set the cusor to the start / the end of the command line? How can I limit the command history to unique entries only? How can I change my color theme on the fly? How can I connect to a server?
Can I add chrome to the terminal? (e.g. a window header, a close box) Not by the means of the Terminal object's interface (since there are way too many things that you may possibly want to add). The Terminal object allows you to specify the background color, the frame color, the frame's width and the font class used. If you want to add more chrome, you must align this in a separate division element. To calculate the dimensions of the terminal use this formula: width: 2 * frameWidth + conf.cols * <width of > + 2 * 2px padding (left and right) height: 2 * frameWidth + conf.rows * conf.rowHeight + 2 * 2px padding (top and bottom). Or you could get the empirical values for width and height by calling a terminal's `getDimensions()' method, once the terminal is open. (see documentation in "readme.txt"). Finnally, you could obviously embed the terminal's division element in your custom chrome layout (see below). [This will not be compatible to Netscape 4.] p.e.:
      <div id="myTerminal1" style="position:absolute; top:100px; left:100px;">
         <table class="termChrome">
         	<tbody>
            <tr>
               <td class="termTitle">terminal 1</td>
            </tr>
            <tr>
               <td class="termBody"><div id="termDiv1" style="position:relative"></div></td>
            </tr>
         	</tbody>
         </table>
       </div>

       // get a terminal for this

       var term1 = new Terminal(
                     {
                       x: 0,
                       y: 0,
                       id: 1,
                       termDiv: "termDiv1",
                       handler: myTermHandler
                     }
                  );
       term1.open();

       // and this is how to move the chrome and the embedded terminal

       TermGlobals.setElementXY( "myTerminal1", 200, 80 );
To keep track of the instance for any widgets use the terminal's `id' property. (You must set this in the configuration object to a unique value for this purpose.) For a demonstration see the Chrome Sample Page.
How can I embed a terminal relative to my HTML layout? Define your devision element with attribute "position" set to "relative" and place this inside your layout. Call "new Terminal()" with config-values { x: 0, y: 0 } to leave it at its relative origin.
I pasted your sample code and just got an error. - ??? The short examples are kept arbitrarily simple to show the syntax. Make sure that your divison element(s) is/are rendered by the browser before `Terminal.open()' is called. Does not work:
      <head>
      <script>
        var term = new Terminal();
        term.open();
      </script>
      </head>
Does work:
      <head>
      <script>
        var term;

        function termOpen() {
           // to be called from outside after compile time
           term = new Terminal();
           term.open();
        }
      </script>
      </head>
c.f. "readme.txt" (Opening a terminal by clicking a link implies also that the page has currently focus.) With v.1.01 and higher this doesn't cause an error any more. `Terminal.prototype.open()' now returns a value for success.
I can't get any input, but I don't get any erros too. The Terminal object's functionality relies on the browsers ability to generate and handle keyboard events. Sadly some browsers lack a full implementation of the event model. (e.g. Konquerer [khtml] and early versions of Apple Safari, which is a descendant of khtml.)
How can I temporary disable the keyboard handlers? (The terminal is blocking my HTML form fields, etc.) With version 1.03 there's a global property `TermGlobals.keylock'. Set this to `true' to disable the keyboard handlers without altering any other state. Reset it to `false' to continue with your terminal session(s).
How can I set the cusor to the start / the end of the command line? In case you need to implement a shortcut (like ^A of some UN*X-shells) to jump to the beginning or the end of the current input line, there are two private instance methods you could utilize: `_getLineEnd(<row>, <col>)' returns an array [<row>, <col>] with the position of the last character in the logical input line with ASCII value >= 32 (0x20). `_getLineStart(<row>, <col>)' returns an array [<row>, <col>] with the position of the first character in the logical input line with ASCII value >= 32 (0x20). Both take a row and a column of a cursor position as arguments. p.e.:
      // jump to the start of the input line

      myCtrlHandler() {
         // catch ^A and jump to start of the line
         if (this.inputChar == 1) {
            var firstChar = this._getLineStart(this.r, this.c);
            this.cursorSet(firstChar[0], firstChar[1]);
         }
      }
(Keep in mind that this is not exactly a good example, since some browser actually don't issue a keyboard event for "^A". And other browsers, which do catch such codes, are not very reliable in that.)
How can I limit the command history to unique entries only? (My application effords commands to be commonly repeated.) With version 1.05 there is a new configuration and control flag `historyUnique'. All you need is setting this to `true' in your terminal's configuration object.
How can I change my color theme on the fly? With version 1.07 there is a new method `Terminal.rebuild()'. This method updates the GUI to current config settings while preserving all other state. p.e.:
       // change color settings on the fly
       // here: set bgColor to white and font style to class "termWhite"
       // method rebuild() updates the GUI without side effects
       // assume var term holds a referene to a Terminal object already active

       term.conf.bgColor = '#ffffff';
       term.conf.fontClass = 'termWhite';
       term.rebuild();
How can I connect to a server? The Terminal object only provides an interface to handle console input and output. External connections have to be handled outside the Terminal object. You could use the XMLHttpRequest-Object (and use a communication model like AJAX or JSON) or connect via a frame or iframe element to a foreign host. Handling connections is considered to be out of the realm of the "termlib.js" library. The code you need is in fact quite simple:
      function connectToHost(url) {
         if (window.XMLHttpRequest) {
            request = new XMLHttpRequest();
         }
         else if (window.ActiveXObject) {
             request = new ActiveXObject('Microsoft.XMLHTTP');
         }
         if (request) {
             request.onreadystatechange = requestChangeHandler;
             request.open('GET', url);
             request.send('');
         }
         else {
            // XMLHttpRequest not implemented
         }
      }

      function requestChangeHandler() {
         if (request.readyState == 4) {
            // readyState 4: complete; now test for server's response status
            if (request.status == 200) {
               // response in request.responseText or request.responseXML if XML-code
               // if it's JS-code we could get this by eval(request.responseText)
               // by this we could import whole functions to be used via the terminal
            }
            else {
               // connection error
               // status code and message in request.status and request.statusText
            }
         }
      }
You should use this only together with a timer (window.setTimeout()) to handle connection timeouts. Additionally you would need some syntax to authenticate and tell the server what you want. For this purpose you could use the following methods of the XMLHttpRequest object:
setRequestHeader("headerLabel", "value") | set a HTTP header to be sent to the server
getResponseHeader("headerLabel") | get a HTTP header sent from the server
open(method, "url" [, asyncFlag [, "userid" [, "password"]]]) | assign the destination properties to the request. be aware that userid and password are not encrypted!
send(content) | transmit a message body (post-string or DOM object)
abort() | use this to stop a pending connection
Norbert Landsteiner - August 2005 http://www.masswerk.at
> top of page