Skip to content

Commit 862b5d9

Browse files
committed
Use color for showing megaphases
1 parent 1776d81 commit 862b5d9

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

compiler/src/dotty/tools/dotc/config/CliCommand.scala

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,34 @@ trait CliCommand:
141141
/** Used for the formatted output of -Xshow-phases */
142142
protected def phasesMessage(using ctx: Context): String =
143143

144+
import scala.io.AnsiColor.*
145+
val colors = Array(GREEN, YELLOW, /*BLUE,*/ MAGENTA, CYAN, RED)
144146
val phases = new Compiler().phases
145147
val nameLimit = 25
146148
val maxCol = ctx.settings.pageWidth.value
147149
val maxName = phases.flatten.map(_.phaseName.length).max
148150
val width = maxName.min(nameLimit)
149151
val maxDesc = maxCol - (width + 6)
150-
val fmt = s"%${width}.${width}s %.${maxDesc}s%n"
152+
val colorSlot = if ctx.useColors then GREEN.length.toString else "0"
153+
val fmt = s"%.${colorSlot}s%${width}.${width}s%.${colorSlot}s %.${maxDesc}s%n"
154+
def plain(name: String, description: String) = fmt.format("", name, "", description)
151155

152156
val sb = new StringBuilder
153-
sb ++= fmt.format("phase name", "description")
154-
sb ++= fmt.format("----------", "-----------")
155-
156-
phases.foreach {
157-
case List(single) =>
158-
sb ++= fmt.format(single.phaseName, single.description)
159-
case Nil => ()
160-
case more =>
161-
sb ++= fmt.format(s"{", "")
162-
more.foreach { mini => sb ++= fmt.format(mini.phaseName, mini.description) }
163-
sb ++= fmt.format(s"}", "")
157+
sb ++= plain("phase name", "description")
158+
sb ++= plain("----------", "-----------")
159+
160+
def color(index: Int): String = colors(index % colors.length)
161+
def emit(index: Int)(phase: core.Phases.Phase): Unit = sb ++= fmt.format(color(index), phase.phaseName, RESET, phase.description)
162+
def group(index: Int)(body: Int => Unit): Unit =
163+
if !ctx.useColors then sb ++= plain(s"{", "")
164+
body(index)
165+
if !ctx.useColors then sb ++= plain(s"}", "")
166+
167+
phases.zipWithIndex.foreach { (phase, index) =>
168+
phase match
169+
case List(single) => emit(index)(single)
170+
case Nil =>
171+
case mega => group(index)(i => mega.foreach(emit(i)))
164172
}
165173
sb.mkString
166174

0 commit comments

Comments
 (0)