Skip to content

Please provide a way to identify a specific device that should receive color #14

Open
@joshtriplett

Description

@joshtriplett

As has arisen in discussions of CLICOLOR_FORCE in some projects, setting CLICOLOR_FORCE can break builds where any command that acts on CLICOLOR_FORCE is run with redirected output (pipe, file, etc).

However, CLICOLOR_FORCE still provides a great deal of value for users of IDEs, CI systems, and similar.

Please consider adding a version of this that flags a specific output as being able to receive color, in such a way that subcommands with redirected output cannot accidentally interpret it as applying to them.

One possible way to do this: set CLICOLOR_FORCE_STAT='123 456', where 123 and 456 are the st_dev and st_ino fields returned by fstat. Then, when checking whether to output color, before trying isatty, check if CLICOLOR_FORCE_STAT is set and if fstat on the fd you want to check returns the corresponding st_dev and st_ino. If so, emit color, otherwise don't.

Pipes, sockets, logfiles, and any other kind of file descriptor have a unique pair of st_dev and st_ino. This will automatically work for anything that inherits the file descriptor. But the moment any process gets run with its output redirected, the stat check won't pass, so it won't force color. Conversely, any program running a subprocess can trivially implement this by calling fstat on the file descriptor it's about to give to a subprocess and setting the environment variable accordingly.

(The use of fstat applies to UNIX platforms. A similar mechanism on Windows is GetFileInformationByHandleEx retrieving FILE_ID_INFO, which has a similar pair of unique numbers. On Windows, those numbers are only guaranteed unique while the file is open, but that should likely be true over the course of a build.)

Would you be open to the idea of incorporating this into the standard? I'd be happy to write a pull request documenting it in detail, if so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions