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 dcac38c WIP.
dcac38c is described below
commit dcac38cee48bb64200fe736a979f92a82ace1338
Author: Aaron Radzinski <[email protected]>
AuthorDate: Wed Aug 26 13:48:33 2020 -0700
WIP.
---
.../nlpcraft/common/ascii/NCAsciiTable.scala | 79 ++++++++++++++++------
.../model/tools/cmdline/NCCommandLine.scala | 14 ++--
2 files changed, 66 insertions(+), 27 deletions(-)
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 6bde0cf..180ad36 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
@@ -25,6 +25,7 @@ import org.apache.nlpcraft.common.ascii.NCAsciiTable._
import resource._
import scala.collection.JavaConverters._
+import scala.collection.mutable
/**
* `ASCII`-based table with minimal styling support.
@@ -207,15 +208,14 @@ class NCAsciiTable {
/**
* Adds row (one or more row cells) with a given style.
*
- * @param style Style to use.
- * @param cells Row cells. For multi-line cells - use `Seq(...)`.
+ * @param cells Row cells tuples (style, text). For multi-line cells - use
`Seq(...)`.
*/
- def +/(style: String, cells: Any*): NCAsciiTable = {
+ def +/(cells: (String, Any)*): NCAsciiTable = {
startRow()
cells foreach {
- case i: Iterable[_] ⇒ addStyledRowCell(style, i.iterator.toSeq: _*)
- case a ⇒ addStyledRowCell(style, a)
+ case i if i._2.isInstanceOf[Iterable[_]] ⇒ addStyledRowCell(i._1,
i._2.asInstanceOf[Iterable[_]].iterator.toSeq: _*)
+ case a ⇒ addStyledRowCell(a._1, a._2)
}
endRow()
@@ -255,13 +255,12 @@ class NCAsciiTable {
/**
* Adds styled header (one or more header cells).
*
- * @param style Style to use.
- * @param cells Header cells. For multi-line cells - use `Seq(...)`.
+ * @param cells Header cells tuples (style, text). For multi-line cells -
use `Seq(...)`.
*/
- def #/(style: String, cells: Any*): NCAsciiTable = {
+ def #/(cells: (String, Any)*): NCAsciiTable = {
cells foreach {
- case i: Iterable[_] ⇒ addStyledHeaderCell(style, i.iterator.toSeq:
_*)
- case a ⇒ addStyledHeaderCell(style, a)
+ case i if i._2.isInstanceOf[Iterable[_]] ⇒
addStyledHeaderCell(i._1, i._2.asInstanceOf[Iterable[_]].iterator.toSeq: _*)
+ case a ⇒ addStyledHeaderCell(a._1, a._2)
}
this
@@ -302,7 +301,42 @@ class NCAsciiTable {
* @param lines
* @return
*/
- private def breakUpByNearestSpace(maxWidth: Int, lines: Seq[Any]):
Seq[String] = ???
+ private def breakUpByNearestSpace(maxWidth: Int, lines: Seq[String]):
Seq[String] =
+ lines.map(_.trim).flatMap(line => {
+ if (line.isEmpty)
+ mutable.Buffer("")
+ else {
+ val buf = mutable.Buffer.empty[String]
+
+ var start = 0
+ var lastSpace = -1
+ var curr = 0
+ val len = line.length
+
+ while (curr < len) {
+ if (curr - start > maxWidth) {
+ val end = if (lastSpace == -1) curr else lastSpace + 1
/* Keep space at the end of the line. */
+
+ buf += line.substring(start, end).trim
+ start = end
+ }
+
+ if (line.charAt(curr) == ' ')
+ lastSpace = curr
+
+ curr += 1
+ }
+
+ if (start < len) {
+ val lastLine = line.substring(start).trim
+
+ if (lastLine.nonEmpty)
+ buf += lastLine
+ }
+
+ buf
+ }
+ })
/**
*
@@ -310,15 +344,16 @@ class NCAsciiTable {
* @param lines
* @return
*/
- private def mkRowCell(style: String, lines: Any*): Cell = {
+ private def mkStyledCell(style: String, lines: Any*): Cell = {
val st = Style(style)
+ val strLines = lines.map(x)
Cell(
st,
if (breakUpByWords)
- breakUpByNearestSpace(st.maxWidth, lines)
+ breakUpByNearestSpace(st.maxWidth, strLines)
else
- (for (line ← lines) yield x(line).grouped(st.maxWidth)).flatten
+ (for (str ← strLines) yield str.grouped(st.maxWidth)).flatten
)
}
@@ -328,7 +363,7 @@ class NCAsciiTable {
* @param lines One or more cell lines.
*/
def addHeaderCell(lines: Any*): NCAsciiTable = {
- hdr :+= mkRowCell(defaultHeaderStyle, lines: _*)
+ hdr :+= mkStyledCell(defaultHeaderStyle, lines: _*)
this
}
@@ -339,7 +374,7 @@ class NCAsciiTable {
* @param lines One or more row cells. Multiple lines will be printed on
separate lines.
*/
def addRowCell(lines: Any*): NCAsciiTable = {
- curRow :+= mkRowCell(defaultRowStyle, lines: _*)
+ curRow :+= mkStyledCell(defaultRowStyle, lines: _*)
this
}
@@ -351,7 +386,7 @@ class NCAsciiTable {
* @param lines One or more cell lines.
*/
def addStyledHeaderCell(style: String, lines: Any*): NCAsciiTable = {
- hdr :+= mkRowCell(style, lines: _*)
+ hdr :+= mkStyledCell(if (style.trim.isEmpty) defaultHeaderStyle else
style, lines: _*)
this
}
@@ -363,7 +398,7 @@ class NCAsciiTable {
* @param lines One or more row cells. Multiple lines will be printed on
separate lines.
*/
def addStyledRowCell(style: String, lines: Any*): NCAsciiTable = {
- curRow :+= mkRowCell(style, lines: _*)
+ curRow :+= mkStyledCell(if (style.trim.isEmpty) defaultRowStyle else
style, lines: _*)
this
}
@@ -384,9 +419,9 @@ class NCAsciiTable {
case _ ⇒ throw new AssertionError(s"Invalid align option in: $sty")
}
}
-
+
override def toString: String = mkString
-
+
/**
* Prepares output string.
*/
@@ -408,7 +443,7 @@ class NCAsciiTable {
colsNum = r.size
else if (colsNum != r.size)
assert(assertion = false, "Table with uneven rows.")
-
+
assert(colsNum > 0, "No columns found.")
// At this point all rows in the table have the
@@ -569,7 +604,7 @@ class NCAsciiTable {
* @param header Optional header.
*/
def error(log: Logger, header: Option[String] = None): Unit =
log.error(mkLogString(header))
-
+
/**
* Renders this table to log as trace.
*
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 f38a9ff..60edc1c 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
@@ -138,7 +138,10 @@ object NCCommandLine extends App {
val tbl = NCAsciiTable().margin(left = 4)
if (params.isEmpty) // Default - show abbreviated help.
- CMDS.foreach(cmd => tbl += (cmd.names.mkString(", "),
cmd.synopsis))
+ CMDS.foreach(cmd => tbl +/ (
+ ("" -> cmd.names.mkString(", ")),
+ ("align:left, maxWidth:65" -> cmd.synopsis)
+ ))
else if (cmd.isParamPresent("all", params)) { // Show a full format
help for all commands.
CMDS.foreach(cmd => {
var lines = mutable.Buffer.empty[String]
@@ -148,13 +151,14 @@ object NCCommandLine extends App {
else
lines += cmd.synopsis
- lines += ""
-
if (cmd.params.nonEmpty) {
- lines += "PARAMETERS"
+ lines ++= Seq("", "PARAMETERS")
}
- tbl += (cmd.names.mkString(", "), lines)
+ tbl +/ (
+ ("" -> cmd.names.mkString(", ")),
+ ("align:left, maxWidth:65" -> lines)
+ )
})
}