Handle escape codes in text for centering

This commit is contained in:
Alexander Gehrke 2024-05-19 02:13:11 +02:00
parent 2dc7e0a0e4
commit 8111309560
4 changed files with 32 additions and 29 deletions

View file

@ -18,11 +18,12 @@ case class Theme(styles: Map[String, fansi.Attrs], figletFonts: Map[String, Stri
def ++(newFonts: Map[String, String])(implicit d: DummyImplicit) = copy(figletFonts = figletFonts ++ newFonts)
object Theme:
given default: Theme = Theme(Map(
given default: Theme = Theme(
Map(
"titleLine" -> (fansi.Bold.On ++ fansi.Color.DarkGray),
"code" -> fansi.Color.Yellow
"code" -> fansi.Color.Yellow,
),
Map("titleLine" -> "smbraille")
Map("titleLine" -> "smbraille"),
)
object Format:
@ -35,12 +36,12 @@ object Format:
def centerLines(str: String) = str.split("\n").map(center).mkString("\n")
def centerBlock(str: String) =
val lines = str.split("\n")
val maxLen = lines.map(_.length).max
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(_.length).sum
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

View file

@ -210,7 +210,7 @@ object TypedCommand:
c => runProcess(Vector(shell, "-c", c.mkString(" ")))
def runInteractive(using Path): Vector[String] => String =
c => { os.proc(c).call(); "" }
c => { os.proc(c).call(stdin = os.Inherit, stdout = os.Inherit, stderr = os.Inherit); "" }
def apply(cmd: String*)(using Path): TypedCommand[Vector[String]] =
TypedCommand(run, cmd.mkString(" "), cmd.toVector)

View file

@ -4,24 +4,21 @@ import os.Path
trait Templates:
def titleLine(title: String)(using theme: Theme) = Paragraph(
"\n" + Format
.figlet(title, theme.font("titleLine", "pagga"))
.block
.blue + "\n"
"\n" + Format.figlet(title, theme.font("titleLine", "pagga")).block.blue + "\n",
)
def header(using theme: Theme) = Meta((p, pos) => {
val left = p.meta.getOrElse("author", "")
val center = p.meta.getOrElse("title", "")
val right = s"${pos} / ${p.slides.size - 1}"
val right = s"${pos + 1} / ${p.slides.size}"
theme.style("titleLine")(Format.distribute(left, center, right)).text
})
def slide(title: String)(slides: Slide*)(using Theme) = Group(
Clear :: header :: titleLine(title) :: slides.toList
Clear :: header :: titleLine(title) :: slides.toList,
)
def slide(slides: Slide*)(using Theme) = Group(
Clear :: header :: slides.toList
Clear :: header :: slides.toList,
)
def markdown(title: String, content: Path)(using Theme) = slide(title)(
@ -30,11 +27,12 @@ trait Templates:
"/usr/bin/mdcat",
"--columns",
(columns * 0.8).toInt.toString,
content.toString
)(using os.pwd).block
)
content.toString,
)(using os.pwd).block,
),
)
lazy val --- = Paragraph(("═" * columns).yellow.toString)
end Templates
/* vim:set tw=120: */

View file

@ -64,7 +64,11 @@ object Terminal:
def csi = if (isTmux) "\u001bPtmux\u001b\u001b[" else "\u001b["
def osc = if (isTmux) "\u001bPtmux\u001b\u001b]" else "\u001b]"
def apc = if (isTmux) "\u001bPtmux;\u001b\u001b_" else "\u001b_"
def st = if (isTmux) "\u0007\u001b\\" else "\u0007"
def st = if (isTmux) "\u0007\u001b\\" else "\u001b\\"
def stripEscapes(str: String) =
// match CSI until letter or OSC/APC until ST
str.replaceAll("\u001b(\\[[^a-zA-Z]*[a-zA-Z]|(\\]|_).*?(\u0007|\u001b\\\\))", "")
def hideCursor() = print(s"${csi}?25l")
def showCursor() = print(s"${csi}?25h")