Add color to error formatting (#746)

* Add color to error formatting

* Apply suggestions from code review

Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>

* Address reviewer comments

* Apply suggestions from code review

Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>

* Define style choices as operations on formatter (abandon semantic API)

* Adjust margin styling

* Review feedback

* Documentation nits

---------

Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>
This commit is contained in:
Philip K.F. Hölzenspies
2024-11-01 10:02:19 +00:00
committed by GitHub
parent e217cfcd6f
commit 03462fefae
38 changed files with 445 additions and 114 deletions

View File

@@ -20,6 +20,7 @@ import java.nio.file.Files
import java.nio.file.Path
import java.time.Duration
import java.util.regex.Pattern
import org.pkl.core.evaluatorSettings.Color
import org.pkl.core.evaluatorSettings.PklEvaluatorSettings.ExternalReader
import org.pkl.core.module.ProjectDependenciesManager
import org.pkl.core.util.IoUtils
@@ -101,6 +102,9 @@ data class CliBaseOptions(
/** The cache directory for storing packages. */
private val moduleCacheDir: Path? = null,
/** Whether to render errors in ANSI color. */
val color: Color? = null,
/** Whether to disable the module cache. */
val noCache: Boolean = false,

View File

@@ -284,6 +284,7 @@ abstract class CliCommand(protected val cliOptions: CliBaseOptions) {
.setEnvironmentVariables(environmentVariables)
.addModuleKeyFactories(moduleKeyFactories(modulePathResolver))
.addResourceReaders(resourceReaders(modulePathResolver))
.setColor(cliOptions.color?.hasColor() ?: false)
.setLogger(Loggers.stdErr())
.setTimeout(cliOptions.timeout)
.setModuleCacheDir(moduleCacheDir)

View File

@@ -17,6 +17,7 @@ package org.pkl.commons.cli.commands
import com.github.ajalt.clikt.parameters.groups.OptionGroup
import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parameters.types.long
import com.github.ajalt.clikt.parameters.types.path
@@ -29,6 +30,7 @@ import java.util.regex.Pattern
import org.pkl.commons.cli.CliBaseOptions
import org.pkl.commons.cli.CliException
import org.pkl.commons.shlex
import org.pkl.core.evaluatorSettings.Color
import org.pkl.core.evaluatorSettings.PklEvaluatorSettings.ExternalReader
import org.pkl.core.runtime.VmUtils
import org.pkl.core.util.IoUtils
@@ -141,6 +143,17 @@ class BaseOptions : OptionGroup() {
)
.associateProps()
val color: Color by
option(
names = arrayOf("--color"),
metavar = "<when>",
help =
"Whether to format messages in ANSI color. Possible values of <when> are 'never', 'auto', and 'always'."
)
.enum<Color> { it.name.lowercase() }
.single()
.default(Color.AUTO)
val noCache: Boolean by
option(names = arrayOf("--no-cache"), help = "Disable caching of packages")
.single()
@@ -265,6 +278,7 @@ class BaseOptions : OptionGroup() {
projectDir = projectOptions?.projectDir,
timeout = timeout,
moduleCacheDir = cacheDir ?: defaults.normalizedModuleCacheDir,
color = color,
noCache = noCache,
testMode = testMode,
testPort = testPort,