From 813815c3c50221adc4b1e35a5ab38f71cb0fb642 Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Thu, 13 Mar 2014 14:49:00 +0000
Subject: [PATCH] OPENDJ-1292 Upgrade display is "broken" when a task has a message longer than 71 chars. - Added to ConsoleApplication a new method which wrap the text message if needed when printing a progress bar. - Modified UpgradeCli to use this new method to display ProgressNotificationCallbacks.

---
 opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java |   50 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java b/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java
index 4210c83..bc7d765 100755
--- a/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java
+++ b/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java
@@ -28,6 +28,7 @@
 package com.forgerock.opendj.cli;
 
 import static com.forgerock.opendj.cli.CliMessages.*;
+import static com.forgerock.opendj.cli.Utils.LINE_SEPARATOR;
 import static com.forgerock.opendj.cli.Utils.MAX_LINE_WIDTH;
 import static com.forgerock.opendj.cli.Utils.CONFIRMATION_MAX_TRIES;
 import static com.forgerock.opendj.cli.Utils.wrapText;
@@ -50,6 +51,8 @@
  */
 public abstract class ConsoleApplication {
 
+    private static final int PROGRESS_LINE = 70;
+
     private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 
     private final InputStream in = System.in;
@@ -60,6 +63,8 @@
 
     private final Console console = System.console();
 
+    private boolean isProgressSuite;
+
     /**
      * Defines the different line styles for output.
      */
@@ -325,13 +330,13 @@
      * @param progress
      *            The current percentage progress to print.
      */
-    public final void printProgressBar(final int linePos, final int progress) {
+    private final void printProgressBar(final int linePos, final int progress) {
         if (!isQuiet()) {
             final int spacesLeft = MAX_LINE_WIDTH - linePos - 10;
             StringBuilder bar = new StringBuilder();
             if (progress != 0) {
-                for (int i = 0; i < 50; i++) {
-                    if ((i < (Math.abs(progress) / 2)) && (bar.length() < spacesLeft)) {
+                for (int i = 0; i < PROGRESS_LINE; i++) {
+                    if (i < (Math.abs(progress) * spacesLeft) / 100 && bar.length() < spacesLeft) {
                         bar.append(".");
                     }
                 }
@@ -341,6 +346,7 @@
                 bar.append(progress).append("%     ");
             } else {
                 bar.append("FAIL");
+                isProgressSuite = false;
             }
             final int endBuilder = linePos + bar.length();
             for (int i = 0; i < endBuilder; i++) {
@@ -348,12 +354,48 @@
             }
             if (progress >= 100 || progress < 0) {
                 bar.append(EOL);
+                isProgressSuite = false;
             }
             out.print(bar.toString());
         }
     }
 
     /**
+     * Prints a progress bar on the same output stream line if not in quiet mode.
+     * If the line's length is upper than the limit, the message is wrapped and the progress
+     * bar is affected to the last one.
+     * e.g.
+     * <pre>
+     *   Changing matching rule for 'userCertificate' and 'caCertificate' to
+     *   CertificateExactMatch...............................................   100%
+     * </pre>
+     *
+     * @param msg
+     *            The message to display before the progress line.
+     * @param progress
+     *            The current percentage progress to print.
+     * @param indent
+     *            Indentation of the message.
+     */
+    public final void printProgressBar(String msg, final int progress, final int indent) {
+        if (!isQuiet()) {
+            String msgToDisplay = wrapText(msg, PROGRESS_LINE, indent);
+            if (msgToDisplay.length() > PROGRESS_LINE) {
+                final String[] msgWrapped = msgToDisplay.split(LINE_SEPARATOR);
+                if (!isProgressSuite) {
+                    for (int pos = 0; pos < msgWrapped.length - 1; pos++) {
+                        println(LocalizableMessage.raw(msgWrapped[pos]));
+                    }
+                    isProgressSuite = true;
+                }
+                msgToDisplay = msgWrapped[msgWrapped.length - 1];
+            }
+            print(LocalizableMessage.raw(msgToDisplay));
+            printProgressBar(msgToDisplay.length(), progress);
+        }
+    }
+
+    /**
      * Print a line with EOL in the output stream.
      *
      * @param msgStyle
@@ -682,7 +724,7 @@
         boolean done = false;
         int nTries = 0;
 
-        while (!done && (nTries < CONFIRMATION_MAX_TRIES)) {
+        while (!done && nTries < CONFIRMATION_MAX_TRIES) {
             nTries++;
             try {
                 v = confirmAction(prompt, defaultValue);

--
Gitblit v1.10.0