Fix ANSI log styling - #10028
Fix ANSI log styling#10028khanak0509 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for stripping ANSI escape codes from pretty-printed log details and refactors textSpansFromAnsi to preserve default text styles. It also includes comprehensive unit tests for these new behaviors. The review feedback recommends caching the computed result of prettyPrinted() to prevent redundant, expensive operations on the UI thread, ensuring better performance and consistency.
| String? prettyPrinted() { | ||
| final text = _prettyPrintedWithAnsi(); | ||
| if (text == null) return null; | ||
| // JSON encoding escapes the ANSI escape character as `\u001b`. | ||
| return stripAnsi(text.replaceAll(_jsonEscapedAnsiEscape, '\x1B')); | ||
| } | ||
|
|
||
| static final _jsonEscapedAnsiEscape = RegExp(r'\\u001[bB]'); |
There was a problem hiding this comment.
[CONCERN] To avoid performance issues (such as UI jank or lag) when rendering large log details or when the user repeatedly triggers redraws, search, or copy actions, we should cache the result of prettyPrinted() once the details have been fully computed. This is consistent with how _encodedDetails is cached in the same class.
String? prettyPrinted() {
if (_prettyPrinted != null) return _prettyPrinted;
final text = _prettyPrintedWithAnsi();
if (text == null) return null;
// JSON encoding escapes the ANSI escape character as \u001b.
final result = stripAnsi(text.replaceAll(_jsonEscapedAnsiEscape, '\x1B'));
if (detailsComputed.isCompleted) {
_prettyPrinted = result;
}
return result;
}
String? _prettyPrinted;
static final _jsonEscapedAnsiEscape = RegExp(r'\\u001[bB]');References
- Prioritize logic, performance on the UI thread, and architectural consistency. Caching computed results avoids redundant heavy operations (like JSON decoding, regex replacement, and ANSI parsing) on the UI thread. (link)
Fixes #9521
this PR fixes issue #9521 where logs with ANSI colors were not shown properly in the Logging screen. If a log had only a background color, the text became black, so with a black background it was fully invisible in dark theme. Colored text was also bigger than the normal text, and the Details panel showed the raw escape codes like
[45mand[0m.How I fixed it:
textSpansFromAnsiwas creating a newTextStylefor colored spans and dropping the default style, so the color became null (black) and the font size fell back to default. Now it usesdefaultStyle.copyWith(...), so colored spans keep the theme color and font size and only change what the ANSI code sets.For the Details panel, I added a
stripAnsi()helper and used it inLogData.prettyPrinted(), so the details text, search and copy all get clean text. Fordart:developerlogs the details are JSON, and JSON encodes the escape char as\u001b, so I handle that too.before --

after --


Pre-launch Checklist
General checklist
///).Issues checklist
contributions-welcomeorgood-first-issuelabel.contributions-welcomeorgood-first-issuelabel. I understand this means my PR might take longer to be reviewed.Tests checklist
AI-tooling checklist
Feature-change checklist
release-notes-not-requiredlabel or left a comment requesting the label be added.packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md.If you need help, consider asking for help on Discord.