gwsw / less

Less - text pager
http://greenwoodsoftware.com/less
Other
573 stars 88 forks source link

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

Open LangLangBart opened 1 day ago

LangLangBart commented 1 day ago

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.

<img src="https://github.com/user-attachments/assets/4a959740-1e61-45b8-a9e8-868e259d162d" width="600">

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:

<img src="https://github.com/user-attachments/assets/971f5dbb-d34f-4b46-ba4d-59d48b732633" width="400">

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

<img src="https://github.com/user-attachments/assets/30136f69-ad5b-464f-a8c0-2eac6832e7be" width="400">

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

gwsw commented 20 hours ago

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.

LangLangBart commented 12 hours ago

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.