Skip to content

Comments

refactor: replacing custom formatter with styleText utility#58

Merged
chenjiahan merged 1 commit intomainfrom
color_style_text_0203
Feb 3, 2026
Merged

refactor: replacing custom formatter with styleText utility#58
chenjiahan merged 1 commit intomainfrom
color_style_text_0203

Conversation

@chenjiahan
Copy link
Member

Refactors the color formatting logic to use Node.js's built-in styleText utility instead of manual ANSI escape sequences.

Copilot AI review requested due to automatic review settings February 3, 2026 06:14
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 styleText function. This change significantly reduces the complexity of the src/color.ts module, making the code more robust, easier to read, and less prone to errors related to string manipulation for color application.

Highlights

  • Refactoring Color Formatting: Replaced the custom implementation for applying ANSI escape sequences for text coloring with Node.js's built-in node:util.styleText utility, simplifying the src/color.ts module.

🧠 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
  • src/color.ts
    • Removed the custom formatter and replaceClose functions, which previously handled ANSI escape sequence generation.
    • Updated the ColorFn type definition to reflect the simpler input requirements of the new styling approach.
    • Introduced a createStyler helper function to wrap node:util.styleText, which is then used to define all color functions (e.g., bold, red, green).
    • Eliminated the dependency on colorLevel from ./utils.js as styleText handles color level detection internally.
Activity
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@chenjiahan chenjiahan merged commit 119dfed into main Feb 3, 2026
8 checks passed
@chenjiahan chenjiahan deleted the color_style_text_0203 branch February 3, 2026 06:15
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 formatter function and replaceClose helper with Node.js's styleText utility
  • Updated ColorFn type signature to remove null | undefined support
  • Removed explicit colorLevel checking in favor of Node.js's internal color detection

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +5 to +8
const createStyler =
(style: Parameters<typeof styleText>[0]): ColorFn =>
text =>
styleText(style, String(text));
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
import { styleText } from 'node:util';

export type ColorFn = (input: string | number | null | undefined) => string;
export type ColorFn = (text: string | number) => string;
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
export type ColorFn = (text: string | number) => string;
export type ColorFn = (text: string | number | null | undefined) => string;

Copilot uses AI. Check for mistakes.
@@ -1,36 +1,16 @@
import { colorLevel } from './utils.js';
import { styleText } from 'node:util';
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant