refactor: replacing custom formatter with styleText utility#58
refactor: replacing custom formatter with styleText utility#58chenjiahan merged 1 commit intomainfrom
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Summary of ChangesHello @chenjiahan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the color formatting utility by migrating from a custom, manual implementation of ANSI escape sequences to using Node.js's native Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the color formatting logic to use Node.js's built-in styleText utility, which is a great improvement that simplifies the code and removes a custom implementation. My review identifies one potential breaking change in the ColorFn type signature, which now disallows null and undefined values that were previously handled. I've suggested a fix to maintain backward compatibility.
There was a problem hiding this comment.
Pull request overview
This PR refactors the color formatting implementation by replacing custom ANSI escape sequence logic with Node.js's built-in styleText utility from the node:util module. The refactor simplifies the codebase by removing the manual formatter and nested color handling logic, reducing the implementation from ~36 lines to ~16 lines.
Changes:
- Replaced custom
formatterfunction andreplaceClosehelper with Node.js'sstyleTextutility - Updated ColorFn type signature to remove
null | undefinedsupport - Removed explicit
colorLevelchecking in favor of Node.js's internal color detection
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const createStyler = | ||
| (style: Parameters<typeof styleText>[0]): ColorFn => | ||
| text => | ||
| styleText(style, String(text)); |
There was a problem hiding this comment.
The previous implementation used colorLevel to conditionally apply colors, returning a plain String function when colorLevel < 2 (no color support). The new implementation using styleText always applies ANSI codes and relies on Node.js's internal color detection. Additionally, the old implementation had special replaceClose logic to handle nested/pre-existing ANSI codes (e.g., when calling bold(output) in gradient.ts where output already contains ANSI escape sequences). Verify that: 1) the color detection behavior remains consistent with the supports-color based approach used elsewhere in the codebase (src/gradient.ts still uses colorLevel), and 2) nested ANSI codes are handled correctly, particularly in src/gradient.ts:39 and src/createLogger.ts:39 where color functions wrap already-styled text.
| import { styleText } from 'node:util'; | ||
|
|
||
| export type ColorFn = (input: string | number | null | undefined) => string; | ||
| export type ColorFn = (text: string | number) => string; |
There was a problem hiding this comment.
The ColorFn type signature has been changed to remove support for null and undefined parameters. While the current codebase doesn't pass null or undefined to these functions, this is a breaking change for the public API. Any external consumers of this library who may be passing null or undefined values will experience TypeScript compilation errors or runtime issues. Consider documenting this as a breaking change in the release notes.
| export type ColorFn = (text: string | number) => string; | |
| export type ColorFn = (text: string | number | null | undefined) => string; |
| @@ -1,36 +1,16 @@ | |||
| import { colorLevel } from './utils.js'; | |||
| import { styleText } from 'node:util'; | |||
There was a problem hiding this comment.
The styleText utility from node:util was introduced in Node.js v20.12.0 and v21.7.0. However, according to the README, this package supports Node.js >= 14. Using styleText will break compatibility with Node.js versions 14-19, which contradicts the documented environment requirements. Consider either updating the minimum Node.js version requirement in the README and package.json, or keeping the custom formatter implementation for backward compatibility.
Refactors the color formatting logic to use Node.js's built-in
styleTextutility instead of manual ANSI escape sequences.