Package jline

Examples of jline.Terminal


    @Option(name = "--lines", description = "stop after N lines")
    int lines;

    @Override
    protected Object doExecute() throws Exception {
        Terminal term = (Terminal) session.get(".jline.terminal");
        if (term == null || !isTty(System.out)) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            return null;
        } else {
            boolean echo = term.isEchoEnabled();
            term.setEchoEnabled(false);
            try {
                if (lines == 0) {
                    lines = term.getHeight();
                }
                LineSplitter reader = new LineSplitter(new BufferedReader(new InputStreamReader(System.in)), term.getWidth());
                int count = 0;
                int c;
                do {
                    do {
                        String line;
                        if ((line = reader.readLine()) == null) {
                            return null;
                        }
                        System.out.println(line);
                    } while (++count < lines - 2);
                    c = -1;
                    while (c == -1) {
                        System.out.flush();
                        System.out.print("--More--");
                        System.out.flush();
                        c = session.getKeyboard().read();
                        switch (c) {
                            case 'q':
                            case -1:
                                c = 'q';
                                break;
                            case '\r':
                            case '\n':
                            case 14: // Down arrow
                                count--;
                                System.out.print("\r          \r");
                                break;
                            case ' ':
                                count = 0;
                                System.out.print("\r          \r");
                                break;
                            case 16: // Up arrow
                                // fall through
                            default:
                                c = -1;
                                System.out.print("\r          \r");
                                break;
                        }
                        if (c == 'q') {
                            break;
                        }
                    }
                } while (c != 'q');
                return null;
            } finally {
                term.setEchoEnabled(echo);
            }
        }
    }
View Full Code Here


            }
        } else {
            // We are going into full blown interactive shell mode.

            final TerminalFactory terminalFactory = new TerminalFactory();
            final Terminal terminal = terminalFactory.getTerminal();
            Console console = createConsole(commandProcessor, in, out, err, terminal);
            CommandSession session = console.getSession();
            session.put("USER", user);
            session.put("APPLICATION", application);
            session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
            session.put("#LINES", new Function() {
                public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                    return Integer.toString(terminal.getHeight());
                }
            });
            session.put("#COLUMNS", new Function() {
                public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                    return Integer.toString(terminal.getWidth());
                }
            });
            session.put(".jline.terminal", terminal);

            console.run();
View Full Code Here

            this.session = session;
        }

        public void start(final Environment env) throws IOException {
            try {
                final Terminal terminal = new SshTerminal(env);
                String encoding = env.getEnv().get("LC_CTYPE");
                if (encoding != null && encoding.indexOf('.') > 0) {
                    encoding = encoding.substring(encoding.indexOf('.') + 1);
                }
                Console console = new Console(commandProcessor,
                                              in,
                                              new PrintStream(new LfToCrLfFilterOutputStream(out), true),
                                              new PrintStream(new LfToCrLfFilterOutputStream(err), true),
                                              terminal,
                                              encoding,
                                              new Runnable() {
                                                  public void run() {
                                                      destroy();
                                                  }
                                              });
                final CommandSession session = console.getSession();
                session.put("APPLICATION", System.getProperty("karaf.name", "root"));
                for (Map.Entry<String,String> e : env.getEnv().entrySet()) {
                    session.put(e.getKey(), e.getValue());
                }
                session.put("#LINES", new Function() {
                    public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                        return Integer.toString(terminal.getHeight());
                    }
                });
                session.put("#COLUMNS", new Function() {
                    public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                        return Integer.toString(terminal.getWidth());
                    }
                });
                session.put(".jline.terminal", terminal);
                new Thread(console) {
                    @Override
View Full Code Here

        return null;
    }

    private int getTermWidth() {
        Terminal term = (Terminal) session.get(".jline.terminal");
        return term != null ? term.getWidth() : 80;
    }
View Full Code Here

            formatter.post(clause, builder);
        }
    }

    protected int getTermWidth() {
        Terminal term = (Terminal) session.get(".jline.terminal");
        return term != null ? term.getWidth() : 80;

    }
View Full Code Here

            this.session = session;
        }

        public void start(final Environment env) throws IOException {
            try {
                final Terminal terminal = new SshTerminal(env);
                Console console = new Console(commandProcessor,
                                              in,
                                              new PrintStream(new LfToCrLfFilterOutputStream(out), true),
                                              new PrintStream(new LfToCrLfFilterOutputStream(err), true),
                                              terminal,
                                              new Runnable() {
                                                  public void run() {
                                                      destroy();
                                                  }
                                              });
                final CommandSession session = console.getSession();
                session.put("APPLICATION", System.getProperty("karaf.name", "root"));
                for (Map.Entry<String,String> e : env.getEnv().entrySet()) {
                    session.put(e.getKey(), e.getValue());
                }
                session.put("#LINES", new Function() {
                    public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                        return Integer.toString(terminal.getHeight());
                    }
                });
                session.put("#COLUMNS", new Function() {
                    public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                        return Integer.toString(terminal.getWidth());
                    }
                });
                session.put(".jline.terminal", terminal);
                new Thread(console) {
                    @Override
View Full Code Here

            }
        } else {
            // We are going into full blown interactive shell mode.

            final TerminalFactory terminalFactory = new TerminalFactory();
            final Terminal terminal = terminalFactory.getTerminal();
            Console console = createConsole(commandProcessor, in, out, err, terminal);
            CommandSession session = console.getSession();
            session.put("USER", user);
            session.put("APPLICATION", application);
            session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
            session.put("#LINES", new Function() {
                public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                    return Integer.toString(terminal.getHeight());
                }
            });
            session.put("#COLUMNS", new Function() {
                public Object execute(CommandSession session, List<Object> arguments) throws Exception {
                    return Integer.toString(terminal.getWidth());
                }
            });
            session.put(".jline.terminal", terminal);

            console.run();
View Full Code Here

            }
        }
        SimpleLogger.setLevel(level);

        SshClient client = null;
        Terminal terminal = null;
        try {
            client = SshClient.setUpDefaultClient();
            client.start();
            int retries = 0;
            ClientSession session = null;
            do {
                ConnectFuture future = client.connect(host, port);
                future.await();
                try {
                    session = future.getSession();
                } catch (RuntimeSshException ex) {
                    if (retries++ < retryAttempts) {
                        Thread.sleep(retryDelay * 1000);
                        System.out.println("retrying (attempt " + retries + ") ...");
                    } else {
                        throw ex;
                    }
                }
            } while (session == null);
            if (!session.authPassword(user, password).await().isSuccess()) {
                throw new Exception("Authentication failure");
            }
            ClientChannel channel;
      if (sb.length() > 0) {
                channel = session.createChannel("exec", sb.append("\n").toString());
                channel.setIn(new ByteArrayInputStream(new byte[0]));
      } else {
                terminal = new TerminalFactory().getTerminal();
         channel = session.createChannel("shell");
                channel.setIn(new NoCloseInputStream(System.in));
                ((ChannelShell) channel).setupSensibleDefaultPty();
            }
            channel.setOut(AnsiConsole.wrapOutputStream(System.out));
            channel.setErr(AnsiConsole.wrapOutputStream(System.err));
            channel.open();
            channel.waitFor(ClientChannel.CLOSED, 0);
        } catch (Throwable t) {
            if (level > 1) {
                t.printStackTrace();
            } else {
                System.err.println(t.getMessage());
            }
            System.exit(1);
        } finally {
            try {
                client.stop();
            } catch (Throwable t) { }
            try {
                if (terminal != null) {
                    terminal.restore();
                }
            } catch (Throwable t) { }
        }
        System.exit(0);
    }
View Full Code Here

    public Object doExecute() throws Exception {
        if (command == null) {
            Set<String> names = (Set<String>) session.get(".commands");
            if (!names.isEmpty()) {
                Terminal term = (Terminal) session.get(".jline.terminal");
                PrintStream out = System.out;
                SortedMap<String,String> commands = new TreeMap<String,String>();
                for (String name : names) {
                    String description = null;
                    Function function = (Function) session.get(name);
                    function = unProxy(function);
                    if (function instanceof AbstractCommand) {
                        try {
                            Method mth = AbstractCommand.class.getDeclaredMethod("createNewAction");
                            mth.setAccessible(true);
                            Action action = (Action) mth.invoke(function);
                            Class<? extends Action> clazz = action.getClass();
                            Command ann = clazz.getAnnotation(Command.class);
                            description = ann.description();
                        } catch (Throwable e) {
                        }
                        if (name.startsWith("*:")) {
                            name = name.substring(2);
                        }
                        commands.put(name, description);
                    }
                }
                // Post process the commands list

                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("COMMANDS").a(Ansi.Attribute.RESET));
                for (Map.Entry<String,String> entry : commands.entrySet()) {
                    out.print("        ");
                    String key = NameScoping.getCommandNameWithoutGlobalPrefix(session, entry.getKey());
                    out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(key).a(Ansi.Attribute.RESET));
                    if (entry.getValue() != null) {
                        DefaultActionPreparator.printFormatted("                ", entry.getValue(), term != null ? term.getWidth() : 80, out);
                    }
                }
                out.println();
            }
            return null;
View Full Code Here

    }

    protected void printUsage(CommandSession session, Action action, Map<Option,Field> optionsMap, Map<Argument,Field> argsMap, PrintStream out)
    {
        Command command = action.getClass().getAnnotation(Command.class);
        Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null;
        List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
        Collections.sort(arguments, new Comparator<Argument>() {
            public int compare(Argument o1, Argument o2) {
                return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
            }
        });
        Set<Option> options = new HashSet<Option>(optionsMap.keySet());
        options.add(HELP);
        boolean globalScope = NameScoping.isGlobalScope(session, command.scope());
        if (command != null && (command.description() != null || command.name() != null))
        {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET));
            out.print("        ");
            if (command.name() != null) {
                if (globalScope) {
                    out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
                } else {
                    out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
                }
                out.println();
            }
            out.print("\t");
            out.println(command.description());
            out.println();
        }
        StringBuffer syntax = new StringBuffer();
        if (command != null)
        {
            if (globalScope) {
                syntax.append(command.name());
            } else {
                syntax.append(String.format("%s:%s", command.scope(), command.name()));
            }
        }
        if (options.size() > 0)
        {
            syntax.append(" [options]");
        }
        if (arguments.size() > 0)
        {
            syntax.append(' ');
            for (Argument argument : arguments)
            {
                if (!argument.required())
                {
                    syntax.append(String.format("[%s] ", argument.name()));
                }
                else
                {
                    syntax.append(String.format("%s ", argument.name()));
                }
            }
        }

        out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SYNTAX").a(Ansi.Attribute.RESET));
        out.print("        ");
        out.println(syntax.toString());
        out.println();
        if (arguments.size() > 0)
        {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("ARGUMENTS").a(Ansi.Attribute.RESET));
            for (Argument argument : arguments)
            {
                out.print("        ");
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(argument.name()).a(Ansi.Attribute.RESET));
                printFormatted("                ", argument.description(), term != null ? term.getWidth() : 80, out);
                if (!argument.required()) {
                    if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
                         try {
                            if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
                                argsMap.get(argument).setAccessible(true);
                                Object o = argsMap.get(argument).get(action);
                                printObjectDefaultsTo(out, o);
                            } else {
                                printDefaultsTo(out, argument.valueToShowInHelp());
                            }
                        } catch (Throwable t) {
                            // Ignore
                        }
                    }
                }
            }
            out.println();
        }
        if (options.size() > 0)
        {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("OPTIONS").a(Ansi.Attribute.RESET));
            for (Option option : options)
            {
                String opt = option.name();
                for (String alias : option.aliases())
                {
                    opt += ", " + alias;
                }
                out.print("        ");
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(opt).a(Ansi.Attribute.RESET));
                printFormatted("                ", option.description(), term != null ? term.getWidth() : 80, out);
                if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) {
                    try {
                        if(Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) {
                            optionsMap.get(option).setAccessible(true);
                            Object o = optionsMap.get(option).get(action);
                            printObjectDefaultsTo(out, o);
                        } else {
                            printDefaultsTo(out, option.valueToShowInHelp());
                        }
                    } catch (Throwable t) {
                        // Ignore
                    }
                }
            }
            out.println();
        }
        if (command.detailedDescription().length() > 0) {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET));
            String desc = loadDescription(action.getClass(), command.detailedDescription());
            printFormatted("        ", desc, term != null ? term.getWidth() : 80, out);
        }
    }
View Full Code Here

TOP

Related Classes of jline.Terminal

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.