Skip to content

Commit 04580dc

Browse files
committed
Add option to remove colors and non-ascii characters, and to format errors for IDEs
1 parent 1b17cb4 commit 04580dc

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

src/main/scala/inox/Main.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ trait MainHelpers {
4444

4545
protected def getOptions: Map[OptionDef[_], Description] = Map(
4646
optHelp -> Description(General, "Show help message"),
47+
optNoColors -> Description(General, "Disable colored output and non-ascii characters (enable this option for better support in IDEs)"),
4748
optTimeout -> Description(General, "Set a timeout for the solver (in sec.)"),
4849
optSelectedSolvers -> Description(General, {
4950
"Use solvers s1,s2,...\nAvailable: " +
@@ -244,8 +245,9 @@ object Main extends MainHelpers {
244245
}
245246
}
246247

248+
val asciiOnly = ctx.options.findOptionOrDefault(optNoColors)
247249
ctx.reporter.whenDebug(utils.DebugSectionTimers) { debug =>
248-
ctx.timers.outputTable(debug)
250+
ctx.timers.outputTable(debug, asciiOnly)
249251
}
250252

251253
exit(error = error)

src/main/scala/inox/Options.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,4 @@ object optSelectedSolvers extends SetOptionDef[String] {
231231
override def formatDefault: String = default.mkString(",")
232232
}
233233

234+
object optNoColors extends inox.FlagOptionDef("no-colors", false)

src/main/scala/inox/Reporter.scala

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class DefaultReporter(debugSections: Set[DebugSection]) extends Reporter(debugSe
182182

183183
def emit(msg: Message) = synchronized {
184184
println(reline(severityToPrefix(msg.severity), smartPos(msg.position) + msg.msg.toString))
185-
printLineContent(msg.position)
185+
printLineContent(msg.position, false)
186186
}
187187

188188
def getLine(pos: Position): Option[String] = {
@@ -201,7 +201,7 @@ class DefaultReporter(debugSections: Set[DebugSection]) extends Reporter(debugSe
201201

202202
val blankPrefix = " " * prefixSize
203203

204-
def printLineContent(pos: Position): Unit = {
204+
def printLineContent(pos: Position, asciiOnly: Boolean): Unit = {
205205
getLine(pos) match {
206206
case Some(line) =>
207207
println(blankPrefix+line)
@@ -218,10 +218,16 @@ class DefaultReporter(debugSections: Set[DebugSection]) extends Reporter(debugSe
218218
("^" * width)+"..."
219219
}
220220

221-
println(blankPrefix+(" " * (bp.col - 1) + Console.RED+carret+Console.RESET))
221+
if (asciiOnly)
222+
println(blankPrefix+(" " * (bp.col - 1) + carret))
223+
else
224+
println(blankPrefix+(" " * (bp.col - 1) + Console.RED+carret+Console.RESET))
222225

223226
case op: OffsetPosition =>
224-
println(blankPrefix+(" " * (op.col - 1) + Console.RED+"^"+Console.RESET))
227+
if (asciiOnly)
228+
println(blankPrefix+(" " * (op.col - 1) + "^"))
229+
else
230+
println(blankPrefix+(" " * (op.col - 1) + Console.RED+"^"+Console.RESET))
225231
}
226232
case None =>
227233
}
@@ -234,12 +240,26 @@ class DefaultReporter(debugSections: Set[DebugSection]) extends Reporter(debugSe
234240
}
235241

236242
class PlainTextReporter(debugSections: Set[DebugSection]) extends DefaultReporter(debugSections) {
237-
override protected def severityToPrefix(sev: Severity): String = sev match {
238-
case ERROR => "[ Error ]"
239-
case WARNING => "[Warning ]"
240-
case INFO => "[ Info ]"
241-
case FATAL => "[ Fatal ]"
242-
case INTERNAL => "[Internal]"
243-
case DEBUG(_) => "[ Debug ]"
243+
override protected def severityToPrefix(sev: Severity): String = ""
244+
245+
protected def severityToString(sev: Severity): String = sev match {
246+
case ERROR => "error"
247+
case WARNING => "warning"
248+
case INFO => "info"
249+
case FATAL => "fatal"
250+
case INTERNAL => "internal"
251+
case DEBUG(_) => "debug"
252+
}
253+
254+
override def emit(msg: Message) = synchronized {
255+
if (msg.severity == ERROR || msg.severity == FATAL || msg.severity == INTERNAL)
256+
println(smartPos(msg.position) + "error: " + msg.msg.toString)
257+
else if (msg.severity == WARNING)
258+
println(smartPos(msg.position) + "warning: " + msg.msg.toString)
259+
else if (msg.severity.isInstanceOf[DEBUG])
260+
println(smartPos(msg.position) + "debug: " + msg.msg.toString)
261+
else
262+
println(smartPos(msg.position) + msg.msg.toString)
263+
printLineContent(msg.position, true)
244264
}
245265
}

src/main/scala/inox/utils/ASCIIHelpers.scala

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,42 @@ object ASCIIHelpers {
6464
(0 until cellsPerRow).map(i => constraints.getOrElse((i, i), 1))
6565
}
6666

67-
def render: String = {
67+
def render(asciiOnly: Boolean): String = {
6868
val colSizes = computeColumnSizes
6969
val titleWidth = trimNonPrintable(title).length
7070
val fullWidth = Math.max(colSizes.sum + colSizes.size * 2, titleWidth + 7)
7171
val padWidth = fullWidth - titleWidth - 5
7272

7373
val sb = new StringBuffer
7474

75-
sb append " ┌─" + ("" * titleWidth) + "─┐\n"
76-
sb append "╔═╡ " + title + "" + ("" * padWidth) + "\n"
77-
sb append "║ └─" + ("" * titleWidth) + "─┘" + (" " * padWidth) + "\n"
75+
val barH = if (asciiOnly) "#" else ""
76+
val topLeft = if (asciiOnly) "#" else ""
77+
val topRight = if (asciiOnly) "#" else ""
78+
val bottomLeft = if (asciiOnly) "#" else ""
79+
val bottomRight = if (asciiOnly) "#" else ""
80+
val dots = if (asciiOnly) "." else ""
81+
val doubleBarH = if (asciiOnly) "#" else ""
82+
val doubleBarV = if (asciiOnly) "#" else ""
83+
val doubleTopLeft = if (asciiOnly) "#" else ""
84+
val doubleTopRight = if (asciiOnly) "#" else ""
85+
val doubleBottomLeft = if (asciiOnly) "#" else ""
86+
val doubleBottomRight = if (asciiOnly) "#" else ""
87+
88+
val leftConnect = if (asciiOnly) "#" else ""
89+
val rightConnect = if (asciiOnly) "#" else ""
90+
val doubleRightConnect = if (asciiOnly) "#" else ""
91+
val doubleLeftConnect = if (asciiOnly) "#" else ""
92+
93+
sb append s" $topLeft$barH" + (barH * titleWidth) + s"$barH$topRight\n"
94+
sb append s"$doubleTopLeft$doubleBarH$leftConnect " + title + s" $rightConnect" + (doubleBarH * padWidth) + s"$doubleTopRight\n"
95+
sb append s"$doubleBarV $bottomLeft$barH" + (barH * titleWidth) + s"$barH$bottomRight" + (" " * padWidth) + s"$doubleBarV\n"
7896

7997
for (r <- rows) r match {
8098
case Separator =>
81-
sb append "" + ("" * fullWidth) + "\n"
99+
sb append doubleRightConnect + (dots * fullWidth) + s"$doubleLeftConnect\n"
82100

83101
case Row(cells) =>
84-
sb append " "
102+
sb append s"$doubleBarV "
85103
var i = 0
86104
for (c <- cells) {
87105
if (i > 0) {
@@ -102,10 +120,10 @@ object ASCIIHelpers {
102120

103121
i += c.spanning
104122
}
105-
sb append "\n"
123+
sb append s" $doubleBarV\n"
106124
}
107125

108-
sb append "" + ("" * fullWidth) + ""
126+
sb append doubleBottomLeft + (doubleBarH * fullWidth) + doubleBottomRight
109127

110128
sb.toString
111129
}

src/main/scala/inox/utils/Timer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ final class TimerStorage private(val _name: Option[String])
100100
*
101101
* NOTE All timers must have been stopped before calling this method.
102102
*/
103-
def outputTable(printer: String => Unit): Unit = {
103+
def outputTable(printer: String => Unit, asciiOnly: Boolean): Unit = {
104104
import utils.ASCIIHelpers.{ Cell, Right, Row, Separator, Table }
105105

106106
var table = Table("Timers")
@@ -148,7 +148,7 @@ final class TimerStorage private(val _name: Option[String])
148148
}
149149

150150
rec("", this)
151-
printer(table.render)
151+
printer(table.render(asciiOnly))
152152
}
153153
}
154154

0 commit comments

Comments
 (0)