-
Notifications
You must be signed in to change notification settings - Fork 7
fix(logger): Preserve box alignment for ANSI-styled warnings/errors #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
db2ab95
56df24b
54dc621
a479bc3
99ab33e
94e69c0
120a5ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,13 +94,7 @@ class StdOutLogger extends Logger { | |
| final LogType type = TextLogType.normal, | ||
| }) { | ||
| if (ansiSupported) { | ||
| final ansiMessage = switch (level) { | ||
| LogLevel.debug => AnsiStyle.darkGray.wrap(message), | ||
| LogLevel.info => message, | ||
| LogLevel.warning => AnsiStyle.yellow.wrap(message), | ||
| LogLevel.error => AnsiStyle.red.wrap(message), | ||
| LogLevel.nothing => message, | ||
| }; | ||
| final ansiMessage = _styleByLevel(message, level); | ||
|
|
||
| _log(ansiMessage, level, newParagraph, type); | ||
| } else { | ||
|
|
@@ -159,9 +153,12 @@ class StdOutLogger extends Logger { | |
| if (type is BoxLogType) { | ||
| message = _formatAsBox( | ||
| wrapColumn: wrapTextColumn ?? _defaultColumnWrap, | ||
| message: message, | ||
| title: type.title, | ||
| message: _stripAnsiCodes(message), | ||
| title: type.title != null ? _stripAnsiCodes(type.title!) : null, | ||
| ); | ||
| if (ansiSupported) { | ||
| message = _styleByLevel(message, logLevel); | ||
|
Comment on lines
154
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't drop caller-supplied ANSI styling from boxed logs. Lines 156-160 build the box from stripped |
||
| } | ||
| } else if (type is TextLogType) { | ||
| switch (type.style) { | ||
| case TextLogStyle.command: | ||
|
|
@@ -237,6 +234,27 @@ class StdOutLogger extends Logger { | |
| } | ||
| } | ||
|
|
||
| String _styleByLevel(final String message, final LogLevel level) { | ||
| return switch (level) { | ||
| LogLevel.debug => AnsiStyle.darkGray.wrap(message), | ||
| LogLevel.info => message, | ||
| LogLevel.warning => AnsiStyle.yellow.wrap(message), | ||
| LogLevel.error => AnsiStyle.red.wrap(message), | ||
| LogLevel.nothing => message, | ||
| }; | ||
| } | ||
|
|
||
| String _stripAnsiCodes(final String input) { | ||
| // Matches ANSI/VT escape sequences including: | ||
| // - C1 control chars introduced by ESC | ||
| // - CSI sequences (ESC [ ... final-byte) | ||
| // - OSC sequences (ESC ] ... BEL or ESC \\) | ||
| final ansiRegex = RegExp( | ||
| r'\x1B(?:\][^\x07\x1B]*(?:\x07|\x1B\\)|\[[0-?]*[ -/]*[@-~]|[@-Z\\-_])', | ||
| ); | ||
| return input.replaceAll(ansiRegex, ''); | ||
| } | ||
|
|
||
| /// wrap text based on column width | ||
| String _wrapText(final String text, final int columnWidth) { | ||
| final textLines = text.split('\n'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest moving the ansi code wrapping to the _log() method, and avoid wrapping, unwrapping, and wrapping again.