To make error messages from Pkl eval easier to read, this change uses the Jansi library to colour the output, making it quicker and easier to scan error messages and understand what's happened.
The Jansi library also detects if the CLI output is a terminal capable of handling colours, and will automatically strip out escape codes if the output won't support them (e.g. piping the output somewhere else).
The implementation is a little crude, but think acceptable given a better approach would probably require rewriting how errors are handled completely, so that the output composition happens near the frontend of the CLI, rather than down in the bowls of Pkl. The CLI frontend is in a much better position to determine terminal capabilities, and adjust outputs as needed, and would allow us to use a much more expressive library like Mordant to really supercharge the output formatting.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.
## 📋 Pull Request Information
**Original PR:** https://github.com/apple/pkl/pull/552
**Author:** [@thomaspurchas](https://github.com/thomaspurchas)
**Created:** 6/27/2024
**Status:** ✅ Merged
**Merged:** 7/1/2024
**Merged by:** [@holzensp](https://github.com/holzensp)
**Base:** `main` ← **Head:** `add-color`
---
### 📝 Commits (2)
- [`65c9ce3`](https://github.com/apple/pkl/commit/65c9ce30840479368cd05f3ac9418eccc60568ba) Add colours to Pkl errors in Cli output
- [`6ffc739`](https://github.com/apple/pkl/commit/6ffc739731e3a6e31b4abb013e72bd9d21aa101e) Add test for colour outputs
### 📊 Changes
**28 files changed** (+147 additions, -28 deletions)
<details>
<summary>View changed files</summary>
📝 `bench/gradle.lockfile` (+1 -0)
📝 `buildSrc/src/main/kotlin/pklKotlinTest.gradle.kts` (+3 -0)
📝 `docs/gradle.lockfile` (+1 -0)
📝 `pkl-cli/gradle.lockfile` (+1 -1)
📝 `pkl-cli/pkl-cli.gradle.kts` (+3 -2)
📝 `pkl-cli/src/main/kotlin/org/pkl/cli/CliTestRunner.kt` (+1 -0)
📝 `pkl-codegen-java/gradle.lockfile` (+1 -0)
📝 `pkl-codegen-kotlin/gradle.lockfile` (+1 -0)
📝 `pkl-commons-cli/gradle.lockfile` (+1 -0)
📝 `pkl-commons-cli/pkl-commons-cli.gradle.kts` (+1 -1)
📝 `pkl-commons-cli/src/main/kotlin/org/pkl/commons/cli/CliMain.kt` (+4 -0)
📝 `pkl-commons-test/gradle.lockfile` (+1 -0)
📝 `pkl-commons-test/pkl-commons-test.gradle.kts` (+1 -0)
📝 `pkl-config-java/gradle.lockfile` (+1 -0)
📝 `pkl-config-kotlin/gradle.lockfile` (+1 -0)
📝 `pkl-core/gradle.lockfile` (+1 -0)
📝 `pkl-core/pkl-core.gradle.kts` (+2 -0)
📝 `pkl-core/src/main/java/org/pkl/core/runtime/StackTraceRenderer.java` (+45 -17)
📝 `pkl-core/src/main/java/org/pkl/core/runtime/VmExceptionRenderer.java` (+9 -2)
➕ `pkl-core/src/test/kotlin/org/pkl/core/ErrorColoringTest.kt` (+54 -0)
_...and 8 more files_
</details>
### 📄 Description
To make error messages from Pkl eval easier to read, this change uses the Jansi library to colour the output, making it quicker and easier to scan error messages and understand what's happened.
The Jansi library also detects if the CLI output is a terminal capable of handling colours, and will automatically strip out escape codes if the output won't support them (e.g. piping the output somewhere else).
The implementation is a little crude, but think acceptable given a better approach would probably require rewriting how errors are handled completely, so that the output composition happens near the frontend of the CLI, rather than down in the bowls of Pkl. The CLI frontend is in a much better position to determine terminal capabilities, and adjust outputs as needed, and would allow us to use a much more expressive library like [Mordant](https://ajalt.github.io/mordant/) to really supercharge the output formatting.

<img width="1050" alt="image" src="https://github.com/apple/pkl/assets/782311/6711e87a-9e1d-47b7-b60e-a949bda98990">
<img width="1282" alt="image" src="https://github.com/apple/pkl/assets/782311/c9198942-4a12-421b-9042-adf7afcaccf5">
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/apple/pkl/pull/552
Author: @thomaspurchas
Created: 6/27/2024
Status: ✅ Merged
Merged: 7/1/2024
Merged by: @holzensp
Base:
main← Head:add-color📝 Commits (2)
65c9ce3Add colours to Pkl errors in Cli output6ffc739Add test for colour outputs📊 Changes
28 files changed (+147 additions, -28 deletions)
View changed files
📝
bench/gradle.lockfile(+1 -0)📝
buildSrc/src/main/kotlin/pklKotlinTest.gradle.kts(+3 -0)📝
docs/gradle.lockfile(+1 -0)📝
pkl-cli/gradle.lockfile(+1 -1)📝
pkl-cli/pkl-cli.gradle.kts(+3 -2)📝
pkl-cli/src/main/kotlin/org/pkl/cli/CliTestRunner.kt(+1 -0)📝
pkl-codegen-java/gradle.lockfile(+1 -0)📝
pkl-codegen-kotlin/gradle.lockfile(+1 -0)📝
pkl-commons-cli/gradle.lockfile(+1 -0)📝
pkl-commons-cli/pkl-commons-cli.gradle.kts(+1 -1)📝
pkl-commons-cli/src/main/kotlin/org/pkl/commons/cli/CliMain.kt(+4 -0)📝
pkl-commons-test/gradle.lockfile(+1 -0)📝
pkl-commons-test/pkl-commons-test.gradle.kts(+1 -0)📝
pkl-config-java/gradle.lockfile(+1 -0)📝
pkl-config-kotlin/gradle.lockfile(+1 -0)📝
pkl-core/gradle.lockfile(+1 -0)📝
pkl-core/pkl-core.gradle.kts(+2 -0)📝
pkl-core/src/main/java/org/pkl/core/runtime/StackTraceRenderer.java(+45 -17)📝
pkl-core/src/main/java/org/pkl/core/runtime/VmExceptionRenderer.java(+9 -2)➕
pkl-core/src/test/kotlin/org/pkl/core/ErrorColoringTest.kt(+54 -0)...and 8 more files
📄 Description
To make error messages from Pkl eval easier to read, this change uses the Jansi library to colour the output, making it quicker and easier to scan error messages and understand what's happened.
The Jansi library also detects if the CLI output is a terminal capable of handling colours, and will automatically strip out escape codes if the output won't support them (e.g. piping the output somewhere else).
The implementation is a little crude, but think acceptable given a better approach would probably require rewriting how errors are handled completely, so that the output composition happens near the frontend of the CLI, rather than down in the bowls of Pkl. The CLI frontend is in a much better position to determine terminal capabilities, and adjust outputs as needed, and would allow us to use a much more expressive library like Mordant to really supercharge the output formatting.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.