1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#!/usr/bin/env python
# coding=utf-8
"""
A sample application for cmd2. Demonstrating colorized output.
Experiment with the command line options on the `speak` command to see how
different output colors ca
The colors setting has three possible values:
Never
poutput() and pfeedback() strip all ANSI escape sequences
which instruct the terminal to colorize output
Terminal
(the default value) poutput() and pfeedback() do not strip any ANSI escape
sequences when the output is a terminal, but if the output is a pipe or a
file the escape sequences are stripped. If you want colorized output you
must add ANSI escape sequences, preferably using some python color library
like `plumbum.colors`, `colorama`, `blessings`, or `termcolor`.
Always
poutput() and pfeedback() never strip ANSI escape sequences, regardless of
the output destination
"""
import random
import argparse
import cmd2
from colorama import Fore, Back
FG_COLORS = {
'black': Fore.BLACK,
'red': Fore.RED,
'green': Fore.GREEN,
'yellow': Fore.YELLOW,
'blue': Fore.BLUE,
'magenta': Fore.MAGENTA,
'cyan': Fore.CYAN,
'white': Fore.WHITE,
}
BG_COLORS = {
'black': Back.BLACK,
'red': Back.RED,
'green': Back.GREEN,
'yellow': Back.YELLOW,
'blue': Back.BLUE,
'magenta': Back.MAGENTA,
'cyan': Back.CYAN,
'white': Back.WHITE,
}
class CmdLineApp(cmd2.Cmd):
"""Example cmd2 application demonstrating colorized output."""
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
# default_to_shell = True
MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
MUMBLE_FIRST = ['so', 'like', 'well']
MUMBLE_LAST = ['right?']
def __init__(self):
self.multiline_commands = ['orate']
self.maxrepeats = 3
# Add stuff to settable and shortcuts before calling base class initializer
self.settable['maxrepeats'] = 'max repetitions for speak command'
self.shortcuts.update({'&': 'speak'})
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
super().__init__(use_ipython=True)
speak_parser = argparse.ArgumentParser()
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
speak_parser.add_argument('-f', '--fg', choices=FG_COLORS, help='foreground color to apply to output')
speak_parser.add_argument('-b', '--bg', choices=BG_COLORS, help='background color to apply to output')
speak_parser.add_argument('words', nargs='+', help='words to say')
@cmd2.with_argparser(speak_parser)
def do_speak(self, args):
"""Repeats what you tell me to."""
words = []
for word in args.words:
if args.piglatin:
word = '%s%say' % (word[1:], word[0])
if args.shout:
word = word.upper()
words.append(word)
repetitions = args.repeat or 1
color_on = ''
if args.fg:
color_on += FG_COLORS[args.fg]
if args.bg:
color_on += BG_COLORS[args.bg]
color_off = Fore.RESET + Back.RESET
for i in range(min(repetitions, self.maxrepeats)):
# .poutput handles newlines, and accommodates output redirection too
self.poutput(color_on + ' '.join(words) + color_off)
do_say = do_speak # now "say" is a synonym for "speak"
do_orate = do_speak # another synonym, but this one takes multi-line input
mumble_parser = argparse.ArgumentParser()
mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
mumble_parser.add_argument('-f', '--fg', help='foreground color to apply to output')
mumble_parser.add_argument('-b', '--bg', help='background color to apply to output')
mumble_parser.add_argument('words', nargs='+', help='words to say')
@cmd2.with_argparser(mumble_parser)
def do_mumble(self, args):
"""Mumbles what you tell me to."""
color_on = ''
if args.fg and args.fg in FG_COLORS:
color_on += FG_COLORS[args.fg]
if args.bg and args.bg in BG_COLORS:
color_on += BG_COLORS[args.bg]
color_off = Fore.RESET + Back.RESET
repetitions = args.repeat or 1
for i in range(min(repetitions, self.maxrepeats)):
output = []
if random.random() < .33:
output.append(random.choice(self.MUMBLE_FIRST))
for word in args.words:
if random.random() < .40:
output.append(random.choice(self.MUMBLES))
output.append(word)
if random.random() < .25:
output.append(random.choice(self.MUMBLE_LAST))
self.poutput(color_on + ' '.join(output) + color_off)
if __name__ == '__main__':
c = CmdLineApp()
c.cmdloop()
|