Java List Print prettyPrintWarningMessage(final List results, final String message)

Here you can find the source of prettyPrintWarningMessage(final List results, final String message)

Description

pretty print the warning message supplied

License

Apache License

Parameter

Parameter Description
results the pretty printed message
message the message

Declaration

private static void prettyPrintWarningMessage(final List<String> results, final String message) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    private static final int TEXT_WARNING_WIDTH = 68;
    private static final String TEXT_WARNING_PREFIX = "* ";
    private static final char ESCAPE_CHAR = '\u001B';

    /**/* w w  w . j ava2 s  .  c o m*/
     * pretty print the warning message supplied
     *
     * @param results the pretty printed message
     * @param message the message
     */
    private static void prettyPrintWarningMessage(final List<String> results, final String message) {
        for (final String line : message.split("\\r?\\n")) {
            final StringBuilder builder = new StringBuilder(line);
            while (builder.length() > TEXT_WARNING_WIDTH) {
                int space = getLastSpace(builder, TEXT_WARNING_WIDTH);
                if (space <= 0) {
                    space = TEXT_WARNING_WIDTH;
                }
                results.add(String.format("%s%s", TEXT_WARNING_PREFIX, builder.substring(0, space)));
                builder.delete(0, space + 1);
            }
            results.add(String.format("%s%s", TEXT_WARNING_PREFIX, builder));
        }
    }

    /**
     * Returns the last whitespace location in string, before width characters.
     * @param message The message to break.
     * @param width The width of the line.
     * @return The last whitespace location.
     */
    private static int getLastSpace(final CharSequence message, final int width) {
        final int length = message.length();
        int stopPos = width;
        int currPos = 0;
        int lastSpace = -1;
        boolean inEscape = false;
        while (currPos < stopPos && currPos < length) {
            final char c = message.charAt(currPos);
            if (c == ESCAPE_CHAR) {
                stopPos++;
                inEscape = true;
            } else if (inEscape) {
                stopPos++;
                if (Character.isLetter(c)) {
                    inEscape = false;
                }
            } else if (Character.isWhitespace(c)) {
                lastSpace = currPos;
            }
            currPos++;
        }
        return lastSpace;
    }
}

Related

  1. prettyPrint(List userPermissions)
  2. prettyPrint(List rows)
  3. prettyPrintArgv(List argv)
  4. prettyPrintList(Collection objects, String separator)
  5. prettyPrintList(List list)
  6. prettyRoleOutput(List roles, final String delimiter)
  7. print(Collection> collections)
  8. print(int totalErrors, int totalTechnical, int totalSuccess, List filesWithErrorsList)
  9. print(Iterable list)