Skip to content

Conversation

@crohasang
Copy link
Contributor

@crohasang crohasang commented Jul 31, 2025

Summary

Improved CLI color control to respect TTY status and NO_COLOR,
though console.log's automatic object coloring remains unaffected.

Related Issue

Changes

  • 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.

Additional Notes

  1. Result image of 'deno task cli inbox' command in terminal.
image




2. Result image of 'deno task cli inbox > inbox.txt' command.
image




3. Result image of 'deno task cli lookup (handle) > lookup.txt'
image

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

export const colorEnabled: boolean = Deno.stdout.isTerminal() &&
  !Deno.env.has("NO_COLOR");

export function formatCliObjectOutputWithColor(
  obj: unknown,
  colors?: boolean,
): string {
  const enableColors = colors ?? colorEnabled;
  return Deno.inspect(obj, { colors: enableColors });
}

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)

    const enableColors = colorEnabled && options.output === undefined;
    content = formatCliObjectOutputWithColor(content, enableColors);

Test Update

  1. Result image of 'deno task cli lookup (handle) -o lookup.txt
image
  1. Result image of 'deno task cli lookup (handle) > lookup.txt
image
  1. Result image of 'deno task cli inbox > inbox.txt
image

Copy link
Contributor

@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.

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_COLOR environment 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/colors to @std/fmt/colors from 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's console.log() when printing JavaScript objects (e.g., in fedify lookup command output) is a built-in runtime feature and remains unaffected by these changes, as it's not controlled by the setColorEnabled() 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

  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.

@github-actions github-actions bot added component/cli CLI tools related good first issue Good for newcomers priority/low Low priority issues type/enhancement Improvements to existing features labels Jul 31, 2025
Copy link
Contributor

@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 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
Comment on lines 16 to 17
const colorEnabled = Deno.stdout.isTerminal() &&
Deno.env.get("NO_COLOR") === undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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");

Copy link
Contributor Author

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!

@github-actions
Copy link
Contributor

github-actions bot commented Jul 31, 2025

The docs for this pull request have been published:

https://7ed287ec.fedify.pages.dev

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.
Copy link
Member

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.

Copy link
Contributor Author

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!

@2chanhaeng
Copy link
Contributor

I read cli/lookup.ts and add log the file like this:

      // 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 deno task cli lookup @[email protected] > lookup.txt, then log like this.

{ 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 content = Deno.inspect(content, { colors: !(options.output) });
I replace !(options.output) with !!(options.output), and the log after runned command like this:

{
  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 colors option of Deno.inspect.
And I read other changes, then I found setColorEnabled code, but getColorEnabled is not using.
So I changed the code like this:

    content = Deno.inspect(content, {
      colors: !(options.output) && colors.getColorEnabled(),
    });

And the log is perpect!
Therefore I suggest this change:

    content = Deno.inspect(content, {
      colors: !(options.output) && colors.getColorEnabled(),
    });

@crohasang crohasang changed the title fix: Disable colors if the standard output is not a TTY (but it's not that perfect) fix: Disable colors if the standard output is not a TTY Aug 2, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Aug 2, 2025

The latest push to this pull request has been published to JSR and npm as a pre-release:

Package Version JSR npm
@fedify/fedify 1.8.1-pr.341.1249+54907966 JSR npm
@fedify/cli 1.8.1-pr.341.1249+54907966 JSR
@fedify/amqp 1.8.1-pr.341.1249+54907966 JSR npm
@fedify/express 1.8.1-pr.341.1249+54907966 JSR npm
@fedify/h3 1.8.1-pr.341.1249+54907966 JSR npm
@fedify/nestjs 1.8.1-pr.341.1249+54907966 npm
@fedify/postgres 1.8.1-pr.341.1249+54907966 JSR npm
@fedify/redis 1.8.1-pr.341.1249+54907966 JSR npm
@fedify/testing 1.8.1-pr.341.1249+54907966 JSR npm

@dahlia dahlia merged commit 5cddd68 into fedify-dev:main Aug 2, 2025
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/cli CLI tools related good first issue Good for newcomers priority/low Low priority issues type/enhancement Improvements to existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fedify CLI: Disable colors if the standard output is not a TTY

3 participants