001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment.cli;
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.io.StringReader;
023    
024    import jline.ConsoleReader;
025    import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
026    
027    /**
028     * Various helpers for deployment.
029     *
030     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
031     */
032    public class DeployUtils extends ConfigIDExtractor {
033        /**
034         * Split up an output line so it indents at beginning and end (to fit in a
035         * typical terminal) and doesn't break in the middle of a word.
036         *
037         * @param source The unformatted String
038         * @param indent The number of characters to indent on the left
039         * @param endCol The maximum width of the entire line in characters,
040         *               including indent (indent 10 with endCol 70 results
041         *               in 60 "usable" characters).
042         */
043        public static String reformat(String source, int indent, int endCol) {
044            if (endCol - indent < 10) {
045                throw new IllegalArgumentException("This is ridiculous!");
046            }
047            StringBuffer buf = new StringBuffer((int) (source.length() * 1.1));
048            String prefix = indent == 0 ? "" : buildIndent(indent);
049            try {
050                BufferedReader in = new BufferedReader(new StringReader(source));
051                String line;
052                int pos;
053                while ((line = in.readLine()) != null) {
054                    if (buf.length() > 0) {
055                        buf.append('\n');
056                    }
057                    while (line.length() > 0) {
058                        line = prefix + line;
059                        if (line.length() > endCol) {
060                            pos = line.lastIndexOf(' ', endCol);
061                            if (pos < indent) {
062                                pos = line.indexOf(' ', endCol);
063                                if (pos < indent) {
064                                    pos = line.length();
065                                }
066                            }
067                            buf.append(line.substring(0, pos)).append('\n');
068                            if (pos < line.length() - 1) {
069                                line = line.substring(pos + 1);
070                            } else {
071                                break;
072                            }
073                        } else {
074                            buf.append(line).append("\n");
075                            break;
076                        }
077                    }
078                }
079            } catch (IOException e) {
080                throw (AssertionError) new AssertionError("This should be impossible").initCause(e);
081            }
082            return buf.toString();
083        }
084    
085        public static void println(String line, int indent, ConsoleReader consoleReader) throws IOException {
086            int endCol = consoleReader.getTermwidth();
087            int start = consoleReader.getCursorBuffer().cursor;
088            if (endCol - indent < 10) {
089                throw new IllegalArgumentException("This is ridiculous!");
090            }
091    //        StringBuffer buf = new StringBuffer((int)(source.length()*1.1));
092            String prefix = indent == 0 ? "" : buildIndent(indent);
093            int pos;
094            while (line.length() > 0) {
095                if (start == 0) {
096                    line = prefix + line;
097                }
098                if (line.length() > endCol - start) {
099                    pos = line.lastIndexOf(' ', endCol - start);
100                    if (pos < indent) {
101                        pos = line.indexOf(' ', endCol - start);
102                        if (pos < indent) {
103                            pos = line.length();
104                        }
105                    }
106                    consoleReader.printString(line.substring(0, pos));
107                    consoleReader.printNewline();
108                    if (pos < line.length() - 1) {
109                        line = line.substring(pos + 1);
110                    } else {
111                        break;
112                    }
113                    start = 0;
114                } else {
115                    consoleReader.printString(line);
116                    consoleReader.printNewline();
117                    break;
118                }
119            }
120        }
121    
122        public static void printTo(String string, int col, ConsoleReader consoleReader) throws IOException {
123            consoleReader.printString(string);
124            for (int i = string.length(); i < col; i++) {
125                consoleReader.printString(" ");
126            }
127        }
128    
129        private static String buildIndent(int indent) {
130            StringBuffer buf = new StringBuffer(indent);
131            for (int i = 0; i < indent; i++) {
132                buf.append(' ');
133            }
134            return buf.toString();
135        }
136    
137    
138    }