readthedocs.org icon indicating copy to clipboard operation
readthedocs.org copied to clipboard

Enable ansi color escape sequences

Open agjohnson opened this issue 3 years ago • 3 comments
trafficstars

Related to https://github.com/readthedocs/readthedocs.org/issues/6883

I'm working to re-enable color code output for commands and having trouble finding a balance between nice color output and various behavior from the commands we execute when color output is available (that is in most cases, the terminal is a ptty).

So far, I've tried various environment variables (TERM=xterm-color, etc) on exec_create and removing the NO_COLOR env var that was previously added, however I've found nothing really works. The underlying issue is that commands executed in the build container are not executed with exec_create(..., tty=True). This is easy enough to add, but then there are various issues with the output from other commands:

  • Git fetch displays a progress bar
  • Pip install steps display a progress bar, unless --progress-bar no flag is passed in
  • Apt probably has a progress bar as well

The progress bar shows up as you would if you piped the command to a file:

image

However, the colorized output is quite nice, and really nice for stack traces, exceptions/warnings.

So, perhaps either:

  • Someone has the knowledge about terminals to enforce color output without designating the terminal as a ptty. As far as I know there is no option like this.
  • We selectively execute commands as a ptty, maybe skipping pip install, apt install, etc
  • We tune the commands being executed to drop interactive features like progress bars -- pip --progress-bar no, etc

I'm really hoping someone has an answer and the first option is possible. However, I'm guess most tools are like Sphinx: https://github.com/sphinx-doc/sphinx/blob/fe47bf46cbb6a615b01d6c5dc9fdcac58c940f69/sphinx/util/console.py#L59-L72

That is, if the terminal output is not a ptty, there is no color.


Related:

  • https://discuss.circleci.com/t/disable-interactive-mode-tty-pip-and-progress-bars-problem/1059/8 -- solution is to disable tty and color by piping to a non ptty

CircleCI smooshes lines like progress bars into a single line:

image

agjohnson avatar Nov 30 '21 22:11 agjohnson

Success!

image

This is with tty=True on all commands. This wasn't a setting but was instead a matter of simulating printing the string (and thus allowing the cursor to drift around with all of the carriage return string overwriting.

image

agjohnson avatar Dec 01 '21 00:12 agjohnson

So, what I did was implement an output parser that seeks back to the last \n for each \r in the string. Unfortunately, there are more control characters here. There is a BSD util (installable on ubuntu even) that does some of this, though also is not a pty. This is the source of the col command:

https://github.com/pexip/os-bsdmainutils/blob/master/usr.bin/col/col.c

agjohnson avatar Dec 01 '21 03:12 agjohnson

Example changes are at c1bcf6ca6

agjohnson avatar Dec 10 '21 17:12 agjohnson