Skip to content

Commit

Permalink
Add support for not outputting color to terminal (#44)
Browse files Browse the repository at this point in the history
This is required when the output is piped into a file or a program.
  • Loading branch information
greenfork authored Jan 9, 2024
1 parent 59f341e commit 22cf5b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
14 changes: 14 additions & 0 deletions .release-notes/44.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Add support for not colorizing the terminal output

Prior to this change any error message called via `PegFormatError.console`
would return an error message with ANSI escape sequence code for coloring. This
created a problem when piping the output to something other than a terminal.

This change adds the ability to remove ANSI escape sequence codes from the
output. The change is non-breaking as the default behavior is to colorize
the output. The colorization can be turned off by supplying `false` to
the `colorize` parameter:

```pony
PegFormatError.console(error, false)
```
14 changes: 7 additions & 7 deletions peg/pegerror.pony
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ primitive MalformedAST is PegError
"""

primitive PegFormatError
fun console(e: PegError val): ByteSeqIter =>
fun console(e: PegError val, colorize: Bool = true): ByteSeqIter =>
let text =
recover
[ ANSI.cyan()
[ if colorize then ANSI.cyan() else "" end
"-- "; e.category(); " --\n\n"
ANSI.reset()
if colorize then ANSI.reset() else "" end
]
end

Expand All @@ -121,16 +121,16 @@ primitive PegFormatError

text.append(
recover
[ ANSI.grey()
[ if colorize then ANSI.grey() else "" end
m._1.path; ":"; line_text; ":"; col.string(); ":"; m._3.string()
"\n\n"
line_text; ": "
ANSI.reset()
if colorize then ANSI.reset() else "" end
source; "\n"
ANSI.red()
if colorize then ANSI.red() else "" end
line_indent; " "; indent; consume mark; "\n"
line_indent; " "; indent; m._4
ANSI.reset()
if colorize then ANSI.reset() else "" end
"\n\n"
]
end
Expand Down

0 comments on commit 22cf5b0

Please sign in to comment.