This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch NLPCRAFT-108
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-108 by this push:
     new b747c01  WIP.
b747c01 is described below

commit b747c01742ceb27e93bf2138344202a9a42ec8c3
Author: Aaron Radzinski <[email protected]>
AuthorDate: Sat Oct 3 18:33:56 2020 -0700

    WIP.
---
 .../nlpcraft/common/ansi/NCAnsiSpinner.scala       | 65 +++++++++++++++++++---
 .../nlpcraft/common/ascii/NCAsciiTable.scala       | 15 +----
 .../org/apache/nlpcraft/common/util/NCUtils.scala  | 12 ++++
 .../nlpcraft/model/tools/cmdline/NCCli.scala       |  7 ++-
 4 files changed, 77 insertions(+), 22 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
index 25badfe..11ab7d2 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
@@ -18,9 +18,11 @@
 package org.apache.nlpcraft.common.ansi
 
 import java.io.PrintStream
+import java.util.Random
 
 import NCAnsi._
 import org.apache.nlpcraft.common._
+import org.apache.nlpcraft.common.ansi.NCAnsiSpinner.RND
 
 /**
  *
@@ -28,10 +30,49 @@ import org.apache.nlpcraft.common._
 class NCAnsiSpinner(out: PrintStream = System.out, ansiColor: String = 
ansiCyanFg, useAnsi: Boolean = true) {
     @volatile var thread: Thread = _
 
-    final val SPIN_CHARS = Seq('-', '\\', '|', '/')
-    final val SPIN_CHARS_SIZE = SPIN_CHARS.size
+    final val SPIN_CHAR_SETS = Seq(
+        Seq('-', '\\', '|', '/'),
+        Seq('.', 'o', 'O', '@', '*'),
+        Seq('←', '↖', '↑', '↗', '→', '↘', '↓', '↙'),
+        Seq('▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▇', '▆', '▅', '▄', '▃', 
'▁'),
+        Seq('▖', '▘', '▝', '▗'),
+        Seq('┤', '┘', '┴', '└', '├', '┌', '┬', '┐'),
+        Seq('◢', '◣', '◤', '◥'),
+        Seq('◰', '◳', '◲', '◱'),
+        Seq('◴', '◷', '◶', '◵'),
+        Seq('◐', '◓', '◑', '◒'),
+        Seq('◡', '⊙', '◠', '⊙'),
+        Seq('⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'),
+        Seq('⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈')
+    )
 
-    var frame = 0
+    private val SPIN_CHARS = SPIN_CHAR_SETS(RND.nextInt(SPIN_CHAR_SETS.size))
+    private var rightPrompt = ""
+    private var leftPrompt = ""
+    private var lastLength = 0
+    private var frame = 0
+
+    /**
+     *
+     * @param p
+     */
+    def setRightPrompt(p: String): Unit =
+        this.rightPrompt = if (p == null) "" else p
+
+    /**
+     *
+     * @param p
+     */
+    def setLeftPrompt(p: String): Unit =
+        this.leftPrompt = if (p == null) "" else p
+
+    /**
+     *
+     */
+    private def clean(): Unit = {
+        out.print(ansiCursorLeft * lastLength)
+        out.print(ansiClearLineAfter)
+    }
 
     /**
      *
@@ -39,13 +80,15 @@ class NCAnsiSpinner(out: PrintStream = System.out, 
ansiColor: String = ansiCyanF
     def start(): Unit =
         if (useAnsi) {
             thread =  U.mkThread("ansi-spinner") { t ⇒
-                out.print(s"$ansiCursorHide")
+                out.print(ansiCursorHide)
 
                 while (!t.isInterrupted) {
                     if (frame > 0)
-                        out.print(s"$ansiCursorLeft$ansiClearLineAfter")
+                        clean()
 
-                    out.print(s"$ansiColor${SPIN_CHARS(frame % 
SPIN_CHARS_SIZE)}$ansiReset")
+                    out.print(s"$leftPrompt$ansiColor${SPIN_CHARS(frame % 
SPIN_CHARS.size)}$ansiReset$rightPrompt")
+
+                    lastLength = U.stripAnsi(leftPrompt).length + 1 + 
U.stripAnsi(rightPrompt).length
 
                     frame += 1
 
@@ -64,7 +107,13 @@ class NCAnsiSpinner(out: PrintStream = System.out, 
ansiColor: String = ansiCyanF
     def stop(): Unit = {
         U.stopThread(thread)
 
-        if (useAnsi && frame > 0)
-            out.print(s"$ansiCursorLeft$ansiClearLineAfter$ansiCursorShow")
+        if (useAnsi && frame > 0) {
+            clean()
+            out.print(ansiCursorShow)
+        }
     }
 }
+
+object NCAnsiSpinner {
+    private val RND = new Random()
+}
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
index 3b9796a..0bfcd2b 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
@@ -18,7 +18,6 @@
 package org.apache.nlpcraft.common.ascii
 
 import java.io.{IOException, PrintStream}
-import java.util.regex.Pattern
 
 import com.typesafe.scalalogging.Logger
 import org.apache.nlpcraft.common._
@@ -33,8 +32,6 @@ import scala.collection.mutable
  * `ASCII`-based table with minimal styling support.
  */
 class NCAsciiTable {
-    private final val ANSI_SEQ = Pattern.compile("\u001B\\[[;\\d]*m")
-
     /**
      * Cell style.
      */
@@ -95,7 +92,7 @@ class NCAsciiTable {
      */
     private sealed case class Cell(style: Style, lines: Seq[String]) {
         // Cell's calculated width including padding.
-        lazy val width: Int = style.padding + (if (height > 0) 
lines.map(stripAnsi(_).length).max else 0)
+        lazy val width: Int = style.padding + (if (height > 0) 
lines.map(U.stripAnsi(_).length).max else 0)
 
         // Gets height of the cell.
         lazy val height: Int = lines.length
@@ -207,14 +204,6 @@ class NCAsciiTable {
     }
 
     /**
-     *
-     * @param s
-     * @return
-     */
-    private def stripAnsi(s: String): String =
-        ANSI_SEQ.matcher(s).replaceAll("")
-
-    /**
      * Adds row (one or more row cells) with a given style.
      *
      * @param cells Row cells tuples (style, text). For multi-line cells - use 
`Seq(...)`.
@@ -449,7 +438,7 @@ class NCAsciiTable {
      * @param sty Style.
      */
     private def aligned(txt: String, width: Int, sty: Style): String = {
-        val d = width - stripAnsi(txt).length
+        val d = width - U.stripAnsi(txt).length
 
         sty.align match {
             case "center" ⇒ space(d / 2) + txt + space(d / 2 + d % 2)
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index e80f973..4e37c20 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -29,6 +29,7 @@ import java.time.{Instant, ZoneId, ZonedDateTime}
 import java.util.concurrent.atomic.AtomicInteger
 import java.util.concurrent.{ExecutorService, LinkedBlockingQueue, 
RejectedExecutionHandler, ThreadFactory, ThreadPoolExecutor, TimeUnit}
 import java.util.jar.JarFile
+import java.util.regex.Pattern
 import java.util.stream.Collectors
 import java.util.zip.{ZipInputStream, GZIPInputStream ⇒ GIS, GZIPOutputStream 
⇒ GOS}
 import java.util.{Locale, Properties, Random, Timer, TimerTask, Calendar ⇒ C}
@@ -65,6 +66,8 @@ import scala.util.{Failure, Success}
   * Project-wide, global utilities ans miscellaneous functions.
   */
 object NCUtils extends LazyLogging {
+    private final val ANSI_SEQ = Pattern.compile("\u001B\\[[?;\\d]*[a-zA-Z]")
+
     final val REGEX_FIX = "//"
     final val DSL_FIX = "^^"
 
@@ -140,6 +143,15 @@ object NCUtils extends LazyLogging {
     def nowUtcTs(): Timestamp = new Timestamp(Instant.now().toEpochMilli)
 
     /**
+     * Strips ANSI escape sequences from the given string.
+     *
+     * @param s
+     * @return
+     */
+    def stripAnsi(s: String): String =
+        ANSI_SEQ.matcher(s).replaceAll("")
+
+    /**
       * Escapes given string for JSON according to RFC 4627 
http://www.ietf.org/rfc/rfc4627.txt.
       *
       * @param s String to escape.
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index aa97354..f27258b 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -486,6 +486,7 @@ object NCCli extends App {
                 var online = false
                 val spinner = mkSpinner()
                 val timeout = currentTime + 5.mins
+                val warnTimeout = currentTime + 1.secs
 
                 spinner.start()
 
@@ -495,8 +496,12 @@ object NCCli extends App {
                     else
                         online = Try(restHealth("http://"; + 
beacon.restEndpoint) == 200).getOrElse(false)
 
-                    if (!online)
+                    if (!online) {
+                        if (currentTime > warnTimeout)
+                            spinner.setRightPrompt(s" ${r("(taking too long - 
check logs)")}")
+
                         Thread.sleep(2.secs) // Check every 2 secs.
+                    }
                 }
 
                 spinner.stop()

Reply via email to