Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LangLangBart opened this issue Nov 26, 2024 · 4 comments

Comments

@LangLangBart
Copy link

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:

Footnotes

  1. less functionality is reduced in preview window · Issue #1118 · junegunn/fzf

@gwsw
Copy link
Owner

gwsw commented Nov 26, 2024

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
Copy link
Author

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.

@gwsw
Copy link
Owner

gwsw commented Nov 30, 2024

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

@LangLangBart
Copy link
Author

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants