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 7b1587d WIP.
7b1587d is described below
commit 7b1587dcb733f77e04ee5fee2007e8d95bacd769
Author: Aaron Radzinski <[email protected]>
AuthorDate: Sun Sep 27 23:58:13 2020 -0700
WIP.
---
.../org/apache/nlpcraft/common/ansi/NCAnsi.scala | 15 +++++
.../nlpcraft/common/ansi/NCAnsiSpinner.scala | 67 ++++++++++++++++++++++
.../model/tools/cmdline/NCCommandLine.scala | 54 +++++++++++------
3 files changed, 119 insertions(+), 17 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
index 053688f..8accbcc 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
@@ -26,6 +26,7 @@ import org.apache.nlpcraft.common._
sealed trait NCAnsi extends LazyLogging {
import NCAnsi._
+ // Colors.
private final val BLACK = "\u001b[30m"
private final val RED = "\u001b[31m"
private final val GREEN = "\u001b[32m"
@@ -49,6 +50,20 @@ sealed trait NCAnsi extends LazyLogging {
private final val REVERSED = "\u001b[7m"
private final val INVISIBLE = "\u001b[8m"
+ // Erase functions.
+ private final val CLEAR_SCREEN = "\u001b[J"
+ private final val CLEAR_SCREEN_AFTER = "\u001b[0J"
+ private final val CLEAR_SCREEN_BEFORE = "\u001b[1J"
+ private final val CLEAR_LINE = "\u001b[K"
+ private final val CLEAR_LINE_AFTER = "\u001b[0K"
+ private final val CLEAR_LINE_BEFORE = "\u001b[1K"
+
+ // Cursor moves.
+ private final val CURSOR_UP = "\u001b[1A"
+ private final val CURSOR_DOWN = "\u001b[1B"
+ private final val CURSOR_LEFT = "\u001b[1C"
+ private final val CURSOR_RIGHT = "\u001b[1D"
+
def isEnabled: Boolean = !U.isSysEnvTrue(PROP)
def ansiBlackFg: String = if (isEnabled) BLACK else ""
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
new file mode 100644
index 0000000..6c60f8a
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nlpcraft.common.ansi
+
+import java.io.PrintStream
+
+import NCAnsi._
+import org.apache.nlpcraft.common._
+
+/**
+ *
+ */
+class NCAnsiSpinner(out: PrintStream = System.out, ansiColor: String =
ansiCyanFg) {
+ @volatile var thread: Thread = _
+
+ final val SPIN_CHARS = Seq('-', '\\', '|', '/')
+ final val SPIN_CHARS_SIZE = SPIN_CHARS.size
+
+ /**
+ *
+ */
+ def start(): Unit = {
+ thread = U.mkThread("ansi-spinner") { t ⇒
+ val ansi = NCAnsi.isEnabled
+
+ var i = 0
+
+ while (!t.isInterrupted) {
+ if (ansi) {
+ if (i > 0)
+ out.print(ansi)
+
+ out.print(s"$ansiColor${SPIN_CHARS(i %
SPIN_CHARS_SIZE)}$ansiReset")
+
+ i += 1
+ }
+ else
+ out.print(".")
+
+ Thread.sleep(if (ansi) 300 else 1000)
+ }
+ }
+
+ thread.start()
+ }
+
+ /**
+ *
+ */
+ def stop(): Unit =
+ U.stopThread(thread)
+}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
index 06d2c27..d17ef94 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
@@ -18,6 +18,7 @@
package org.apache.nlpcraft.model.tools.cmdline
import java.io.{File, FileInputStream, IOException, ObjectInputStream}
+import java.net.URL
import com.google.gson._
import org.apache.commons.lang3.SystemUtils
@@ -316,8 +317,6 @@ object NCCommandLine extends App {
* @param args Arguments, if any, for this command.
*/
private def cmdStartServer(cmd: Command, args: Seq[Argument]): Unit = {
- title()
-
// TODO
}
@@ -326,13 +325,28 @@ object NCCommandLine extends App {
* @param args Arguments, if any, for this command.
*/
private def cmdPingServer(cmd: Command, args: Seq[Argument]): Unit = {
- title()
+ val endpoint = args.find(_.parameter.id == "endpoint") match {
+ case Some(arg) ⇒ new URL(arg.value.get).toURI.toString
+ case None ⇒ "https://localhost:8081"
+ }
+ var num = args.find(_.parameter.id == "number") match {
+ case Some(arg) ⇒
+ try
+ Integer.parseInt(arg.value.get)
+ catch {
+ case _ :Exception ⇒ throw new
IllegalArgumentException(s"Invalid number of pings: ${arg.value.get}")
+ }
+
+ case None ⇒ 1
+ }
+
+
+
+
+
+
- var endpoint = "https://localhost:8081"
- var num = 1
- val endpointParam = cmd.params.find(_.id == "endpoint").get
- val numberParam = cmd.params.find(_.id == "number").get
// TODO
}
@@ -342,8 +356,6 @@ object NCCommandLine extends App {
* @param args Arguments, if any, for this command.
*/
private def cmdStopServer(cmd: Command, args: Seq[Argument]): Unit = {
- title()
-
val path = new File(SystemUtils.getUserHome, SRV_PID_PATH)
var pid = -1L
@@ -383,8 +395,6 @@ object NCCommandLine extends App {
* @param args Arguments, if any, for this command.
*/
private def cmdHelp(cmd: Command, args: Seq[Argument]): Unit = {
- title()
-
/**
*
*/
@@ -509,8 +519,6 @@ object NCCommandLine extends App {
* @param args Arguments, if any, for this command.
*/
private def cmdRepl(cmd: Command, args: Seq[Argument]): Unit = {
- title()
-
// TODO
}
@@ -521,7 +529,12 @@ object NCCommandLine extends App {
*/
private def cmdVersion(cmd: Command, args: Seq[Argument]): Unit =
if (args.isEmpty)
- `>`(s"$NAME ver. ${VER.version}, released on ${VER.date}")
+ log((
+ new NCAsciiTable
+ += ("Version:", ansiCyan(VER.version))
+ += ("Release date:", ansiCyan(VER.date.toString))
+ ).toString
+ )
else {
val isS = args.exists(_.parameter.id == "semver")
val isD = args.exists(_.parameter.id == "reldate")
@@ -545,7 +558,9 @@ object NCCommandLine extends App {
// Make sure we exit with non-zero status.
exitStatus = 1
- System.err.println(s"${ansiRed("ERR")} $msg")
+ val msg2 = if (msg.head.isLower) msg.head.toUpper + msg.tail else msg
+
+ System.err.println(s"${ansiRed("ERR")} $msg2")
}
/**
@@ -634,8 +649,11 @@ object NCCommandLine extends App {
* @param args
*/
private def boot(args: Array[String]): Unit = {
- if (args.isEmpty)
+ if (args.isEmpty) {
+ title()
+
DFLT_CMD.body(DFLT_CMD, Seq.empty)
+ }
else {
// Handle 'no-ansi' command right away and remove it from the list.
args.find(arg ⇒ NO_ANSI_CMD.names.contains(arg)) match {
@@ -643,6 +661,8 @@ object NCCommandLine extends App {
case None ⇒ ()
}
+ title()
+
val xargs = args.filter(arg ⇒ !NO_ANSI_CMD.names.contains(arg))
val cmdName = xargs.head
@@ -652,7 +672,7 @@ object NCCommandLine extends App {
try
cmd.body(cmd, processParameters(cmd, xargs.tail))
catch {
- case e: IllegalArgumentException ⇒
error(e.getLocalizedMessage)
+ case e: Exception ⇒ error(e.getLocalizedMessage)
}
case None ⇒ error(s"Unknown command: $cmdName")