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

Is hSupportsANSIColor correct in emacs? #150

Open
Bodigrim opened this issue Feb 3, 2023 · 7 comments
Open

Is hSupportsANSIColor correct in emacs? #150

Bodigrim opened this issue Feb 3, 2023 · 7 comments

Comments

@Bodigrim
Copy link
Contributor

Bodigrim commented Feb 3, 2023

hSupportsANSIColor :: Handle -> IO Bool
hSupportsANSIColor h = (||) <$> hSupportsANSI h <*> isEmacsTerm
where
isEmacsTerm = (\env -> (insideEmacs env) && (isDumb env)) <$> getEnvironment
insideEmacs env = any (\(k, _) -> k == "INSIDE_EMACS") env
isDumb env = Just "dumb" == lookup "TERM" env

hSupportsANSIColor was introduced in #75 by @fommil to implement UnkindPartition/tasty#233, but I struggle to find any reference for this incantation.

If I open emacs, go for M-x compile and probe the terminal I get TERM=dumb and INSIDE_EMACS=28.2,compile. But this terminal still does not support colors (after all, it's dumb) as can be checked by, say, running any tasty test suite:

All
  Builder
    Data.ByteString.Builder
      toLazyByteStringWith:                                �[32;22mOK�[m�[32;22m (0.08s)�[m�[32;22m

If I go fo M-x shell then TERM=xterm-color and INSIDE_EMACS=28.2,compile, and this terminal indeed support colors. But this case is already handled by normal hSupportsANSI:

hSupportsANSI h = (&&) <$> hIsTerminalDevice h <*> isNotDumb
where
-- cannot use lookupEnv since it only appeared in GHC 7.6
isNotDumb = (/= Just "dumb") . lookup "TERM" <$> getEnvironment

(This is my first time running emacs, so maybe I'm doing something horribly stupid?..)

@sergv
Copy link

sergv commented Feb 4, 2023

The gist is that even if you do M-x compile and the terminal is TERM=dumb but there's also INSIDE_EMACS=28.2,compile then Emacs can do something on its side to process escape sequences. The default config you used didn't do anything so you saw the escsapes.

This line in one of the linked PRs https://github.com/haskell/haskell-mode/pull/1608/files#diff-170ec957d1245a41f5cd84e55aa1326f9c7ec2021fbb9a679eba7afc2ca7c3ddR107 gives an example of what Emacs can do here - ansi-color-apply-on-region will colorize escape sequences in the buffer. You may even get some luck by calling this function yourself (M-x eval-expression (ansi-color-apply-on-region (point-min) (point-max))) in the compilation buffer you got.

@Bodigrim
Copy link
Contributor Author

Bodigrim commented Feb 4, 2023

OK, so it requires either manually call ansi-color-apply-on-region, or have a specific setup for a buffer as haskell-mode does. It is not an internal capability of the terminal to support ANSI color sequences.

I think this is all quite wrong. If haskell-mode knows that it is going to apply ansi-color-apply-on-region to the output, it's its responsibility to change TERM to not-dumb. It's not up to ansi-terminal to return True from hSupportsANSIColor speculatively, in a (false) hope that the output will be passed through ansi-color-apply-on-region, which does not happen by default.

Thus, I suggest deprecating hSupportsANSIColor and/or setting hSupportsANSIColor = hSupportsANSI in the meantime.

@fommil
Copy link
Contributor

fommil commented Feb 4, 2023

haskell-mode is not the only show in town. c.f. https://gitlab.com/tseenshe/haskell-tng.el

Do what you feel is best, but please don't break tools that are working just fine.

@Bodigrim
Copy link
Contributor Author

haskell-tng does the same: it explicitly runs ans-color-apply-on-region at https://gitlab.com/tseenshe/haskell-tng.el/-/blob/tng/haskell-tng-compile.el#L127. It should have changed TERM to not-dumb as well, to signal programs that they can emit colors.

@Bodigrim
Copy link
Contributor Author

@andreasabel as an Agda developer, I suspect you are an Emacs user ;) Could you possibly opine?

@andreasabel
Copy link
Contributor

andreasabel commented Apr 22, 2024

So on macOS with my emacs I get in M-x shell:

bash-3.2$ echo $TERM
dumb
bash-3.2$ echo $INSIDE_EMACS
29.1,comint

This seems to match the isEmacsTerm condition you quote. (And it supports colors.)

@Bodigrim
Copy link
Contributor Author

@andreasabel my point was that having $INSIDE_EMACS set does not make Emacs terminal natively capable of ANSI escape sequences for colors. One has to execute ansi-color-apply-on-region manually (or have it applied by a plugin), in which case they should not advertise their $TERM as dumb.

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

No branches or pull requests

4 participants