2

I am curious that how a program determines its terminal supports colour as I am trying to create some terminal apps like this, however it doesn't seem to be outputting colours (just plain gray text) and upon inspecting the environment of the java it runs, I found

HOME=/home/user
LANG=en_US.UTF-8
LOGNAME=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
SHELL=/bin/bash
SUDO_COMMAND=/usr/local/bin/minecraftd -d
SUDO_GID=1000
SUDO_UID=1000
SUDO_USER=user
TERM=xterm-256color
USER=root
USERNAME=root

Which has a $TERM supporting colours.

And upon inspecting the environment of another process which outputs colours properly (FYI it's bash), I found

GCC_COLORS=error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01
HOME=/home/user
LANG=en_US.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
LESSOPEN=| /usr/bin/lesspipe %s
LOGNAME=user
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42
MAIL=/var/mail/user
MYSQL_PS1=\u@\h [\c/\d]>
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PWD=/home/user
SHELL=/bin/bash
SHLVL=1
SSH_CLIENT=[irrelevant]
SSH_CONNECTION=[irrelevant]
SSH_TTY=/dev/pts/4
TERM=screen
TMUX=/tmp/tmux-1000/default,28912,0
TMUX_PANE=%3
USER=user
XDG_RUNTIME_DIR=/run/user/1000
XDG_SESSION_ID=c57
_=/usr/bin/tmux

Which also has a $TERM supporting colours.

My question is, how a program determine if colours is supported because I have troubles figuring it out.

3

The value of TERM denotes color if one of these is true:

  • a program uses TERM to find a terminal description (in terminfo or termcap) and looks at the capabilities in that description to see if it supports color, or

  • a program uses some other mechanism (usually hardcoded) to associate that value with "color".

A Java application won't be using the terminal database, except rarely (rare enough that you can take it for granted that it's not a possibility). It might use some hard-coded behavior (but then it will have to construct escape sequences and write those — again, not often done).

Some programs which can produce escape sequences will check if their output is redirected (not going to a terminal), and will suppress those escape sequences to make log-files, etc., easier to read. You can work around those by running them in script (which makes the process running in script "see" that it is writing to a terminal, while script captures the output to its own typescript file).

  • The problem I encountered is that, when running the Java program from screen, the colours are normal, but the colours didn't show up when I am piping it's stdout. Any clues? – lkp111138 Jun 11 '17 at 13:26
  • @lkp111138: many programs and terminal libraries automatically detects if it's connected to a pipe instead of a terminal, and turns off color output. There's sometimes a command line argument in the program to force enable color output (and/or to force disable color output). – Lie Ryan Jun 11 '17 at 13:33
  • @LieRyan Is there any way that I can trick it into thinking that its stdout is a terminal? – lkp111138 Jun 11 '17 at 13:52
  • @lkp111138: programs usually detect that it is connected to a tty by calling isatty() syscall. isatty() will return true when the file descriptor is connected to a character special device file of type tty. You can create such pseudo tty device file by requesting one from /dev/ptmx using posix_openpt(). From shell script, you may try using expect to do the ground work of opening pty file. – Lie Ryan Jun 11 '17 at 14:49
  • You can then connect the program's stdin/stdout/stderr to the pty. Generally, just telling the program explicitly that it should skip terminal detection is the easiest way to force color though. – Lie Ryan Jun 11 '17 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.