package de.qwertyuiop.copret
import de.qwertyuiop.copret.syntax._

def %%(command: os.Shellable*)(cwd: os.Path = null): os.CommandResult =
  os.proc(command).call(cwd = cwd)

case class Theme(styles: Map[String, fansi.Attrs], figletFonts: Map[String, String]):
  def style(key: String, default: fansi.Attrs = fansi.Attrs()) =
    styles.getOrElse(key, default)

  def font(key: String, default: String) =
    figletFonts.getOrElse(key, default)

  def extend(newStyles: Map[String, fansi.Attrs]) = copy(styles = styles ++ newStyles)
  def ++(newStyles: Map[String, fansi.Attrs])     = copy(styles = styles ++ newStyles)

  def extend(newFonts: Map[String, String])(implicit d: DummyImplicit) = copy(figletFonts = figletFonts ++ newFonts)
  def ++(newFonts: Map[String, String])(implicit d: DummyImplicit)     = copy(figletFonts = figletFonts ++ newFonts)

object Theme:
  given default: Theme = Theme(
    Map(
      "titleLine"   -> (fansi.Bold.On ++ fansi.Color.DarkGray),
      "code"        -> fansi.Color.Yellow,
    ),
    Map("titleLine" -> "smbraille"),
  )

object Format:
  def alignRight(str: String, padding: Int = 2) = " " * (columns - str.length - padding) + str + " " * padding

  def center(str: String) = " " * ((columns - str.length) / 2) + str

  def figlet(str: String, font: String): String = new String(%%("figlet", "-t", "-f", font, str)().out.bytes)

  def centerLines(str: String) = str.split("\n").map(center).mkString("\n")
  def centerBlock(str: String) =
    val lines  = str.split("\n")
    val maxLen = lines.map(s => Terminal.stripEscapes(s).length).max
    val pad    = " " * ((columns - maxLen) / 2)
    lines.map(pad + _).mkString("\n")

  def distribute(texts: String*) =
    val totalPad = columns - texts.map(s => Terminal.stripEscapes(s).length).sum
    val numPads  = texts.size - 1
    val pad      = " " * (totalPad / numPads)
    texts.init.mkString(pad) + pad + " " * (totalPad % numPads) + texts.last

  private[copret] val ticks = raw"`([^`]*)`".r

/* vim:set tw=120: */
