Skip to content

Commit 893bf57

Browse files
committed
Use color for showing megaphases
1 parent 1b70511 commit 893bf57

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
@@ -138,26 +138,34 @@ trait CliCommand:
138138
/** Used for the formatted output of -Xshow-phases */
139139
protected def phasesMessage(using ctx: Context): String =
140140

141+
import scala.io.AnsiColor.*
142+
val colors = Array(GREEN, YELLOW, /*BLUE,*/ MAGENTA, CYAN, RED)
141143
val phases = new Compiler().phases
142144
val nameLimit = 25
143145
val maxCol = ctx.settings.pageWidth.value
144146
val maxName = phases.flatten.map(_.phaseName.length).max
145147
val width = maxName.min(nameLimit)
146148
val maxDesc = maxCol - (width + 6)
147-
val fmt = s"%${width}.${width}s %.${maxDesc}s%n"
149+
val colorSlot = if ctx.useColors then GREEN.length.toString else "0"
150+
val fmt = s"%.${colorSlot}s%${width}.${width}s%.${colorSlot}s %.${maxDesc}s%n"
151+
def plain(name: String, description: String) = fmt.format("", name, "", description)
148152

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

0 commit comments

Comments
 (0)