66Experiment with the command line options on the `speak` command to see how
77different output colors ca
88
9- The colors setting has three possible values:
9+ The allow_ansi setting has three possible values:
1010
1111Never
1212 poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences
1616 (the default value) poutput(), pfeedback(), and ppaged() do not strip any
1717 ANSI escape sequences when the output is a terminal, but if the output is
1818 a pipe or a file the escape sequences are stripped. If you want colorized
19- output you must add ANSI escape sequences, preferably using some python
20- color library like `plumbum.colors`, `colorama`, `blessings`, or
21- `termcolor`.
19+ output you must add ANSI escape sequences using either cmd2's internal ansi
20+ module or another color library such as `plumbum.colors` or `colorama`.
2221
2322Always
2423 poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences,
2524 regardless of the output destination
2625"""
27-
28- import random
2926import argparse
3027
3128import cmd2
32- from colorama import Fore , Back
33-
34- FG_COLORS = {
35- 'black' : Fore .BLACK ,
36- 'red' : Fore .RED ,
37- 'green' : Fore .GREEN ,
38- 'yellow' : Fore .YELLOW ,
39- 'blue' : Fore .BLUE ,
40- 'magenta' : Fore .MAGENTA ,
41- 'cyan' : Fore .CYAN ,
42- 'white' : Fore .WHITE ,
43- }
44- BG_COLORS = {
45- 'black' : Back .BLACK ,
46- 'red' : Back .RED ,
47- 'green' : Back .GREEN ,
48- 'yellow' : Back .YELLOW ,
49- 'blue' : Back .BLUE ,
50- 'magenta' : Back .MAGENTA ,
51- 'cyan' : Back .CYAN ,
52- 'white' : Back .WHITE ,
53- }
29+ from cmd2 import ansi
5430
5531
5632class CmdLineApp (cmd2 .Cmd ):
5733 """Example cmd2 application demonstrating colorized output."""
58-
59- # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
60- # default_to_shell = True
61- MUMBLES = ['like' , '...' , 'um' , 'er' , 'hmmm' , 'ahh' ]
62- MUMBLE_FIRST = ['so' , 'like' , 'well' ]
63- MUMBLE_LAST = ['right?' ]
64-
6534 def __init__ (self ):
66- shortcuts = dict (cmd2 .DEFAULT_SHORTCUTS )
67- shortcuts .update ({'&' : 'speak' })
6835 # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
69- super ().__init__ (use_ipython = True , multiline_commands = [ 'orate' ], shortcuts = shortcuts )
36+ super ().__init__ (use_ipython = True )
7037
7138 self .maxrepeats = 3
7239 # Make maxrepeats settable at runtime
7340 self .settable ['maxrepeats' ] = 'max repetitions for speak command'
7441
42+ # Should ANSI color output be allowed
43+ self .allow_ansi = ansi .ANSI_TERMINAL
44+
7545 speak_parser = argparse .ArgumentParser ()
7646 speak_parser .add_argument ('-p' , '--piglatin' , action = 'store_true' , help = 'atinLay' )
7747 speak_parser .add_argument ('-s' , '--shout' , action = 'store_true' , help = 'N00B EMULATION MODE' )
7848 speak_parser .add_argument ('-r' , '--repeat' , type = int , help = 'output [n] times' )
79- speak_parser .add_argument ('-f' , '--fg' , choices = FG_COLORS , help = 'foreground color to apply to output' )
80- speak_parser .add_argument ('-b' , '--bg' , choices = BG_COLORS , help = 'background color to apply to output' )
49+ speak_parser .add_argument ('-f' , '--fg' , choices = ansi . FG_COLORS , help = 'foreground color to apply to output' )
50+ speak_parser .add_argument ('-b' , '--bg' , choices = ansi . BG_COLORS , help = 'background color to apply to output' )
8151 speak_parser .add_argument ('words' , nargs = '+' , help = 'words to say' )
8252
8353 @cmd2 .with_argparser (speak_parser )
@@ -92,49 +62,11 @@ def do_speak(self, args):
9262 words .append (word )
9363
9464 repetitions = args .repeat or 1
95-
96- color_on = ''
97- if args .fg :
98- color_on += FG_COLORS [args .fg ]
99- if args .bg :
100- color_on += BG_COLORS [args .bg ]
101- color_off = Fore .RESET + Back .RESET
65+ output_str = ansi .style (' ' .join (words ), fg = args .fg , bg = args .bg )
10266
10367 for i in range (min (repetitions , self .maxrepeats )):
10468 # .poutput handles newlines, and accommodates output redirection too
105- self .poutput (color_on + ' ' .join (words ) + color_off )
106-
107- do_say = do_speak # now "say" is a synonym for "speak"
108- do_orate = do_speak # another synonym, but this one takes multi-line input
109-
110- mumble_parser = argparse .ArgumentParser ()
111- mumble_parser .add_argument ('-r' , '--repeat' , type = int , help = 'how many times to repeat' )
112- mumble_parser .add_argument ('-f' , '--fg' , help = 'foreground color to apply to output' )
113- mumble_parser .add_argument ('-b' , '--bg' , help = 'background color to apply to output' )
114- mumble_parser .add_argument ('words' , nargs = '+' , help = 'words to say' )
115-
116- @cmd2 .with_argparser (mumble_parser )
117- def do_mumble (self , args ):
118- """Mumbles what you tell me to."""
119- color_on = ''
120- if args .fg and args .fg in FG_COLORS :
121- color_on += FG_COLORS [args .fg ]
122- if args .bg and args .bg in BG_COLORS :
123- color_on += BG_COLORS [args .bg ]
124- color_off = Fore .RESET + Back .RESET
125-
126- repetitions = args .repeat or 1
127- for i in range (min (repetitions , self .maxrepeats )):
128- output = []
129- if random .random () < .33 :
130- output .append (random .choice (self .MUMBLE_FIRST ))
131- for word in args .words :
132- if random .random () < .40 :
133- output .append (random .choice (self .MUMBLES ))
134- output .append (word )
135- if random .random () < .25 :
136- output .append (random .choice (self .MUMBLE_LAST ))
137- self .poutput (color_on + ' ' .join (output ) + color_off )
69+ self .poutput (output_str )
13870
13971
14072if __name__ == '__main__' :
0 commit comments