jsos

college code for operating system fundamentals in js

git clone https://9o.is/git/jsos.git

console.js

(7481B)


      1 /* ------------
      2    Console.js
      3 
      4    Requires globals.js
      5 
      6    The OS Console - stdIn and stdOut by default.
      7    Note: This is not the Shell.  The Shell is the "command line interface" (CLI) or interpreter for this console.
      8    ------------ */
      9 
     10 function Console() {
     11   // Properties
     12   this.CurrentFont      = DEFAULT_FONT;
     13   this.CurrentFontSize  = DEFAULT_FONT_SIZE;
     14   this.CurrentXPosition = 0;
     15   this.CurrentYPosition = DEFAULT_FONT_SIZE;
     16   this.buffer = "";
     17   this.statusText = ""; 
     18   this.scrollOffset = 0;
     19   this.textDrawBuffer = new Array();
     20     
     21   // Methods
     22   this.init        = consoleInit;
     23   this.clearScreen = consoleClearScreen;
     24   this.resetXY     = consoleResetXY;
     25   this.handleInput = consoleHandleInput;
     26   this.putText     = consolePutText;
     27   this.advanceLine = consoleAdvanceLine;
     28   this.backspace   = consoleBackspace;
     29   this.bsod        = consoleBSOD;
     30   this.gsod        = consoleGSOD;
     31   this.taskbarRefresh = consoleTaskbarRefresh;
     32   this.taskbarSetStatus = consoleSetTaskbarStatus;
     33   this.taskbarStatus = consoleTaskbarStatus;
     34   this.taskbarDateTime = consoleTaskbarDateTime;
     35   this.resetScroll = consoleResetScroll;
     36   this.scroll = consoleScroll;
     37   this.refresh = consoleRefresh;
     38   this.setColor = consoleSetColor;
     39 }
     40 
     41 function consoleInit() {
     42   consoleClearScreen();
     43   consoleResetXY();
     44   this.taskbarRefresh();
     45 }
     46 
     47 function consoleResetScroll() {
     48   this.textDrawBuffer = new Array();
     49   this.scrollOffset = 0;
     50 }
     51 
     52 function consoleClearScreen() {
     53   DRAWING_CONTEXT.clearRect(0, 0, CANVAS.width, CANVAS.height);
     54 }
     55 
     56 function consoleResetXY() {
     57   this.CurrentXPosition = 0;
     58   this.CurrentYPosition = this.CurrentFontSize;    
     59 }
     60 
     61 /*
     62  * Gets the next character from the kernel input queue
     63  * and processes it.
     64  * TODO: Write a case for Ctrl-C.
     65  */
     66 function consoleHandleInput() {
     67   while (_KernelInputQueue.getSize() > 0) {
     68     var chr = _KernelInputQueue.dequeue();
     69 
     70     if (chr == String.fromCharCode(13)) handleEnter(this); 
     71     else if (chr == String.fromCharCode(8)) this.backspace();   
     72     else handleText(this, chr);
     73   }
     74 }
     75 
     76 /*
     77  * Enter key marks the end of command so send it to shell
     78  * and reset our buffer.
     79  */
     80 function handleEnter(console) {
     81   _OsShell.handleInput(console.buffer);
     82   console.buffer = "";
     83 }
     84 
     85 /* 
     86  * A "normal" character to be drawn on the screen
     87  * and added to our buffer.
     88  */
     89 function handleText(console, chr) {
     90   console.putText(chr);
     91   console.buffer += chr;
     92 }
     93 
     94 // 
     95 /*
     96  * Draws the text at the current X and Y coordinates and 
     97  * moves the current X position.
     98  * Note: the term "text" connotes string or char.
     99  */
    100 function consolePutText(txt) {
    101   if (txt != "") { 
    102     // process by char not by string so we can word wrap
    103     for(var i in txt) {
    104       var chr = txt[i];
    105       var offset = DRAWING_CONTEXT.measureText(
    106           this.CurrentFont, 
    107           this.CurrentFontSize, 
    108           chr); 
    109       
    110       // the new x position
    111       var offsetx = this.CurrentXPosition + offset; 
    112 
    113       // if new x position is outside of canvas, wrap.
    114       if(offsetx >= CANVAS.width) {
    115         this.advanceLine();
    116         offsetx = offset;
    117       }
    118 
    119       // add the height of a new line * offsetscreen to the y.
    120       var savedYPosition = this.CurrentYPosition + 
    121           ((DEFAULT_FONT_SIZE + FONT_HEIGHT_MARGIN)*this.scrollOffset);
    122       this.textDrawBuffer.push(new Array(
    123           this.CurrentXPosition, 
    124           savedYPosition, 
    125           chr)); //save params to redraw later
    126 
    127       // Draw the text.
    128       DRAWING_CONTEXT.drawText(
    129           this.CurrentFont, 
    130           this.CurrentFontSize, 
    131           this.CurrentXPosition, 
    132           this.CurrentYPosition, 
    133           chr);
    134 
    135       // Move to the next position.
    136       this.CurrentXPosition = offsetx;
    137     }
    138   }
    139 }
    140 
    141 function consoleAdvanceLine() {
    142   this.CurrentXPosition = 0;
    143   this.CurrentYPosition += DEFAULT_FONT_SIZE + FONT_HEIGHT_MARGIN;
    144   // scroll screen if passed height
    145   if(this.CurrentYPosition > CANVAS.height - (this.CurrentFontSize+20)) {
    146     this.scroll();
    147   }
    148 }
    149 
    150 function consoleScroll() {
    151   this.scrollOffset++;
    152   this.CurrentYPosition -= DEFAULT_FONT_SIZE + FONT_HEIGHT_MARGIN;    
    153   this.refresh();
    154 }
    155 
    156 /* 
    157  * To support scrolling, we must save the canvas, move 
    158  * context one line up by the scroll offset, redraw text 
    159  * on canvas, and restore the canvas.
    160  */
    161 function consoleRefresh() {
    162   DRAWING_CONTEXT.save(); 
    163   this.clearScreen(); 
    164   DRAWING_CONTEXT.translate(0, 0 - 
    165       (DEFAULT_FONT_SIZE + FONT_HEIGHT_MARGIN) * this.scrollOffset );
    166   for( var i = 0 ; i < this.textDrawBuffer.length ; i++) {
    167     if(this.textDrawBuffer[i][1] > (DEFAULT_FONT_SIZE + 
    168         FONT_HEIGHT_MARGIN) * this.scrollOffset) {
    169       DRAWING_CONTEXT.drawText(
    170           this.CurrentFont,  
    171           this.CurrentFontSize, 
    172           this.textDrawBuffer[i][0], 
    173           this.textDrawBuffer[i][1], 
    174           this.textDrawBuffer[i][2]);                                                                      
    175     }
    176   }
    177   DRAWING_CONTEXT.restore();
    178 }
    179 
    180 /*
    181  * Deletes the leftmost character by measuring the text and
    182  * clearing it out; also removes the character from the buffer.
    183  */
    184 function consoleBackspace() {
    185   if (this.buffer.length > 0) {
    186     var charWidth = DRAWING_CONTEXT.measureText(
    187         this.CurrentFont,
    188         this.CurrentFontSize,
    189         this.buffer.slice(-1));
    190     var offset = this.CurrentXPosition - charWidth;
    191     DRAWING_CONTEXT.clearRect(
    192           offset,
    193           this.CurrentYPosition - this.CurrentFontSize,
    194           this.CurrentXPosition,
    195           this.CurrentYPosition + 2); //fudge  
    196     this.CurrentXPosition = offset;
    197     this.buffer = this.buffer.slice(0, -1);
    198   }
    199 }
    200 
    201 /*
    202  * BLUE SCREEN OF DEATH
    203  */
    204 function consoleBSOD(msg) {
    205   _OsShell.promptStr = "";
    206   DRAWING_CONTEXT.fillStyle = '#89CFF0'; // baby blue
    207   DRAWING_CONTEXT.fillRect(0, 0, CANVAS.width, CANVAS.height);
    208   DRAWING_CONTEXT.drawText(
    209       this.CurrentFont,
    210       this.CurrentFontSize,
    211       10, //fudge
    212       this.CurrentFontSize + 10, //fudge
    213       msg);
    214 }
    215 
    216 /*
    217  * GREEN SCREEN OF DEATH - just in case
    218  */
    219 function consoleGSOD() {
    220   _OsShell.promptStr = "";
    221   DRAWING_CONTEXT.fillStyle = '#5DFC0A'; // greenLED
    222   DRAWING_CONTEXT.fillRect(0, 0, CANVAS.width, CANVAS.height);
    223   DRAWING_CONTEXT.drawText(
    224       this.CurrentFont,
    225       this.CurrentFontSize,
    226       10, //fudge
    227       this.CurrentFontSize + 10, //fudge
    228       'Overriding all data... Now save yourself!');
    229   simDisableKeyboardInterrupt();
    230 }
    231 
    232 /* 
    233  * Draws the date and time on the bottom left of the canvas.
    234  */
    235 function consoleTaskbarDateTime() {
    236   DRAWING_CONTEXT.drawText(
    237       this.CurrentFont, 
    238       this.CurrentFontSize, 
    239       10, 
    240       CANVAS.height - this.CurrentFontSize, 
    241       Date());
    242 }
    243 
    244 /* 
    245  * Draws the status on the bottom rightish of the canvas.
    246  */
    247 function consoleTaskbarStatus(status) {
    248   DRAWING_CONTEXT.drawText(
    249       this.CurrentFont, 
    250       this.CurrentFontSize, 
    251       CANVAS.width / 2, 
    252       CANVAS.height - this.CurrentFontSize, 
    253       status);
    254 }
    255 
    256 /* 
    257  * Sets the status of the status.
    258  */
    259 function consoleSetTaskbarStatus(status) {
    260   this.statusText = status;
    261 }
    262 
    263 /* Erases and redraws entire taskbar. */
    264 function consoleTaskbarRefresh() {  
    265   DRAWING_CONTEXT.clearRect(
    266        0,
    267        CANVAS.height - (this.CurrentFontSize+12),
    268        CANVAS.width,
    269        (this.CurrentFontSize+12));
    270   
    271   this.taskbarDateTime();
    272   this.taskbarStatus(this.statusText); 
    273 }
    274 
    275 /* Sets fill style color. Usually to change text color. */
    276 function consoleSetColor(color) {
    277   DRAWING_CONTEXT.strokeStyle = color;
    278 }