-
-
Notifications
You must be signed in to change notification settings - Fork 94
fix: Disable colors if the standard output is not a TTY #341
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
Conversation
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.
Summary of Changes
Hello @crohasang, 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 refines the command-line interface's color output by implementing a more robust and standard-compliant mechanism. It ensures that colors are automatically disabled when the output is not a terminal or when the NO_COLOR environment variable is present, achieved through a migration to Deno's standard library for color utilities and corresponding syntax updates across the CLI.
Highlights
- Improved CLI Color Control: The CLI now intelligently disables color output when the standard output is not a TTY (e.g., when redirecting to a file) or when the
NO_COLORenvironment variable is set. This enhances usability for scripting and environments where ANSI escape codes are undesirable. - Color Library Migration: The project's color handling has been standardized by migrating from
@cliffy/ansi/colorsto@std/fmt/colorsfrom the Deno standard library. This change involved updating all instances of color application syntax across the CLI codebase. - Deno's
console.log()Behavior: It's important to note that the automatic color formatting applied by Deno'sconsole.log()when printing JavaScript objects (e.g., infedify lookupcommand output) is a built-in runtime feature and remains unaffected by these changes, as it's not controlled by thesetColorEnabled()function.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| 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 issue 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 or fill out our survey to provide feedback.
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
-
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. ↩
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.
Code Review
This pull request does a great job of standardizing color handling in the CLI by migrating from @cliffy/ansi/colors to @std/fmt/colors. The implementation correctly disables colors when the output is not a TTY or when the NO_COLOR environment variable is set. The changes across the various command files are consistent with the new library's API.
I have one suggestion in cli/mod.ts to make the NO_COLOR check more robust by using Deno.env.has() instead of checking for undefined. This is a minor improvement for maintainability.
It's good that you've noted the limitation regarding Deno.inspect's color handling in the PR description. Overall, this is a solid improvement to the CLI's user experience.
cli/mod.ts
Outdated
| const colorEnabled = Deno.stdout.isTerminal() && | ||
| Deno.env.get("NO_COLOR") === undefined; |
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.
The current check for the NO_COLOR environment variable, Deno.env.get("NO_COLOR") === undefined, is functionally correct for now but could be more robust.
A more explicit way to check for the presence of an environment variable is by using Deno.env.has(). This makes the intent clearer and is less susceptible to potential changes in how Deno.env.get() handles edge cases (like empty strings) in future Deno versions.
Using !Deno.env.has("NO_COLOR") would improve readability and robustness.
const colorEnabled = Deno.stdout.isTerminal() && !Deno.env.has("NO_COLOR");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.
Thanks for advice. I applied your feedback!
|
The docs for this pull request have been published: |
CHANGES.md
Outdated
|
|
||
| - Implemented NO_COLOR environment variable detection for global color control using @std/fmt/colors.setColorEnabled(). | ||
| - Standardized color application by switching from @cliffy/ansi/colors to @std/fmt/colors and adapting the syntax (for example, colors.bold(colors.green(...))). | ||
| - The automatic color formatting applied by Deno's console.log() when printing JavaScript objects (for example, in fedify lookup command output) is a built-in runtime feature and remains unaffected by this change. |
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.
You could utilize the Deno.inspect() API which provides option colors: boolean. Under the hood, console.log() utilizes it.
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.
Thanks for advice! I made helper function using Deno.inspect and tried to control colors and it worked!
|
I read // line 109
+ console.log(
+ {
+ where: "before inspect",
+ content,
+ },
+ );
content = Deno.inspect(content, {
colors: !(options.output),
});
const encoder = new TextEncoder();
const bytes = encoder.encode(content + "\n");
+ console.log(
+ {
+ where: "after inspect",
+ content,
+ },
+ );And I run { isTerminal: false, colorEnabled: false }
{
where: "before inspect",
options: {
cacheDir: "/root/.cache/fedify",
firstKnock: "rfc9421",
separator: "----"
},
colorEnable: false,
content: Person {
id: URL "https://hackers.pub/ap/actors/0197d646-c123-7ad4-a2f1-c6ff8a98fd2f",
// ...
}
}
{
where: "after inspect",
content: "Person {\n" +
' id: URL \x1b[32m"https://hackers.pub/ap/actors/0197d646-c123-7ad4-a2f1-c6ff8a98fd2f"\x1b[39m,\n' +
' name: \x1b[32m"
// ...
}So that means, somthing is wrong in {
where: "before inspect",
options: {
cacheDir: "/root/.cache/fedify",
firstKnock: "rfc9421",
separator: "----"
},
colorEnable: false,
content: Person {
id: URL "https://hackers.pub/ap/actors/0197d646-c123-7ad4-a2f1-c6ff8a98fd2f",
// ...
}
{
where: "after inspect",
content: "Person {\n" +
' id: URL "https://hackers.pub/ap/actors/0197d646-c123-7ad4-a2f1-c6ff8a98fd2f",\n' +
// ...
}So the problem is content = Deno.inspect(content, {
colors: !(options.output) && colors.getColorEnabled(),
});And the log is perpect! |
…lor output if not - add @std/fmt/colors in deno.json
- Replaced @cliffy/ansi/colors with @std/fmt/colors for consistent color management - Updated chained color calls to nested calls
…o change color settings
- Renamed the function from formatObjectForOutput to formatCliObjectOutputWithColor - Moved the formatObjectForOutput function and colorEnabled constant to a utils file
|
The latest push to this pull request has been published to JSR and npm as a pre-release:
|
Summary
Improved CLI color control to respect TTY status and NO_COLOR,
though console.log's automatic object coloring remains unaffected.Related Issue
Changes
The automatic color formatting applied by Deno's console.log() when printing JavaScript objects (for example, in fedify lookup command output) is a built-in runtime feature and remains unaffected by this change.Additional Notes
2. Result image of 'deno task cli inbox > inbox.txt' command.
3. Result image of 'deno task cli lookup (handle) > lookup.txt'
The colors aren't disappearing because, I guess, Deno's console.log() automatically adds color when formatting JavaScript objects, a behavior not controlled by setColorEnabled().update (KST 20250802 1634)
I created helper function using Deno.inspect in utils.js
and applied it in several files.
I added 'colors' optional property because this function have to think about '-o' command in lookup. (Thanks to @2chanhaeng)
Test Update