JScript Simulation "Console" program

xiaoxiao2021-03-06  14

When you are bored, I think of JScript to simulate the Console program. I tried it, I feel very fun: P

A JScript "Console" class

Leave three demo before reading the code:

http://akira.bigwww.com/silver luna/demos/consoledemo.html Enter two numbers through "console" and http://akira.bigwww.com/silver luna/demos/consoledemo2.html "Console" Enter a sentence, statistical character type, and quantity http://akira.bigwww.com/silver luna/demos/consoleDemo3.html A simulation that can be used to enter the command via the keyboard to enter the simulation of the carriage return, but also support Begincodes .. . Description :)

Below is the program code:

// Console class, simulate the program running on the console, can control the "standard input output" // How to use: On the page contains the console.js file, construct the Console object, define the main () function write control command in the Main function // version: 1.00 // author: Akira // write date: 2004-11-19

Function console (page, consolename) {// static member console.key_enter = 13; // Enter key console.key_return = 13; // Enter console.key_newline = 10; // Return console.key_tab = 9; // Tab key console.key_backspace = 8; // 退 格 键 constole.key_left = 37; // Left shift console.key_right = 38; // Right shift console.key_up = 39; // Upshoot console.Key_Down = 40; / / Down

// Initialization Page IF {Page = Self;} IF (page! = Self) {// do situation ... throw new error ("Parameter error: Current version does not support other form objects Initializing console / n Please use the Self parameter ");} this.page = page;

// Input output stream properties, use strings array analog stream this.stream = new object (); this.stream.stdin = new array (); // Standard input Thistream.stdout = new array (); / / Standard output flow this.stream.stderr = new array (); // standard error flow

This.stream.stdin.Opened = false; // Determine if the current input stream is turned on, turn it on = Allow writes this.stream.stdin.readoffset = 0; // Judgment the current input stream bias, read () method uses this. Stream.semaphores = 0; // stream signal quantity, used to control the output stream display, when the input function is called, // When the amount of signal is greater than the number of STDIN members, the abnormal blocking standard output flow is thrown. Wait for Console.Prototype.Signal = function () {if (this.stream.Semapho} if (this.stream.Semapho) {Return True;}. Length) {throw new waitforinput (); // Throw an exception blocking program to wait for input} return false;}

THIS.BODY = Page.Document.body;

IF (this.body == null) {throw new error ("The console lack of initialization failed, check if the main document is missing the body mark");

this.page.document.title = "--Javascript Console"; this.page.PageLoadEventSender = this; this.body.onload = function () {this.PageLoadEventSender.PageLoad (this.PageLoadEventSender, this.PageLoadEventSender.page .event);} // event definitions PageLoad this.page.ResizeEventSender = this; this.body.onresize = function () {this.ResizeEventSender.OnResize (this.ResizeEventSender, this.ResizeEventSender.page.event);}

This.ConsoleMain = page.document.getlementByid (consoleName); // You can specify the name of the initialization object Textarea by parameter, which can embed the console // main window in the web page

// If you do not specify a TextArea object, establish a console main window and let the window occupy the entire page if (this.consolemain == null) {this.body.style.background = "# 000000"; this.page.document .BGCOLOR = "# 000000"; this.consolemain = page.document.createElement ("textarea"); this.consolemain; this.consolemain.style.width = "100%"; this .Consolemain.style.height = "100%"; this.consolemain.style.Overflowx = "hidden"; this.consolemain.style.overflowy = "hidden"; this.consolemain.style.Border = "0px"; this. Consolemain.style.background = "# 000000"; this.consolemain.style.color = "c0c0c0";} if (this.consolemain.tagname.touppercase ()! = "Textarea") {throw new error ("Console Lord Window type error, main window object must be TEXTAREA type ");

this.ConsoleMain.KeyPressSender = this; // KeyPress event this.ConsoleMain.onkeypress = function () {this.KeyPressSender.ConsoleMain_KeyPress (this.KeyPressSender, this.KeyPressSender.page.event);} this.ConsoleMain.KeyUpSender = this; // KeyUp event this.ConsoleMain.onkeyup = function () {this.KeyUpSender.ConsoleMain_KeyUp (this.KeyUpSender, this.KeyUpSender.page.event);} this.ConsoleMain.KeyDownSender = this; // KeyDown event this.ConsoleMain. onkeydown = function () {return (this.KeyDownSender.ConsoleMain_KeyDown (this.KeyDownSender, this.KeyDownSender.page.event));} this.ConsoleMain.ClickSender = this; // click event, to avoid the position of the cursor with a mouse click Change this.consolemain.onclick = function () {this.clicksender.consolemain_click (this.clicksender, this.clicksender.page.event);

// Event Handlers Console.Prototype.pageLoad = function (sender, event) // PageLoad Event Processing, call this.main () method to initialize the console {// create frameconsole this.main ();} console.prototype. OnResize = function (sender, event) // Resize event processing, display the text correctly in the top of the console {this.SetCursor ();} console.prototype.consolemain_keydown = function (sender, event) {if (event.keycode == Console.key_tab) // Handle the Tab key to avoid the main window to lose focus {this.consolemain.value = "/ t"; if (! This.stream.stdin.opened) {this.stream.stdin.push ("/ T "); this.Stream.stdin.Opened = true;} else {this.stream.stdin [this.stream.stdin.Length - 1] =" / t "; Return false;} if (event.keycode == console.key_left || Event.keycode == console.key_right || event.keycode == console.key_up || Event.keycode == console.key_down) // ignore the arrow keys , Lock the cursor {Return false;} return true;} console.prototype.consolemain_keyup = function (sender, event) {if (event.keycode ==

CONSOLE.KEY_ENTER) // Processes the write {if (! This.stream.stdin.opened) // ignore the blank and continuous Enter Return; else = false; else = false; This.main (); // End the stream writes, and let the program continue to perform}} if (event.keycode == console.key_backspace) // Processing the retracted key {if (! this.stream.stdin. Opened) // Ignore the return button when current flow is empty {return;} else {IF (this.stream.stdin [this.stream.stdin.Length - 1] .length == 0) {return;} this.stream.stdin [this.stream.stdin.Length - 1] = this.stream. STDIN [this.stream.stdin.Length - 1] .substr (0, this.stream.stdin [this.stream.stdin.length - 1] .length - 1); if (this.stream.stdin [this.stream] .stdin.length - 1] .length == 0) {this.stream.stdin.length -; this.stream.stdin.opened = false;

}}}} Console.prototype.consolemain_keypress = function (sender, event) // Keyboard event, write characters {if (! This.stream.stdin.opened // new stream {if (Event.Keycode! = Console.key_enter) {this.stream.stdin.push (String.Fromcharcode (es "); this.stream.stdin.opened = true;}} else {if (event.keycode! = Console.key_enter). .. Key_Enter). Stream.stdin [this.Stream.stdin.Length - 1] = string.fromcharcode (es)}}}} console.prototype.consolemain_click = function (sender, event) {this.setCursor ();} //Methods Console.Prototype.init = function () // Initialization method, called {//this.consolemain.value= "console initialization completed in this.main (); this.consolemain.contendeditable = true; this.stream.semaphoRes = 0 "This.stream.stdin.readoffset = 0; this.stream.stdin.opened = false;} console.prototype.writeline = function (msg) // Write a line of text to standard output stream {if (this.signal () ) {This.consolemain.value = msg "/ n"; this.stream.stdout.push (msg "/ n");

}}} Console.prototype.write = function (msg) // Write a string to standard output stream {if (this.Signal ()) {this.consolemain.value = msg; this.stream.stdout.push (MSG) );}} Console.Prototype.Error = function (msg) // Write error message to standard error flow {if (this.Signal ()) {this.consolemain.value = msg; this.stream.stderr.push ( MSG);}} console.prototype.readline = function () // reads a row from the standard input stream {if (this.Signal () {this.SetCursor (); // cursor shift to the input location throw new Waitforin Put (); // Throw an exception preventing procedure to wait for input} this.stream.semaphores ; if (this.stream.SemaphoRes <= this.stream.stdin.length) {IF (ISNAN (this. Stream.Stdin [this.stream.semaphores - 1])) // Non number Return Object {Return this.stream.stdin [this.stream.semaphoRes - 1];} else {return this.stream.stdin [this.Stream . Semaphores - 1] - 0;

// Digital return value}}}} console.prototype.setcursor = function () {this.consolemain.focus (); var ranging = this.consolemain.createtextRange (); Range.Movestart ('Character', this.consolemain.Value lence.collapse (false); Range.select ();}

Console.prototype.read = function () // Remove a character {if (this.Signal () && this.stream.Signal () && this.stream.stdin.readoffset == 0) {this.consolemain.focus (); var Range = this.consolemain.createtextRange (); Range.MoveStart ('Character', this.consolemain.Value.LENGHT); Range.Collapse (false); Range.Select (); this.Stream.stdin.push ("" " ); This.stream.stdin.opened = true; throw new waitforinput (); // Throw an exception blocking program to wait for input} else if (this.stream.SemaphoS <= this.stream.stdin. Length) {if (this.stream.stdin.readoffset == 0) // reads the first character of the flow, the amount of semaphore is {this.stream.semaphores ;} if (this.stream.stdin.Readoffset

This.Stream.STDIN [this.Stream.SemaphoRes - 1] .length 1) {this.stream.stdin.readoffset = 0; return console.key_newline;} else {throw new error ("Stack over Flow! Read Error! ");}}} Console.Prototype.main = function () {this.init (); try {main (); if (this.Signal ()) {this.dispose ();}} catch (e) { IF (E InstanceOf Waitforinput) {} else {this.Error ("Error:" E.NUMBER "," E.NAME ":" E.MESSAGE); // Write the error information to standard error stream this.dispose ();}}} console.prototype.dispose = function () // End the console process, press any key to close the window {this.consolemain.conteTeditable = False; this .Consolemain.focus (); this.write ("/ npress any key to pay ... / n"); this.consolemain.keydownsender = this; this.consolemain.onkeydown = function () {this.keydownsender.page. Close (); return false;}}}

// Wait for the entered "exception" class to block the operation of the program when requested by the user. Function Waitforinput () {} waitforinput.prototype = new error ();

// Debug Console - can run and debug javascript block // Version: 0.01 Demo // Author: Akira // Date: 2004-11-19function DebugConsole () {DebugConsole.prototype.RunCommand = function (command) {if (this.Signal ()) {var regexp = new regexp ("Document.writeln", "G"); Command = Command.Replace (Regexp, "this.writeLine"); regexp = new regexp ("Document.write" , "g"); command = command.replace (regexp, "this.write"); try {eval (command);} catch (e) {this.Error ("Instruction invalid:" E.NUMBER ", " E.NAME ": " E.Message " / n ");}}} debugconsole.Prototype.Start = Function ) {THIS.WRITELINE ("Debug Console Initialization ... "); Var command =" "; var runproc = false;

While ((Command = this.readline ())! = "stop") {if (command == "begin") {process = ""; runproc = true; // execute a set of commands Continue;} if (Command = = "end") {this.Runcommand (process); runproc = false; contract;}} == "=") {Command = "this.writeline (" command.substring (1) ");"} If (! Runproc) {this.RunCommand (Command); // Perform a command} else {process = Command; }}}} Debugconsole.prototype = new console (); // This console current disadvantage is: 1) Abnormal information Unable to display line number 2) Can't dynamically establish a console in the new window // to be improved ... ...

转载请注明原文地址:https://www.9cbs.com/read-48883.html

New Post(0)