Package org.jboss.aesh.console

Examples of org.jboss.aesh.console.Console


                console.getShell().out().println("^C");
                console.clearBufferAndDisplayPrompt();
            }
        });

        final Console exampleConsole = new Console(builder.create());

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=foo");
                    co.setOffset(0);
                }
                /*
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=bar");
                    co.setOffset(0);
                }
                */
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=foo");
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=bar");
                if(co.getBuffer().equals("--")) {
                    commands.add("--help-");
                }
                if("--help-me".startsWith(co.getBuffer())) {
                    commands.add("--help-me");
                }
                if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                    co.setOffset(0);
                }
                if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                if(co.getBuffer().equals("testing")) {
                    commands.add("testing YAY");
                }
                if(co.getBuffer().equals("val") ||
                        co.getBuffer().equals("value ")) {
                    commands.add("value 1");
                    commands.add("value 2");
                    commands.add("value 10");
                    commands.add("value 20");
                }
                if(co.getBuffer().equals("valu"))
                    commands.add("value 10");

                co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);

        final ConsoleCallback consoleCallback = new ConsoleCallback() {
            @Override
            public int readConsoleOutput(ConsoleOperation output) {
                try {
                //To change body of implemented methods use File | Settings | File Templates.
                exampleConsole.getShell().out().print("======>\"" + output.getBuffer() + "\"\n");
                if(masking) {
                    exampleConsole.getShell().out().print("got password: " + output.getBuffer() + ", stopping masking");
                    masking = false;
                    exampleConsole.setPrompt(prompt);
                }
                else if (output.getBuffer().equalsIgnoreCase("quit") || output.getBuffer().equalsIgnoreCase("exit") ||
                        output.getBuffer().equalsIgnoreCase("reset")) {
                    exampleConsole.stop();
                }
                else if(output.getBuffer().equalsIgnoreCase("password")) {
                    masking = true;
                    exampleConsole.setPrompt(new Prompt("password: ", (char) 0));
                }
                else if(output.getBuffer().startsWith("blah")) {
                    exampleConsole.getShell().err().print("blah. command not found.\n");
                    exampleConsole.getShell().out().print("BAH" + Config.getLineSeparator());
                }
                else if(output.getBuffer().equals("clear"))
                    exampleConsole.clear();
                else if(output.getBuffer().startsWith("man")) {
                    //exampleConsole.attachProcess(test);
                    ConsoleCommand test =
                            new ExampleConsoleCommand(exampleConsole, output);
                    exampleConsole.attachProcess(test);
                }
                else if(output.getBuffer().startsWith("login")) {
                    exampleConsole.setConsoleCallback(passwordCallback);
                    exampleConsole.setPrompt(new Prompt("Username: "));
                }
                 return 0;
                }
                catch (IOException ioe) {
                    exampleConsole.getShell().out().println("Exception: "+ioe.getMessage());
                    return -1;
                }
            }
        };
        exampleConsole.setConsoleCallback(consoleCallback);
        exampleConsole.start();
        exampleConsole.setPrompt(prompt);

        passwordCallback = new ConsoleCallback() {
            private boolean hasUsername = false;

            @Override
            public int readConsoleOutput(ConsoleOperation output) {
                if(hasUsername) {
                    password = output.getBuffer();
                    hasPassword = true;
                    exampleConsole.getShell().out().print("Username: " + username + ", password: " + password + Config.getLineSeparator());
                    exampleConsole.setPrompt(prompt);
                    exampleConsole.setConsoleCallback(consoleCallback);
                }
                else {
                    username = output.getBuffer();
                    exampleConsole.setPrompt( new Prompt("Password: ", (char) 0));
                    hasUsername = true;
                }
                return 0;
            }
        };

        //show how we can change the prompt async
        try {
            Thread.sleep(4000);
            exampleConsole.setPrompt(new Prompt(
                    new TerminalString("[FOO]» ", new TerminalColor( Color.RED, Color.DEFAULT), new TerminalTextStyle(CharacterType.BOLD))));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

View Full Code Here


                console.getShell().out().println("^C");
                console.clearBufferAndDisplayPrompt();
            }
        });

        final Console exampleConsole = new Console(builder.create());

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=foo");
                    co.setOffset(0);
                }
                /*
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=bar");
                    co.setOffset(0);
                }
                */
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=foo");
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=bar");
                if(co.getBuffer().equals("--")) {
                    commands.add("--help-");
                }
                if("--help-me".startsWith(co.getBuffer())) {
                    commands.add("--help-me");
                }
                if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                    co.setOffset(0);
                }
                if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                if(co.getBuffer().equals("testing")) {
                    commands.add("testing YAY");
                }
                if(co.getBuffer().equals("val") ||
                        co.getBuffer().equals("value ")) {
                    commands.add("value 1");
                    commands.add("value 2");
                    commands.add("value 10");
                    commands.add("value 20");
                }
                if(co.getBuffer().equals("valu"))
                    commands.add("value 10");

                co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);

        final ConsoleCallback consoleCallback = new AeshConsoleCallback() {
            @Override
            public int execute(ConsoleOperation output) {
                try {
                //To change body of implemented methods use File | Settings | File Templates.
                exampleConsole.getShell().out().print("======>\"" + output.getBuffer() + "\"\n");
                if(masking) {
                    exampleConsole.getShell().out().print("got password: " + output.getBuffer() + ", stopping masking");
                    masking = false;
                    exampleConsole.setPrompt(prompt);
                }
                else if (output.getBuffer().equalsIgnoreCase("quit") || output.getBuffer().equalsIgnoreCase("exit") ||
                        output.getBuffer().equalsIgnoreCase("reset")) {
                    exampleConsole.stop();
                }
                else if(output.getBuffer().equalsIgnoreCase("password")) {
                    masking = true;
                    exampleConsole.setPrompt(new Prompt("password: ", (char) 0));
                }
                else if(output.getBuffer().startsWith("blah")) {
                    exampleConsole.getShell().err().print("blah. command not found.\n");
                    exampleConsole.getShell().out().print("BAH" + Config.getLineSeparator());
                }
                else if(output.getBuffer().equals("clear"))
                    exampleConsole.clear();
                else if(output.getBuffer().startsWith("man")) {
                    //exampleConsole.attachProcess(test);
                    //man = new ExampleConsoleCommand(exampleConsole, output);
                    exampleConsole.getShell().out().println("trying to wait for input");
                    CommandOperation co = null;
                    try {
                        co = getInput();
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                        return -1;
                    }
                    exampleConsole.getShell().out().println("got: " + co.toString());
                    //exampleConsole.attachProcess(test);
                }
                else if(output.getBuffer().startsWith("login")) {
                    exampleConsole.setConsoleCallback(passwordCallback);
                    exampleConsole.setPrompt(new Prompt("Username: "));
                }
                 return 0;
                }
                catch (IOException ioe) {
                    exampleConsole.getShell().out().println("Exception: "+ioe.getMessage());
                    return -1;
                }
            }
        };

        exampleConsole.setConsoleCallback(consoleCallback);
        exampleConsole.start();
        exampleConsole.setPrompt(prompt);

        passwordCallback = new AeshConsoleCallback() {
            private boolean hasUsername = false;

            @Override
            public int execute(ConsoleOperation output) {
                if(hasUsername) {
                    password = output.getBuffer();
                    hasPassword = true;
                    exampleConsole.getShell().out().print("Username: " + username + ", password: " + password + Config.getLineSeparator());
                    exampleConsole.setPrompt(prompt);
                    exampleConsole.setConsoleCallback(consoleCallback);
                }
                else {
                    username = output.getBuffer();
                    exampleConsole.setPrompt( new Prompt("Password: ", (char) 0));
                    hasUsername = true;
                }
                return 0;
            }
        };

        //show how we can change the prompt async
        try {
            Thread.sleep(4000);
            exampleConsole.setPrompt(new Prompt(
                    new TerminalString("[FOO]» ", new TerminalColor( Color.RED, Color.DEFAULT), new TerminalTextStyle(CharacterType.BOLD))));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

View Full Code Here

   private void interactiveRun() throws IOException {
      config = new ConfigImpl(SystemUtils.getAppConfigFolder("InfinispanShell"));
      config.load();
      Settings settings = Settings.getInstance();
      settings.setAliasEnabled(false);
      console = new Console();
      context.setOutputAdapter(new ConsoleIOAdapter(console));
      console.addCompletion(new Completer(context));
      ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
      sessionPingTask = executor.scheduleWithFixedDelay(new PingTask(), SESSION_PING_TIMEOUT, SESSION_PING_TIMEOUT, TimeUnit.SECONDS);
View Full Code Here

                    e.printStackTrace();
                }
            }
        });

        final Console exampleConsole = new Console(builder.create());

        final ConsoleCommand test = new ConsoleCommand(exampleConsole) {

            @Override
            protected void afterAttach() throws IOException {
                if(!hasRedirectOut()) {
                    console.pushToStdOut(ANSI.getAlternateBufferScreen());
                }

                readFromFile();

                //detach after init if hasRedirectOut()
                if(hasRedirectOut()) {
                    detach();
                }
            }

            @Override
            protected void afterDetach() throws IOException {
                if(!hasRedirectOut())
                    console.pushToStdOut(ANSI.getMainBufferScreen());
            }

            private void readFromFile() throws IOException {
                if(getConsoleOutput().getStdOut() != null &&
                        getConsoleOutput().getStdOut().length() > 0) {
                    console.pushToStdOut("FROM STDOUT: "+getConsoleOutput().getStdOut());
                }
                else
                    console.pushToStdOut("here should we present some text... press 'q' to quit");
            }

            @Override
            public void processOperation(Operation operation) throws IOException {
                if(operation.getInput()[0] == 'q') {
                    detach();
                }
                else if(operation.getInput()[0] == 'a') {
                    readFromFile();
                }
                else {

                }
            }
        };

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=foo");
                    co.setOffset(0);
                }
                /*
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=bar");
                    co.setOffset(0);
                }
                */
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=foo");
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=bar");
                if(co.getBuffer().equals("--")) {
                    commands.add("--help-");
                }
                if("--help-me".startsWith(co.getBuffer())) {
                    commands.add("--help-me");
                }
                if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                    co.setOffset(0);
                }
                if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                if(co.getBuffer().equals("testing")) {
                    commands.add("testing YAY");
                }
                co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);

        final ConsoleOutput line;
        final ConsoleCallback consoleCallback = new ConsoleCallback() {
            @Override
            public int readConsoleOutput(ConsoleOutput output) throws IOException{
                //To change body of implemented methods use File | Settings | File Templates.

                exampleConsole.pushToStdOut("======>\"" + output.getBuffer() + "\"\n");
                if(masking) {
                    exampleConsole.pushToStdOut("got password: "+output.getBuffer()+", stopping masking");
                    masking = false;
                    exampleConsole.setPrompt(prompt);
                }
                else if (output.getBuffer().equalsIgnoreCase("quit") || output.getBuffer().equalsIgnoreCase("exit") ||
                        output.getBuffer().equalsIgnoreCase("reset")) {
                    exampleConsole.stop();
                }
                else if(output.getBuffer().equalsIgnoreCase("password")) {
                    masking = true;
                    exampleConsole.setPrompt(new Prompt("password: ", (char) 0));
                }
                else if(output.getBuffer().startsWith("blah")) {
                    exampleConsole.pushToStdErr("blah. command not found.\n");
                    exampleConsole.pushToStdOut("BAH"+ Config.getLineSeparator());
                }
                else if(output.getBuffer().equals("clear"))
                    exampleConsole.clear();
                else if(output.getBuffer().startsWith("man")) {
                    //exampleConsole.attachProcess(test);
                    test.attach(output);
                }
                else if(output.getBuffer().startsWith("login")) {
                    exampleConsole.setConsoleCallback(passwordCallback);
                    exampleConsole.setPrompt(new Prompt("Username: "));
                }
                 return 0;
            }
        };
        exampleConsole.setConsoleCallback(consoleCallback);
        exampleConsole.start();
        exampleConsole.setPrompt(prompt);

        passwordCallback = new ConsoleCallback() {
            private boolean hasUsername = false;

            @Override
            public int readConsoleOutput(ConsoleOutput output) throws IOException {
                if(hasUsername) {
                    password = output.getBuffer();
                    hasPassword = true;
                    exampleConsole.pushToStdOut("Username: "+username+", password: "+password+Config.getLineSeparator());
                    exampleConsole.setPrompt(prompt);
                    exampleConsole.setConsoleCallback(consoleCallback);
                }
                else {
                    username = output.getBuffer();
                    exampleConsole.setPrompt( new Prompt("Password: ", (char) 0));
                    hasUsername = true;
                }
                return 0;
            }
        };

        //show how we can change the prompt async
        try {
            Thread.sleep(4000);
            exampleConsole.setPrompt(new Prompt(
                    new TerminalString("[FOO]» ", Color.DEFAULT_BG, Color.RED_TEXT, CharacterType.BOLD)));
            //exampleConsole.pushToStdOut(new TerminalString("PUSHING", Color.DEFAULT_BG, Color.BLUE_TEXT).getAsString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
View Full Code Here

        //Settings.getInstance().setAnsiConsole(false);
        SettingsBuilder settingsBuilder = new SettingsBuilder();
        settingsBuilder.readInputrc(false);
        settingsBuilder.logging(true);
        final Console exampleConsole = new Console(settingsBuilder.create());

        PrintWriter out = new PrintWriter(System.out);

        final Man man = new Man(exampleConsole);
        //man.addPage(new File("/tmp/README.md"), "test");

        final Harlem harlem = new Harlem(exampleConsole);

        final Less less = new Less(exampleConsole);
        final More more = new More(exampleConsole);

        List<org.jboss.aesh.extensions.choice.console.MultipleChoice> choices = new ArrayList<org.jboss.aesh.extensions.choice.console.MultipleChoice>();
        choices.add(new org.jboss.aesh.extensions.choice.console.MultipleChoice(1,"Do you want foo?"));
        choices.add(new org.jboss.aesh.extensions.choice.console.MultipleChoice(2,"Do you want bar?"));

        final MultipleChoice choice =
                new MultipleChoice(exampleConsole, "choice", choices);

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                else if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                else if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                else if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                else if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                else if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                else if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                else if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                 co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);
        exampleConsole.addCompletion(man);
        exampleConsole.addCompletion(less);
        exampleConsole.addCompletion(more);
        exampleConsole.addCompletion(harlem);

        exampleConsole.setPrompt(new Prompt("[test@foo]~> "));
        //exampleConsole.pushToConsole(ANSI.greenText());
        //while ((consoleOutput = exampleConsole.read("[test@foo.bar]~> ")) != null) {
        exampleConsole.setConsoleCallback(new AeshConsoleCallback() {
            @Override
            public int execute(ConsoleOperation consoleOutput) {
                try {
                    String line = consoleOutput.getBuffer();
                    exampleConsole.getShell().out().print("======>\"" + line + "\"\n");

                    if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit") ||
                            line.equalsIgnoreCase("reset")) {
                        exampleConsole.stop();
                    }
                    if(line.equals("clear"))
                        exampleConsole.clear();
                    if(line.startsWith("man")) {
                        //exampleConsole.attachProcess(test);
                        //man.setCurrentManPage("test");
                        try {
                            man.setFile("/tmp/test.txt.gz");
                            man.setConsole(exampleConsole);
                            man.setControlOperator(consoleOutput.getControlOperator());
                            //exampleConsole.attachProcess(man);
                        }
                        catch (IllegalArgumentException iae) {
                            exampleConsole.getShell().out().print(iae.getMessage());
                        }
                    }
                    if(line.startsWith("choice")) {

                        //exampleConsole.attachProcess(choice);
                    }
                    if(line.startsWith("harlem")) {
                        //exampleConsole.attachProcess(harlem);
                        harlem.afterAttach();
                    }
                    if(line.trim().startsWith("less")) {
                        //is it getting input from pipe
                        if(exampleConsole.getShell().in().getStdIn().available() > 0) {
                            java.util.Scanner s = new java.util.Scanner(exampleConsole.getShell().in().getStdIn()).useDelimiter("\\A");
                            String fileContent = s.hasNext() ? s.next() : "";
                            less.setInput(fileContent);
                            less.setControlOperator(consoleOutput.getControlOperator());
                            //exampleConsole.attachProcess(less);
                            less.afterAttach();

                        }
                        else if(line.length() > "less".length()) {
                            File f = new File(Parser.switchEscapedSpacesToSpacesInWord(line.substring("less ".length())).trim());
                            if(f.isFile()) {
                                //less.setPage(f);
                                less.setFile(f);
                                less.setControlOperator(consoleOutput.getControlOperator());
                                //exampleConsole.attachProcess(less);
                                less.afterAttach();
                            }
                            else if(f.isDirectory()) {
                                exampleConsole.getShell().out().print(f.getAbsolutePath()+": is a directory"+
                                        Config.getLineSeparator());
                            }
                            else {
                                exampleConsole.getShell().out().print(f.getAbsolutePath()+": No such file or directory"+
                                        Config.getLineSeparator());
                            }
                        }
                        else {
                            exampleConsole.getShell().out().print("Missing filename (\"less --help\" for help)\n");
                        }
                    }

                    if(line.startsWith("more")) {
                        if(exampleConsole.getShell().in().getStdIn().available() > 0) {
                            java.util.Scanner s = new java.util.Scanner(exampleConsole.getShell().in().getStdIn()).useDelimiter("\\A");
                            String fileContent = s.hasNext() ? s.next() : "";
                            more.setInput(fileContent);
                            more.setControlOperator(consoleOutput.getControlOperator());
                            //exampleConsole.attachProcess(more);
                            more.afterAttach();

                        }
                        else {
                            File f = new File(Parser.switchEscapedSpacesToSpacesInWord(line.substring("more ".length())).trim());
                            if(f.isFile()) {
                                more.setFile(f);
                                more.setControlOperator(consoleOutput.getControlOperator());
                                //exampleConsole.attachProcess(more);
                                more.afterAttach();

                            }
                            else if(f.isDirectory()) {
                                exampleConsole.getShell().out().print(f.getAbsolutePath()+": is a directory"+
                                        Config.getLineSeparator());
                            }
                            else {
                                exampleConsole.getShell().out().print(f.getAbsolutePath()+": No such file or directory"+
                                        Config.getLineSeparator());
                            }
                        }
                    }
                    return 0;
                }
                catch(IOException ioe) {
                    return -1;
                }
            }
        });

        exampleConsole.start();
    }
View Full Code Here

                    console.stop();
                }
            }
        });

        final Console exampleConsole = new Console(builder.create());

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=foo");
                    co.setOffset(0);
                }
                /*
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=bar");
                    co.setOffset(0);
                }
                */
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=foo");
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=bar");
                if(co.getBuffer().equals("--")) {
                    commands.add("--help-");
                }
                if("--help-me".startsWith(co.getBuffer())) {
                    commands.add("--help-me");
                }
                if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                    co.setOffset(0);
                }
                if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                if(co.getBuffer().equals("testing")) {
                    commands.add("testing YAY");
                }
                if(co.getBuffer().equals("val") ||
                        co.getBuffer().equals("value ")) {
                    commands.add("value 1");
                    commands.add("value 2");
                    commands.add("value 10");
                    commands.add("value 20");
                }
                if(co.getBuffer().equals("valu"))
                    commands.add("value 10");

                co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);

        final ConsoleCallback consoleCallback = new AeshConsoleCallback() {
            @Override
            public int execute(ConsoleOperation output) throws InterruptedException {
                try {
                //To change body of implemented methods use File | Settings | File Templates.
                exampleConsole.getShell().out().println("======>\"" + output.getBuffer());
                if(masking) {
                    exampleConsole.getShell().out().print("got password: " + output.getBuffer() + ", stopping masking");
                    masking = false;
                    exampleConsole.setPrompt(prompt);
                }
                else if (output.getBuffer().equalsIgnoreCase("quit") || output.getBuffer().equalsIgnoreCase("exit") ||
                        output.getBuffer().equalsIgnoreCase("reset")) {
                    exampleConsole.stop();
                }
                else if(output.getBuffer().equalsIgnoreCase("password")) {
                    masking = true;
                    exampleConsole.setPrompt(new Prompt("password: ", (char) 0));
                }
                else if(output.getBuffer().startsWith("blah")) {
                    exampleConsole.getShell().err().println("blah. command not found.");
                    exampleConsole.getShell().out().print("BAH" + Config.getLineSeparator());
                }
                else if(output.getBuffer().equals("clear"))
                    exampleConsole.clear();
                else if(output.getBuffer().startsWith("man")) {
                    //exampleConsole.attachProcess(test);
                    //man = new ExampleConsoleCommand(exampleConsole, output);
                    exampleConsole.getShell().out().println("trying to wait for input");
                    CommandOperation co = null;
                    try {
                        co = getInput();
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                        return -1;
                    }
                    exampleConsole.getShell().out().println("got: " + co.toString());
                    //exampleConsole.attachProcess(test);
                }
                else if(output.getBuffer().startsWith("login")) {
                    exampleConsole.setConsoleCallback(passwordCallback);
                    exampleConsole.setPrompt(new Prompt("Username: "));
                }
                 return 0;
                }
                catch (IOException ioe) {
                    exampleConsole.getShell().out().println("Exception: "+ioe.getMessage());
                    return -1;
                }
            }
        };

        exampleConsole.setConsoleCallback(consoleCallback);
        exampleConsole.start();
        exampleConsole.setPrompt(prompt);

        passwordCallback = new AeshConsoleCallback() {
            private boolean hasUsername = false;

            @Override
            public int execute(ConsoleOperation output) throws InterruptedException {
                if(hasUsername) {
                    password = output.getBuffer();
                    hasPassword = true;
                    exampleConsole.getShell().out().print("Username: " + username + ", password: " + password + Config.getLineSeparator());
                    exampleConsole.setPrompt(prompt);
                    exampleConsole.setConsoleCallback(consoleCallback);
                }
                else {
                    username = output.getBuffer();
                    exampleConsole.setPrompt( new Prompt("Password: ", (char) 0));
                    hasUsername = true;
                }
                return 0;
            }
        };

        //show how we can change the prompt async
        try {
            Thread.sleep(4000);
            exampleConsole.setPrompt(new Prompt(
                    new TerminalString("[FOO]» ", new TerminalColor( Color.RED, Color.DEFAULT), new TerminalTextStyle(CharacterType.BOLD))));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

View Full Code Here

                    e.printStackTrace();
                }
            }
        });

        final Console exampleConsole = Console.getInstance();

        final ConsoleCommand test = new ConsoleCommand(exampleConsole) {

            @Override
            protected void afterAttach() throws IOException {
                if(!hasRedirectOut()) {
                    console.pushToStdOut(ANSI.getAlternateBufferScreen());
                }

                readFromFile();

                //detach after init if hasRedirectOut()
                if(hasRedirectOut()) {
                    detach();
                }
            }

            @Override
            protected void afterDetach() throws IOException {
                if(!hasRedirectOut())
                    console.pushToStdOut(ANSI.getMainBufferScreen());
            }

            private void readFromFile() throws IOException {
                if(getConsoleOutput().getStdOut() != null &&
                        getConsoleOutput().getStdOut().length() > 0) {
                    console.pushToStdOut("FROM STDOUT: "+getConsoleOutput().getStdOut());
                }
                else
                    console.pushToStdOut("here should we present some text... press 'q' to quit");
            }

            @Override
            public void processOperation(Operation operation) throws IOException {
                if(operation.getInput()[0] == 'q') {
                    detach();
                }
                else if(operation.getInput()[0] == 'a') {
                    readFromFile();
                }
                else {

                }
            }
        };

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=foo");
                    co.setOffset(0);
                }
                /*
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=bar");
                    co.setOffset(0);
                }
                */
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=foo");
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=bar");
                if(co.getBuffer().equals("--")) {
                    commands.add("--help-");
                }
                if("--help-me".startsWith(co.getBuffer())) {
                    commands.add("--help-me");
                }
                if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                    co.setOffset(0);
                }
                if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                if(co.getBuffer().equals("testing")) {
                    commands.add("testing YAY");
                }
                co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);

        final ConsoleOutput line;
        final ConsoleCallback consoleCallback = new ConsoleCallback() {
            @Override
            public int readConsoleOutput(ConsoleOutput output) throws IOException{
                //To change body of implemented methods use File | Settings | File Templates.

                exampleConsole.pushToStdOut("======>\"" + output.getBuffer() + "\"\n");
                if(masking) {
                    exampleConsole.pushToStdOut("got password: "+output.getBuffer()+", stopping masking");
                    masking = false;
                    exampleConsole.setPrompt(prompt);
                }
                else if (output.getBuffer().equalsIgnoreCase("quit") || output.getBuffer().equalsIgnoreCase("exit") ||
                        output.getBuffer().equalsIgnoreCase("reset")) {
                    exampleConsole.stop();
                }
                else if(output.getBuffer().equalsIgnoreCase("password")) {
                    masking = true;
                    exampleConsole.setPrompt(new Prompt("password: ", (char) 0));
                }
                else if(output.getBuffer().startsWith("blah")) {
                    exampleConsole.pushToStdErr("blah. command not found.\n");
                    exampleConsole.pushToStdOut("BAH"+ Config.getLineSeparator());
                }
                else if(output.getBuffer().equals("clear"))
                    exampleConsole.clear();
                else if(output.getBuffer().startsWith("man")) {
                    //exampleConsole.attachProcess(test);
                    test.attach(output);
                }
                else if(output.getBuffer().startsWith("login")) {
                    exampleConsole.setConsoleCallback(passwordCallback);
                    exampleConsole.setPrompt(new Prompt("Username: "));
                }
                 return 0;
            }
        };
        exampleConsole.setConsoleCallback(consoleCallback);
        exampleConsole.start();
        exampleConsole.setPrompt(prompt);

        passwordCallback = new ConsoleCallback() {
            private boolean hasUsername = false;

            @Override
            public int readConsoleOutput(ConsoleOutput output) throws IOException {
                if(hasUsername) {
                    password = output.getBuffer();
                    hasPassword = true;
                    exampleConsole.pushToStdOut("Username: "+username+", password: "+password+Config.getLineSeparator());
                    exampleConsole.setPrompt(prompt);
                    exampleConsole.setConsoleCallback(consoleCallback);
                }
                else {
                    username = output.getBuffer();
                    exampleConsole.setPrompt( new Prompt("Password: ", (char) 0));
                    hasUsername = true;
                }
                return 0;
            }
        };



        try {
            Thread.sleep(4000);
            exampleConsole.setPrompt(new Prompt("[FOO]» "));
        } catch (InterruptedException e) {
            e.printStackTrace()//To change body of catch statement use File | Settings | File Templates.
        }

    }
View Full Code Here

   private void interactiveRun() throws IOException {
      config = new ConfigImpl(SystemUtils.getAppConfigFolder("InfinispanShell"));
      config.load();
      Settings settings = Settings.getInstance();
      settings.setAliasEnabled(false);
      console = new Console();
      context.setOutputAdapter(new ConsoleIOAdapter(console));
      console.addCompletion(new Completer(context));
      ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
      sessionPingTask = executor.scheduleWithFixedDelay(new PingTask(), SESSION_PING_TIMEOUT, SESSION_PING_TIMEOUT, TimeUnit.SECONDS);
View Full Code Here

                    e.printStackTrace();
                }
            }
        });

        final Console exampleConsole = Console.getInstance();

        final ConsoleCommand test = new ConsoleCommand(exampleConsole) {

            @Override
            protected void afterAttach() throws IOException {
                if(!hasRedirectOut()) {
                    console.pushToStdOut(ANSI.getAlternateBufferScreen());
                }

                readFromFile();

                //detach after init if hasRedirectOut()
                if(hasRedirectOut()) {
                    detach();
                }
            }

            @Override
            protected void afterDetach() throws IOException {
                if(!hasRedirectOut())
                    console.pushToStdOut(ANSI.getMainBufferScreen());
            }

            private void readFromFile() throws IOException {
                if(getConsoleOutput().getStdOut() != null &&
                        getConsoleOutput().getStdOut().length() > 0) {
                    console.pushToStdOut("FROM STDOUT: "+getConsoleOutput().getStdOut());
                }
                else
                    console.pushToStdOut("here should we present some text... press 'q' to quit");
            }

            @Override
            public void processOperation(Operation operation) throws IOException {
                if(operation.getInput()[0] == 'q') {
                    detach();
                }
                else if(operation.getInput()[0] == 'a') {
                    readFromFile();
                }
                else {

                }
            }
        };

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=foo");
                    co.setOffset(0);
                }
                /*
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=bar");
                    co.setOffset(0);
                }
                */
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=foo");
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=bar");
                if(co.getBuffer().equals("--")) {
                    commands.add("--help-");
                }
                if("--help-me".startsWith(co.getBuffer())) {
                    commands.add("--help-me");
                }
                if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                    co.setOffset(0);
                }
                if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                if(co.getBuffer().equals("testing")) {
                    commands.add("testing YAY");
                }
                co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);

        final ConsoleOutput line;
        final ConsoleCallback consoleCallback = new ConsoleCallback() {
            @Override
            public int readConsoleOutput(ConsoleOutput output) throws IOException{
                //To change body of implemented methods use File | Settings | File Templates.

                exampleConsole.pushToStdOut("======>\"" + output.getBuffer() + "\"\n");
                if(masking) {
                    exampleConsole.pushToStdOut("got password: "+output.getBuffer()+", stopping masking");
                    masking = false;
                    exampleConsole.setPrompt(prompt);
                }
                else if (output.getBuffer().equalsIgnoreCase("quit") || output.getBuffer().equalsIgnoreCase("exit") ||
                        output.getBuffer().equalsIgnoreCase("reset")) {
                    exampleConsole.stop();
                }
                else if(output.getBuffer().equalsIgnoreCase("password")) {
                    masking = true;
                    exampleConsole.setPrompt(new Prompt("password: ", (char) 0));
                }
                else if(output.getBuffer().startsWith("blah")) {
                    exampleConsole.pushToStdErr("blah. command not found.\n");
                    exampleConsole.pushToStdOut("BAH"+ Config.getLineSeparator());
                }
                else if(output.getBuffer().equals("clear"))
                    exampleConsole.clear();
                else if(output.getBuffer().startsWith("man")) {
                    //exampleConsole.attachProcess(test);
                    test.attach(output);
                }
                else if(output.getBuffer().startsWith("login")) {
                    exampleConsole.setConsoleCallback(passwordCallback);
                    exampleConsole.setPrompt(new Prompt("Username: "));
                }
                 return 0;
            }
        };
        exampleConsole.setConsoleCallback(consoleCallback);
        exampleConsole.start();
        exampleConsole.setPrompt(prompt);

        passwordCallback = new ConsoleCallback() {
            private boolean hasUsername = false;

            @Override
            public int readConsoleOutput(ConsoleOutput output) throws IOException {
                if(hasUsername) {
                    password = output.getBuffer();
                    hasPassword = true;
                    exampleConsole.pushToStdOut("Username: "+username+", password: "+password+Config.getLineSeparator());
                    exampleConsole.setPrompt(prompt);
                    exampleConsole.setConsoleCallback(consoleCallback);
                }
                else {
                    username = output.getBuffer();
                    exampleConsole.setPrompt( new Prompt("Password: ", (char) 0));
                    hasUsername = true;
                }
                return 0;
            }
        };



        try {
            Thread.sleep(4000);
            exampleConsole.setPrompt(new Prompt("[FOO]» "));
        } catch (InterruptedException e) {
            e.printStackTrace()//To change body of catch statement use File | Settings | File Templates.
        }

    }
View Full Code Here

                    e.printStackTrace();
                }
            }
        });

        Console exampleConsole = new Console();

        ConsoleCommand test = new ConsoleCommand(exampleConsole) {

            @Override
            protected void afterAttach() throws IOException {
                if(!hasRedirectOut()) {
                    console.pushToStdOut(ANSI.getAlternateBufferScreen());
                }

                readFromFile();

                //detach after init if hasRedirectOut()
                if(hasRedirectOut()) {
                    detach();
                }
            }

            @Override
            protected void afterDetach() throws IOException {
                if(!hasRedirectOut())
                    console.pushToStdOut(ANSI.getMainBufferScreen());
            }

            private void readFromFile() throws IOException {
                if(getConsoleOutput().getStdOut() != null &&
                        getConsoleOutput().getStdOut().length() > 0) {
                    console.pushToStdOut("FROM STDOUT: "+getConsoleOutput().getStdOut());
                }
                else
                    console.pushToStdOut("here should we present some text... press 'q' to quit");
            }

            @Override
            public void processOperation(Operation operation) throws IOException {
                if(operation.getInput()[0] == 'q') {
                    detach();
                }
                else if(operation.getInput()[0] == 'a') {
                    readFromFile();
                }
                else {

                }
            }
        };

        Completion completer = new Completion() {
            @Override
            public void complete(CompleteOperation co) {
                // very simple completor
                List<String> commands = new ArrayList<String>();
                if(co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) {
                    commands.add("foo");
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                    commands.add("foobbx");
                    commands.add("foobcx");
                    commands.add("foobdx");
                }
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=foo");
                    co.setOffset(0);
                }
                /*
                if(co.getBuffer().equals("p")) {
                    commands.add("profile=bar");
                    co.setOffset(0);
                }
                */
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=foo");
                if(co.getBuffer().equals("profile="))
                    commands.add("profile=bar");
                if(co.getBuffer().equals("--")) {
                    commands.add("--help-");
                }
                //if(co.getBuffer().startsWith("--help-") || co.getBuffer().startsWith("--help-m")) {
                if("--help-me".startsWith(co.getBuffer())) {
                    commands.add("--help-me");
                }
                if(co.getBuffer().equals("fooba")) {
                    commands.add("foobaa");
                    commands.add("foobar");
                    commands.add("foobaxxxxxx");
                }
                if(co.getBuffer().equals("foobar")) {
                    commands.add("foobar");
                }
                if(co.getBuffer().equals("bar")) {
                    commands.add("bar/");
                }
                if(co.getBuffer().equals("h")) {
                    commands.add("help.history");
                    commands.add("help");
                    co.setOffset(0);
                }
                if(co.getBuffer().equals("help")) {
                    commands.add("help.history");
                    commands.add("help");
                }
                if(co.getBuffer().equals("help.")) {
                    commands.add("help.history");
                }
                if(co.getBuffer().equals("deploy")) {
                    commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre");
                }
                if(co.getBuffer().equals("testing")) {
                    commands.add("testing YAY");
                }
                co.setCompletionCandidates(commands);
            }
        };

        exampleConsole.addCompletion(completer);

        ConsoleOutput line;
        exampleConsole.pushToStdOut(ANSI.greenBackground());
        //while ((line = exampleConsole.read(prompt)) != null) {
        //while ((line = exampleConsole.read("[test@foo.bar]~> ")) != null) {
        while ((line = exampleConsole.read(prompt, null)) != null) {
            exampleConsole.pushToStdOut("======>\"" + line.getBuffer() + "\"\n");

            if (line.getBuffer().equalsIgnoreCase("quit") || line.getBuffer().equalsIgnoreCase("exit") ||
                    line.getBuffer().equalsIgnoreCase("reset")) {
                break;
            }
            if(line.getBuffer().equalsIgnoreCase("password")) {
                line = exampleConsole.read(new Prompt("password: "), Character.valueOf((char) 0));
                exampleConsole.pushToStdOut("password typed:" + line.getBuffer() + "\n");

            }
            //test stdErr
            if(line.getBuffer().startsWith("blah")) {
                exampleConsole.pushToStdErr("blah. command not found.\n");
            }
            if(line.getBuffer().equals("clear"))
                exampleConsole.clear();
            if(line.getBuffer().startsWith("man")) {
                //exampleConsole.attachProcess(test);
                test.attach(line);
            }
        }
        if(line != null && line.getBuffer().equals("reset")) {
            exampleConsole.stop();
            exampleConsole = new Console();

            while ((line = exampleConsole.read("> ")) != null) {
                exampleConsole.pushToStdOut("======>\"" + line + "\"\n");
                if (line.getBuffer().equalsIgnoreCase("quit") || line.getBuffer().equalsIgnoreCase("exit") ||
                        line.getBuffer().equalsIgnoreCase("reset")) {
                    break;
                }

            }
        }

        try {
            exampleConsole.stop();
        } catch (Exception e) {
        }
    }
View Full Code Here

TOP

Related Classes of org.jboss.aesh.console.Console

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.