From 5f510bb9b954d7e44fc798782e87003b1eb4f5fb Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 6 Feb 2025 21:55:18 +0100 Subject: [PATCH] Add --indent option --- src/main/scala/de/plugh/asciiprooftree/Main.scala | 4 +++- src/main/scala/de/plugh/asciiprooftree/file/Block.scala | 2 +- src/main/scala/de/plugh/asciiprooftree/file/Formatter.scala | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/scala/de/plugh/asciiprooftree/Main.scala b/src/main/scala/de/plugh/asciiprooftree/Main.scala index 3df648e..ccf9943 100644 --- a/src/main/scala/de/plugh/asciiprooftree/Main.scala +++ b/src/main/scala/de/plugh/asciiprooftree/Main.scala @@ -12,6 +12,7 @@ val lineRe = "^\\s*ยง(?.*)$".r class Conf(args: Seq[String]) extends ScallopConf(args): val path: ScallopOption[Path] = trailArg[Path]() + val indent: ScallopOption[Int] = opt[Int](default = Some(2)) val blockRegex: ScallopOption[String] = opt[String](default = Some(blockRe.regex)) val lineRegex: ScallopOption[String] = opt[String](default = Some(lineRe.regex)) verify() @@ -19,7 +20,8 @@ class Conf(args: Seq[String]) extends ScallopConf(args): @main def main(args: String*): Unit = val conf = new Conf(args) - val formatter = Formatter(blockRe = Regex(conf.blockRegex()), lineRe = Regex(conf.lineRegex())) + val formatter = + Formatter(indent = conf.indent(), blockRe = Regex(conf.blockRegex()), lineRe = Regex(conf.lineRegex())) reformat(conf.path(), formatter) def reformat(path: Path, formatter: Formatter): Unit = diff --git a/src/main/scala/de/plugh/asciiprooftree/file/Block.scala b/src/main/scala/de/plugh/asciiprooftree/file/Block.scala index 747e511..db34282 100644 --- a/src/main/scala/de/plugh/asciiprooftree/file/Block.scala +++ b/src/main/scala/de/plugh/asciiprooftree/file/Block.scala @@ -5,7 +5,7 @@ case class Block(lines: Seq[(String, String)]): def last: (String, String) = lines.last def content: Seq[String] = lines.map((_, content) => content) - def toLines: Seq[String] = lines.map((prefix, content) => s"$prefix $content") + def toLines(indent: Int): Seq[String] = lines.map((prefix, content) => prefix + " " * indent + content) def extend(block: Block): Block = Block(lines ++ block.lines) def extend(prefix: String, content: String): Block = extend(Block(prefix, content)) diff --git a/src/main/scala/de/plugh/asciiprooftree/file/Formatter.scala b/src/main/scala/de/plugh/asciiprooftree/file/Formatter.scala index 1fdaa83..752037f 100644 --- a/src/main/scala/de/plugh/asciiprooftree/file/Formatter.scala +++ b/src/main/scala/de/plugh/asciiprooftree/file/Formatter.scala @@ -7,7 +7,7 @@ import scala.util.boundary import scala.util.matching.Regex import scala.util.matching.Regex.Match -case class Formatter(blockRe: Regex, lineRe: Regex): +case class Formatter(indent: Int, blockRe: Regex, lineRe: Regex): private val blockReI = blockRe.pattern.namedGroups().get("block") private val lineReI = lineRe.pattern.namedGroups().get("block") @@ -46,7 +46,7 @@ case class Formatter(blockRe: Regex, lineRe: Regex): for info <- findBlocks(cleanText) do if resultEnd < info.start then result.append(cleanText.slice(resultEnd, info.start)) val block = info.block.replace(info.tree.formatted.toString.linesIterator.toIndexedSeq) // Clunky :D - result.append(block.toLines.mkString("\n")) + result.append(block.toLines(indent).mkString("\n")) if info.endsWithNewline then result.append("\n") resultEnd = info.end