plumbum icon indicating copy to clipboard operation
plumbum copied to clipboard

how do i use colors on windows command line?

Open leileigong opened this issue 4 years ago • 8 comments

i try the demo.

class MyApp_colour(cli.Application):
    PROGNAME = colors.green
    VERSION = colors.blue | "1.0.2"
    COLOR_GROUPS = {"Meta-switches" : colors.bold & colors.yellow}
    opts =  cli.Flag("--ops", help=colors.magenta | "This is help")

    def main(self, filename):
        pass

whether dos shell or mingw64, the color the not works. How can i use colors?

leileigong avatar Dec 25 '20 11:12 leileigong

Can you try pip install colorama? We use colorama to handle the (non-trivial) conversion to CMD’s weird color system, but don’t require it to install plumbum.

henryiii avatar Dec 25 '20 17:12 henryiii

i install colorama. But i got an error when i used COLOR_GROUPS

my demo code like this.

from colorama import Fore,Style, init
class MyApp_colour(cli.Application):
    init(autoreset=True)
    PROGNAME = (Fore.GREEN + Style.BRIGHT)  + "TESTAPP"
    VERSION = Fore.BLUE + "1.0.2"
    COLOR_GROUPS = {"Meta-switches" :  (Style.BRIGHT + Fore.YELLOW)}
    opts =  cli.Flag("--ops", help=Fore.MAGENTA + "This is help")

    def main(self, filename):
        pass

here is error traceback info:

Traceback (most recent call last):
  File "plumbum-demo1.py", line 310, in <module>
    MyApp_colour.run()
  File "C:\Users\lei\.virtualenvs\samplecode-oJY8VmVs\lib\site-packages\plumbum\cli\application.py", line 555, in run
    inst.help()
  File "C:\Users\lei\.virtualenvs\samplecode-oJY8VmVs\lib\site-packages\plumbum\cli\application.py", line 870, in help
    for switch_info, prefix, color in switchs(by_groups, True):
  File "C:\Users\lei\.virtualenvs\samplecode-oJY8VmVs\lib\site-packages\plumbum\cli\application.py", line 842, in switchs
    print(self.COLOR_GROUPS[grp] | lgrp + ':')
TypeError: unsupported operand type(s) for |: 'str' and 'str'

leileigong avatar Dec 27 '20 08:12 leileigong

the error occured in file application.py , line 842, print(self.COLOR_GROUPS[grp] | lgrp + ':') and line 892 print(description_indent.format(color | prefix, padding, color | msg)).

if colorama is used, the type of self.COLOR_GROUPS[grp] and color are string, the operation | is not allowed. Maybe they should be modify as follows. print(self.COLOR_GROUPS[grp] + lgrp + ':') and print(description_indent.format(color + prefix, padding, color + msg)).

leileigong avatar Dec 27 '20 09:12 leileigong

Just init colorama, then use plumbum colors like normal. Colorama, after calling init, intercepts and translates Unix style colors automatically.

henryiii avatar Dec 27 '20 14:12 henryiii

yeah,i did that.

class MyApp_colour_nix(cli.Application):
    PROGNAME = colors.green
    VERSION = colors.BLUE | "1.0.2"
    COLOR_GROUPS = {"Meta-switches" : colors.bold & colors.yellow}
    opts =  cli.Flag("--ops", help=colors.magenta | "This is help")

    def main(self, filename):
        pass

But, the operate & and | can not be used by colorama, which get an error. i must use + replace them.

like this.

from colorama import Fore,Style, init
class MyApp_colour(cli.Application):
    init(autoreset=True)
    PROGNAME = (Fore.GREEN + Style.BRIGHT)  + "TESTAPP"
    VERSION = Fore.BLUE + "1.0.2"
    COLOR_GROUPS = {"Meta-switches" :  (Style.BRIGHT + Fore.YELLOW)}
    opts =  cli.Flag("--ops", help=Fore.MAGENTA + "This is help")

    def main(self, filename):
        pass

leileigong avatar Dec 29 '20 04:12 leileigong

This is what you should have:

from colorama import init
init()

colors.use_color=1  # Force plumbum to output basic color sequences.

class MyApp_colour_nix(cli.Application):
    PROGNAME = colors.green
    VERSION = colors.BLUE | "1.0.2"
    COLOR_GROUPS = {"Meta-switches" : colors.bold & colors.yellow}
    opts =  cli.Flag("--ops", help=colors.magenta | "This is help")

    def main(self, filename):
        pass

Colorama's "init" function intercepts ANSI escape sequences and turns them into colors on Windows. Plumbum will not detect this [^1], so you have to force it to use colors. "1" is basic 8-color support, which is all colorama does, I think. See here, in the docs.

[^1]: We could possibly improve detection. Certainly should improve docs.

henryiii avatar Dec 29 '20 05:12 henryiii

By the way, + in Colorama's simple color system and | in Plumbum are different. str | color will turn the color on for the string, then back off again afterwords - + just changes the color in place and doesn't change it back.

henryiii avatar Dec 29 '20 05:12 henryiii

I got it. thanks very much!

leileigong avatar Dec 30 '20 05:12 leileigong