less icon indicating copy to clipboard operation
less copied to clipboard

[FEATURE] Option to retain 'LESS_TERMCAP_xx' colors in non-interactive terminals

Open LangLangBart opened this issue 1 year ago • 4 comments

Problem

When a user defines LESS_TERMCAP_xx, these colors are not applied when the output of less is going to a non-interactive terminal.

Consider this Docker command as an example:

docker run --tty --rm \
  -e LESS_TERMCAP_md=$(tput bold; tput setaf 4) \
  alpine sh -c 'apk add less; printf "F\bFO\bOO\bO\nBAR\n" | less -RF'

In an interactive terminal, FOO is displayed in bold blue. However, if you run the same Docker command without the --tty flag, FOO is not colored or bold.

Feature Request

Would it be possible to add a new option that forces LESS_TERMCAP_xx colors to be retained when the output of less goes to a non-interactive terminal?


More context

My use case involves fzf and its --preview window, which executes the command in non-interactive mode[^1].

The command downloads a html file using curl, and the html2text transforms into text but retains backspaces. Finally, less would make the output more readable by adding the user-defined LESS_TERMCAP_xx colors.

export LESS_TERMCAP_md=$(tput bold; tput setaf 4)
: | fzf \
  --preview-window 'left,80%' \
  --preview 'curl -fsSL https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/printf.html | html2text | less'

Currently, the result looks like this:

But I would like it to look like this inside fzf's preview window:

[^1]: less functionality is reduced in preview window · Issue #1118 · junegunn/fzf

LangLangBart avatar Nov 26 '24 06:11 LangLangBart

Perhaps I'm not fully understanding, but if you're not using less for paging but only for adding coloring, I'm not sure that it's the right tool for the job. It seems like you could do what you want with a simple sed script or something like that.

gwsw avatar Nov 26 '24 19:11 gwsw

Perhaps I'm not fully understanding

The request is to introduce an optional flag that retains the colors when the output is not a tty device.

For similar use cases, git offers the --color=always flag. However, less currently lacks such a feature.

I'm not sure that it's the right tool for the job.

That is the question: do you think less should be able to do this or not? Whatever you decide will be accepted.

It seems like you could do what you want with a simple sed script or something like that

For the use case outlined in the description of the issue, one can also use bat --color=always -pl man, but it would look better with less.

LangLangBart avatar Nov 27 '24 03:11 LangLangBart

Can you explain why it would look better using less rather than bat?

gwsw avatar Nov 30 '24 18:11 gwsw

Can you explain why it would look better using less rather than bat?

Having an option to retain the colors in less would improve the appearance because it ensures consistent highlighting, whether you're in an interactive or a non-interactive terminal. This consistency helps to spot visual cues more easily, making complex outputs easier to read.

The interactive command is shown below and creates the output you see in the image below. Run the command without the --interactive and --tty flags for the non-interactive version. By default, bat uses --pager "less -RF".

docker run --interactive --tty --rm  \
  -e LESS_TERMCAP_md=$(tput bold; tput setaf 4) \
  -e LESS_TERMCAP_us=$(tput smul; tput setaf 2) \
  -e LESS_TERMCAP_ue=$(tput sgr0) \
  alpine sh -c 'apk add bat curl less html2text; curl -fsSL https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/printf.html | html2text | bat -pl man --color=always'

LangLangBart avatar Dec 01 '24 00:12 LangLangBart

I'm going to say that this seems more trouble than it's worth. Currently when less detects a non-interactive terminal, it just outputs the raw input file, byte by byte, with no processing at all. Implementing this request would require a substantial implementation of a filter, similar to but different than the normal interactive processing, that detects ANSI sequences and applies color. But it would presumably not do any of the other processing normally done in interactive mode, like breaking lines at the terminal width, tab expansion, backspace processing, displaying a printable representation of control characters, etc. I don't think that this feature is worth the trouble and risk. If it were me, I would use a different tool to apply color to non-interactive output.

gwsw avatar Aug 04 '25 18:08 gwsw

Closing this, per the reasoning above.

LangLangBart avatar Aug 04 '25 19:08 LangLangBart