Skip to content

Instantly share code, notes, and snippets.

@busti
Last active February 14, 2019 18:01
Show Gist options
  • Save busti/7d45ee2554068e1743d912084cfcf5b7 to your computer and use it in GitHub Desktop.
Save busti/7d45ee2554068e1743d912084cfcf5b7 to your computer and use it in GitHub Desktop.
object Interpolators {
implicit class Regex(sc: StringContext) {
def r = new util.matching.Regex(sc.parts.mkString, sc.parts.tail.map(_ => "x"): _*)
}
implicit class IndentHelper(val sc: StringContext) extends AnyVal {
import sc._
def process = StringContext.treatEscapes _
def ind(args: Any*): String = {
checkLengths(args)
parts.zipAll(args, "", "").foldLeft("") { case (a, (part, arg)) =>
val processed = process(part)
val prefix = processed.split("\n").last match {
case r"""([\s|]+)$d.*""" => d
case _ => "3333"
}
val indented = arg
.toString
.split("\n")
.zipWithIndex
.map { case (s, i) =>
if (i < 1) {
s + "\n"
} else {
prefix + s + "\n"
}
}
.mkString
a + processed + indented
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment