Skip to content

Instantly share code, notes, and snippets.

@nafg
Created March 27, 2024 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafg/6bcc76d15615867309928219c9e30f71 to your computer and use it in GitHub Desktop.
Save nafg/6bcc76d15615867309928219c9e30f71 to your computer and use it in GitHub Desktop.
def printTable[A](rows: Seq[A], header: String*)(f: A => Seq[String]) = {
val rowsStrings = rows.map(f)
val cells = header +: rowsStrings
val widths = cells.transpose.map(_.map(_.length).max)
val format = widths.map("%-" + _ + "s").mkString(" | ")
val headerStr = format.format(header *)
val separator = widths.map("-" * _).mkString("-+-", "-+-", "-")
val rowsStr = rowsStrings.map(format.format(_ *)).mkString("\n")
println(s"$headerStr\n$separator\n$rowsStr")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment