laike9m / pdir2

Pretty dir() printing with joy:beer:
MIT License
1.33k stars 47 forks source link

Disable coloring doesn't work? #65

Closed tkossak closed 2 years ago

tkossak commented 2 years ago

Hello, i tried disabling coloring wiith config file:

$ cat ~/.pdir2config
[global]
uniform-color = bright white

but pdir still prints color characters in xonsh eg, when I run pdir(__xonsh__.history) lines look like:

?[1;37mfunction:?[0m
    ?[1;37mall_items?[0m?[1;37m: ?[0m?[1;37mReturns all history as found in XONSH_DATA_DIR.?[0m
    ?[1;37mappend?[0m?[1;37m: ?[0m?[1;37mAppends command to history.?[0m
    ?[1;37mclear?[0m?[1;37m: ?[0m?[1;37mClears the current session's history from both memory and disk.?[0m
    ?[1;37mflush?[0m?[1;37m: ?[0m?[1;37mFlushes the current command buffer to disk.?[0m
    ?[1;37minfo?[0m?[1;37m: ?[0m?[1;37mA collection of information about the shell history.?[0m
    ?[1;37mitems?[0m?[1;37m: ?[0m?[1;37mDisplay history items of current session.?[0m
    ?[1;37mrun_gc?[0m?[1;37m: ?[0m?[1;37mRun the garbage collector.?[0m

How to get rid of these characters? Either disable coloring or make them work for xonsh.

laike9m commented 2 years ago

Thanks for the bug report. xonsh is actually not officially supported, but I think I can look into it https://xon.sh/customization.html#color-my-man-pages

tkossak commented 2 years ago

It would be useful if it had 'disable colors' option. Then it would work everywhere, xonsh or not. Thank you for awesome module!

laixintao commented 2 years ago

I think this issue is not related to xonsh. The color is not related to shell, it's related to terminal instead, when we print something from terminal, like printf "\\x1b[93mfoo\\x1b[92mbar"(escape code), the terminal (iTerm2) will render it to colorful text.

image

The config uniform-color = bright white is not really "disable" colors, instead, it still output text with colors, just the color is white. That's why you will still see those escape codes.

Cli usually check if the current stdout is a TTY or not, if it is, cli will output with colors.

image

If it is not a TTY, then will output without colors(aka, without the escape code)

image

If you want, you can still enable color even it is not a TTY.

image

So I think we can do the same in pdir2:

  1. if ENV PDIR2_NOCOLOR is set, then disable color always
  2. otherwise, if config output_color = auto which is the default setting, then pdir2 will output with colors if current stdout is a TTY, otherwise output plain text. If the config was set to output_color=True or output_color=False , then always follows the config.

What do you think?

tkossak commented 2 years ago

That would be awesome! Pdir2 could start working not only in xonsh, but also in vscode and other places that do not accept standard terminal escape sequences. Thank you.

laike9m commented 2 years ago

I'll cut a new release this weekend

laike9m commented 2 years ago

In 0.3.6 you can disable color, see https://github.com/laike9m/pdir2/wiki/User-Configuration#disable-color

Let me know if you encounter any issues.