Summarize a shell command (possibly multi-line) by truncating it (if needed) to the specified width and putting "..." - Java Native OS

Java examples for Native OS:Shell Command

Description

Summarize a shell command (possibly multi-line) by truncating it (if needed) to the specified width and putting "..."

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 Arapiki Solutions Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   ww w.j av a 2  s. com
 *    "Peter Smith <psmith@arapiki.com>" - initial API and 
 *        implementation and/or initial documentation
 *******************************************************************************/

public class Main{
    public static void main(String[] argv) throws Exception{
        String shellCommand = "java2s.com";
        int width = 2;
        System.out.println(getCommandSummary(shellCommand,width));
    }
    /**
     * Summarize a shell command (possibly multi-line) by truncating it (if needed) to the
     * specified width and putting "..." to indicate that it continues.
     * 
     * @param shellCommand The original shell command string (possibly multi-line).
     * @param width       The number of characters to truncate it to.
     * @return             The summary string.
     *  
     */
    public static String getCommandSummary(String shellCommand, int width) {

        /* 
         * For now, we treat all commands as being the same, and simply return the
         * first 'width' characters from the action's command string.
         */
        String command = ShellCommandUtils.joinCommandLine(shellCommand);

        /* for strings that are longer than 'width', truncate them and suffix them with "..." */
        boolean dotsNeeded = false;
        int stringLen = command.length();
        if (stringLen > width - 3) {
            stringLen = width - 3;
            dotsNeeded = true;
        }

        /* form the summary string, possibly with ... */
        StringBuffer sb = new StringBuffer(command.substring(0, stringLen));
        if (dotsNeeded) {
            sb.append("...");
        }

        /* return the summary string */
        return sb.toString();
    }
    /**
     * Given a (possibly) multi-line shell command, merge that command into a single line.
     * If a particular line is terminated by a \ character, the next line is considered
     * to be the same command. If not, the current line and next line will be joined together
     * with "&&".
     * <p>
     * Finally, leading spaces/tabs are removed off all lines.
     * 
     * @param cmdLine The (possibly) multi-line shell command.
     * @return The joined-together shell command.
     */
    public static String joinCommandLine(String cmdLine) {

        /* 
         * Create a StringBuffer for storing the result. We'll append to this
         * as we progress through the lines.
         */
        StringBuffer sb = new StringBuffer(256);

        /* 
         * We'll traverse the string, line by line, with startPos being the start
         * index of the next line.
         */
        int startPos = 0;

        /* we'll track whether the next line should be prepend by && */
        boolean andAndRequiredNextTime = false;

        /* repeat until we've processed every line of the input */
        int cmdLineLen = cmdLine.length();
        while (startPos < cmdLineLen) {
            /*
             * Find the index of the end of the current line (possibly the end of the whole
             * string).
             */
            int nlPos = cmdLine.indexOf('\n', startPos);
            if (nlPos == -1) {
                nlPos = cmdLine.length();
            }

            /* trim any leading spaces/tabs */
            while (startPos < nlPos) {
                char ch = cmdLine.charAt(startPos);
                if ((ch != ' ') && (ch != '\t')) {
                    break;
                }
                startPos++;
            }

            /* ignore lines that are empty */
            if (startPos < nlPos) {

                /* if the previous line didn't end with \, we should insert a && */
                if (andAndRequiredNextTime) {
                    sb.append(" && ");
                }

                /* 
                 * Does this line end with \? If so, we discard the \ and merge this
                 * line with the next (no && required)
                 */
                if (cmdLine.charAt(nlPos - 1) == '\\') {
                    sb.append(cmdLine.substring(startPos, nlPos - 1));
                    andAndRequiredNextTime = false;
                }

                /* 
                 * No it doesn't, so print a && before the next line
                 */
                else {
                    sb.append(cmdLine.substring(startPos, nlPos));
                    andAndRequiredNextTime = true;
                }
            }

            /* 
             * Empty lines require a && before the next command, since it's
             * definitely the case that the previous command has ended.
             */
            else {
                andAndRequiredNextTime = true;
            }

            /* the next line starts immediately after this one */
            startPos = nlPos + 1;
        }

        return sb.toString();
    }
}

Related Tutorials