Skip to content

09 Custom rendering

Vladyslav Pekker edited this page Jan 3, 2019 · 12 revisions

⬅️ | ➡️

In the CustomRenderingModule spells defines a trait CustomRendering with a single unimplemented method rendered. It is fairly straightforward to implement:

implicit class NodeOps(node: => xml.Node) extends CustomRendering {
  def rendered(implicit availableWidthInCharacters: spells.StringOpsModule#AvailableWidthInCharacters = SpellsConfig.terminal.WidthInCharacters.value): String =
    new xml.PrettyPrinter(availableWidthInCharacters, step = 2) format node
}

🔥 CustomRendering is haunted by this issue and I don't have a workaround yet 😔

Spells contains quite a few implementations of CustomRendering, which are sometimes used as type class instances. Here are a few examples:

scala> println(61.minutes.toString) // scala.concurrent.duration.FiniteDuration
61 minutes

scala> println(61.minutes.rendered)
1 hour 1 minute
scala> println(java.util.Calendar.getInstance.toString)
java.util.GregorianCalendar[time=1451071278007,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=4,DAY_OF_MONTH=25,DAY_OF_YEAR=359,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=21,SECOND=18,MILLISECOND=7,ZONE_OFFSET=3600000,DST_OFFSET=0]

scala> println(java.util.Calendar.getInstance.rendered)
Friday, December 25, 2015 20:21:18.007 +0100 CET
scala> println(new java.util.Date().toString)
Fri Dec 25 20:21:18 CET 2015

scala> println(new java.util.Date().rendered)
Friday, December 25, 2015 20:21:18.007 +0100 CET
scala> println(new Exception().toString)
java.lang.Exception

scala> println(new Exception().rendered) // FULL stack trace printed in red
java.lang.Exception
     at $line20.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.<init>(<console>:21)
     at $line20.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.<clinit>(<console>)
     at $line20.$eval$.$print$lzycompute(<console>:7)
     at $line20.$eval$.$print(<console>:6)
     at $line20.$eval.$print(<console>)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:784)
     at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:1039)
     at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq$1.apply(IMain.scala:636)
     at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq$1.apply(IMain.scala:635)
     at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
     at scala.reflect.internal.util.AbstractFileClassLoader.asContext(AbstractFileClassLoader.scala:19)
     at scala.tools.nsc.interpreter.IMain$WrappedRequest.loadAndRunReq(IMain.scala:635)
     at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:567)
     at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:563)
     at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:802)
     at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:836)
     at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:694)
     at scala.tools.nsc.interpreter.ILoop.processLine(ILoop.scala:404)
     at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:424)
     at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:925)
     at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:911)
     at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:911)
     at scala.reflect.internal.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:97)
     at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:911)
     at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:936)
     at xsbt.ConsoleInterface.run(ConsoleInterface.scala:62)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:101)
     at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:76)
     at sbt.Console.sbt$Console$$console0$1(Console.scala:22)
     at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:23)
     at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:23)
     at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:23)
     at sbt.Logger$$anon$4.apply(Logger.scala:85)
     at sbt.TrapExit$App.run(TrapExit.scala:248)
     at java.lang.Thread.run(Unknown Source)
scala> println(((_: Int) => "Hello World!").toString)
<function1>

scala> println(((_: Int) => "Hello World!").rendered)
Int => java.lang.String
scala> println((61.minutes -> new java.util.Date).toString)
(61 minutes,Fri Dec 25 20:41:06 CET 2015)

scala> println((61.minutes -> new java.util.Date).rendered)
1 hour 1 minute -> Friday, December 25, 2015 20:41:43.784 +0100 CET
scala> println(Array(1.second, 26.hours, 61.minutes).toString)
[Lscala.concurrent.duration.FiniteDuration;@2780cdcd

scala> println(Array(1.second, 26.hours, 61.minutes).rendered)
scala.Array[scala.concurrent.duration.FiniteDuration] with 3 elements:

0  1 second
1  1 day 2 hours
2  1 hour 1 minute

⬅️ | ➡️

Clone this wiki locally