rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.34k stars 2.34k forks source link

Cargo display color codes on terminals (`TERM`) without color support #11211

Open amarao opened 1 year ago

amarao commented 1 year ago

Problem

Some terminals lack ability to display colors. If TERM is set to the terminal type without color support, cargo should not put random (other terminal codes) on the screen.

Steps

  1. TERM=t10 cargo build

Expected result: no color codes. Actual result: color console codes in output.

Possible Solution(s)

Unknown terminal types should switch cargo into 'no colors' mode.

Notes

infocmp t10
#   Reconstructed via infocmp from file: /usr/share/terminfo/t/t10
t10|Teleray 10 special,
    km, xhp, xt,
    cols#80, it#8, lines#24, xmc#2,
    clear=\Ej$<30/>, cub1=^H, cud1=\EB, cuf1=\EC,
    cup=\EY%p1%' '%+%c%p2%' '%+%c, cuu1=\EA, dch1=\EQ,
    dl1=\EM, ed=\EJ, el=\EK, home=\EH, ht=^I, ich1=\EP, il1=\EL,
    ind=\Eq, pad=\0, ri=\Ep, rmso=\ER@, rmul=\ER@, smso=\ERD,
    smul=\ERH,

Version

cargo 1.63.0 (fd9c4297c 2022-07-01)
release: 1.63.0
commit-hash: fd9c4297ccbee36d39e9a79067edab0b614edb5a
commit-date: 2022-07-01
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1n)
os: Ubuntu 22.10 (kinetic) [64-bit]
weihanglo commented 1 year ago

Cargo at this moment depends on termcolor and atty to detect color support. So far as I understand it, rules are:

  1. If atty detects the stream source is a tty, hand over to termcolor to determine.^1
    1. if TERM=dumb or NO_COLOR is set, disable the color support. ^2
    2. Otherwise enable it.
  2. Otherwise disable the color support.

I am not an expert on tty specifications, but this seems to be more an enhancement of upstreams.

As of now, environment variable CARGO_TERM_COLOR=never is at your service, as well as the configuration counterpart term.color.

epage commented 1 year ago

I'm curious, what is your reason for TERM=t10?

Unknown terminal types should switch cargo into 'no colors' mode.

For some reason most new CLIs seem to be eschewing termcap and running heuristics on TERM. Most I see just check TERM=dumb though supports-color does do a few more checks.

At least when discussing it within the CLI-WG, someone made this statement

Remember that terminfo and termcap came from a time when there were a lot of very weird non-VT-series terminals around, and if you wanted your app to work at all, then you had to use the peculiar sequences required by those terminals. Nowadays it's reasonable to assume basic ANSI support. So, for colour if you just use plain 8-colour ANSI sequences and maybe bold, it shouldn't need any feature testing. So either aim for the lowest common denominator (easy) or try to get fancy and detect extra features (harder). Also bear in mind that the user might be running with either a white or black default background (colour 49). So blue on default-background might be readable on white, but probably won't be on black. This is all UNIX, though -- I don't know the Windows situation.