Example #1
0
    commandTextHandler: function(e) {
        var line      = e.currentTarget.getValue(),
            //idx       = cmdHistory.indexOf(line),
            hisLength = cmdHistory.length,
            newVal    = "",
            code      = e.keyCode;
        if (cmdBuffer === null || (this.commandHistoryIndex == 0 && cmdBuffer !== line))
            cmdBuffer = line;
        parser.parseLine(line);

        if (code == 38) { //UP
            if (this.$winHints.visible) {
                this.selectHintUp();
            }
            else {
                if (!hisLength)
                    return;
                newVal = cmdHistory[--this.commandHistoryIndex];
                if (this.commandHistoryIndex < 0)
                    this.commandHistoryIndex = 0;
                if (newVal)
                    e.currentTarget.setValue(newVal);
            }
            return false;
        }
        else if (code == 40) { //DOWN
            if (this.$winHints.visible) {
                this.selectHintDown();
            }
            else {
                if (!hisLength)
                    return;
                newVal = cmdHistory[++this.commandHistoryIndex] || "";//(++idx > hisLength - 1 || idx === 0) ? (cmdBuffer || "") : 
                if (this.commandHistoryIndex >= cmdHistory.length)
                    this.commandHistoryIndex = cmdHistory.length;
                e.currentTarget.setValue(newVal);
            }
            return false;
        }
        else if (code == 27 && this.$winHints.visible) {
            return this.hideHints();
        }
        else if (code != 13 && code != 9) {
            this.autoComplete(e, parser, 2);
            return;
        }
        
        if (this.$winHints.visible && selectedHint && hintNodes)
            return this.hintClick(hintNodes[selectedHint]);

        if (parser.argv.length === 0) {
            // no commmand line input

            if (e.name == "keydown") {
                //this.log(this.getPrompt(), "prompt");
                //this.enable();
            }
        }
        else if (parser.argQL[0]) {
            // first argument quoted -> error
            this.write("Syntax error: first argument quoted.");
        }
        else {
            var s,
                cmd = parser.argv[parser.argc++];

            if (code == 9) {
                this.autoComplete(e, parser, 1);
                return false;
            }

            this.commandHistoryIndex = cmdHistory.push(line);
            cmdBuffer = null;
            e.currentTarget.setValue(newVal);
            this.hideHints();

            this.log(this.getPrompt() + " " + parser.argv.join(" "), "prompt");
            this.enable();
            tabConsole.set("console");

            switch (cmd) {
                case "help":
                    this.help();
                    break;
                case "clear":
                    txtConsole.clear();
                    break;
                case "sudo":
                    s = parser.argv.join(" ").trim();
                    if (s == "sudo make me a sandwich") {
                        this.write("Okay.");
                        break;
                    }
                    else if (s == "sudo apt-get moo") {
                        //this.clear();
                        this.write([" ",
                            "        (__)",
                            "        (oo)",
                            "  /------\\/ ",
                            " / |    ||  ",
                            "*  /\\---/\\  ",
                            "   ~~   ~~  ",
                            "....\"Have you mooed today?\"...",
                            " "]);
                        break;
                    }
                    else {
                        this.write("E: Invalid operation " + parser.argv[parser.argc++]);
                        break;
                    }
                case "man":
                    var pages = {
                        "last": "Man, last night was AWESOME.",
                        "help": "Man, help me out here.",
                        "next": "Request confirmed; you will be reincarnated as a man next.",
                        "cat":  "You are now riding a half-man half-cat."
                    };
                    this.write((pages[parser.argv[parser.argc++]]
                        || "Oh, I'm sure you can figure it out."));
                    break;
                case "locate":
                    var keywords = {
                        "ninja": "Ninja can not be found!",
                        "keys": "Have you checked your coat pocket?",
                        "joke": "Joke found on user.",
                        "problem": "Problem exists between keyboard and chair.",
                        "raptor": "BEHIND YOU!!!"
                    };
                    this.write((keywords[parser.argv[parser.argc++]] || "Locate what?"));
                    break;
                default:
                    var jokes = {
                        "make me a sandwich": "What? Make it yourself.",
                        "make love": "I put on my robe and wizard hat.",
                        "i read the source code": "<3",
                        //"pwd": "You are in a maze of twisty passages, all alike.",
                        "lpr": "PC LOAD LETTER",
                        "hello joshua": "How about a nice game of Global Thermonuclear War?",
                        "xyzzy": "Nothing happens.",
                        "date": "March 32nd",
                        "hello": "Why hello there!",
                        "who": "Doctor Who?",
                        "su": "God mode activated. Remember, with great power comes great ... aw, screw it, go have fun.",
                        "f**k": "I have a headache.",
                        "whoami": "You are Richard Stallman.",
                        "nano": "Seriously? Why don't you just use Notepad.exe? Or MS Paint?",
                        "top": "It's up there --^",
                        "moo":"moo",
                        "ping": "There is another submarine three miles ahead, bearing 225, forty fathoms down.",
                        "find": "What do you want to find? Kitten would be nice.",
                        "more":"Oh, yes! More! More!",
                        "your gay": "Keep your hands off it!",
                        "hi":"Hi.",
                        "echo": "Echo ... echo ... echo ...",
                        "bash": "You bash your head against the wall. It's not very effective.",
                        "ssh": "ssh, this is a library.",
                        "uname": "Illudium Q-36 Explosive Space Modulator",
                        "finger": "Mmmmmm...",
                        "kill": "Terminator deployed to 1984.",
                        "use the force luke": "I believe you mean source.",
                        "use the source luke": "I'm not luke, you're luke!",
                        "serenity": "You can't take the sky from me.",
                        "enable time travel": "TARDIS error: Time Lord missing.",
                        "ed": "You are not a diety."
                    };
                    s = parser.argv.join(" ").trim();
                    if (jokes[s]) {
                        this.write(jokes[s]);
                        break;
                    }
                    else {
                        var data = {
                            command: cmd,
                            argv: parser.argv,
                            line: line,
                            cwd: this.getCwd()
                        };
                        ide.dispatchEvent("track_action", {type: "console", cmd: cmd});
                        if (ext.execCommand(cmd, data) !== false) {
                            if (ide.dispatchEvent("consolecommand." + cmd, {
                              data: data
                            }) !== false) {
                                if (!ide.onLine)
                                    this.write("Cannot execute command. You are currently offline.");
                                else
                                    ide.socket.send(JSON.stringify(data));
                            }
                        }
                        return;
                    }
            }
        }
    },
Example #2
0
    commandTextHandler: function(e) {
        var line = e.currentTarget.getValue();
        if (cmdBuffer === null || (cmdHistory._index === 0 && cmdBuffer !== line)) {
            cmdBuffer = line;
        }

        parser.parseLine(line);

        var code = e.keyCode;
        var hisLength = cmdHistory.length();
        var hintsVisible = Hints.visible();

        if (code === KEY_UP) { //UP
            if (hintsVisible) {
                Hints.selectUp();
            }
            else if (hisLength) {
                var newVal = cmdHistory.getPrev();

                if (newVal)
                    e.currentTarget.setValue(newVal);
            }
            return false;
        }
        else if (code === KEY_DOWN) { //DOWN
            if (hintsVisible) {
                Hints.selectDown();
            }
            else if (hisLength) {
                e.currentTarget.setValue(cmdHistory.getNext() || "");
            }
            return false;
        }
        else if (code === KEY_ESC && hintsVisible) {
            return Hints.hide();
        }
        else if (code != KEY_CR && code != KEY_TAB) {
            return this.autoComplete(e, parser, 2);
        }

        if (hintsVisible && Hints.selected())
            return Hints.click(Hints.selected());

        if (parser.argv.length === 0) {
            // no commmand line input
        }
        else if (parser.argQL[0]) {
            // first argument quoted -> error
            this.write("Syntax error: first argument quoted.");
        }
        else {
            if (code === KEY_TAB) {
                this.autoComplete(e, parser, 1);
                return false;
            }

            cmdHistory.push(line);
            cmdBuffer = null;
            e.currentTarget.setValue("");
            Hints.hide();

            Logger.log(this.getPrompt() + " " + parser.argv.join(" "), "prompt");
            this.enable();
            tabConsole.set("console");

            var cmd = parser.argv[0];

            // If there is a predefined (i.e. hardcoded) output for the current
            // command being executed in the CLI, show that.
            if (Output[cmd]) {
                var rest = parser.argv.join(" ").replace(new RegExp("^" + cmd), "").trim();
                var msg = Output[cmd][rest];

                if (Output[cmd][rest])
                    this.write(msg);
                else
                    this.write(Output[cmd].__default__.replace("%s", cmd));
            }
            else {
                if (cmd === "help") {
                    this.help();
                }
                else if (cmd === "clear") {
                    txtConsole.clear();
                }
                else {
                    var rest = parser.argv.join(" ").trim();
                    if (Output.general[rest]) {
                        this.write(Output.general[rest]);
                    }
                    else {
                        if (cmd.trim().charAt(0) == "!") {
                            var bandRE = /^\s*!/;
                            cmd = "bash";
                            parser.argv[0] = parser.argv[0].replace(bandRE, "");
                            line = line.replace(bandRE, "");
                        }

                        ide.dispatchEvent("track_action", {
                            type: "console",
                            cmd: cmd
                        });

                        var data = {
                            command: cmd,
                            argv: parser.argv,
                            line: line,
                            cwd: this.getCwd()
                        };

                        if (ext.execCommand(cmd, data) !== false) {
                            var evtName = "consolecommand." + cmd;
                            if (ide.dispatchEvent(evtName, { data: data }) !== false) {
                                if (!ide.onLine)
                                    this.write("Cannot execute command. You are currently offline.");
                                else
                                    ide.send(JSON.stringify(data));
                            }
                        }
                    }
                }
            }
        }
    },