PyInquirer 1.0.4__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PyInquirer/__init__.py +29 -0
- PyInquirer/color_print.py +41 -0
- PyInquirer/prompt.py +106 -0
- PyInquirer/prompts/__init__.py +0 -0
- PyInquirer/prompts/checkbox.py +256 -0
- PyInquirer/prompts/common.py +92 -0
- PyInquirer/prompts/confirm.py +86 -0
- PyInquirer/prompts/editor.py +182 -0
- PyInquirer/prompts/expand.py +195 -0
- PyInquirer/prompts/input.py +62 -0
- PyInquirer/prompts/list.py +191 -0
- PyInquirer/prompts/password.py +14 -0
- PyInquirer/prompts/rawlist.py +183 -0
- PyInquirer/separator.py +15 -0
- PyInquirer/utils.py +34 -0
- examples/__init__.py +33 -0
- examples/checkbox.py +71 -0
- examples/confirm.py +32 -0
- examples/editor.py +28 -0
- examples/expand.py +46 -0
- examples/hierarchical.py +87 -0
- examples/input.py +47 -0
- examples/list.py +55 -0
- examples/password.py +21 -0
- examples/pizza.py +110 -0
- examples/rawlist.py +36 -0
- examples/when.py +46 -0
- pyinquirer-1.0.4.dist-info/METADATA +403 -0
- pyinquirer-1.0.4.dist-info/RECORD +32 -0
- pyinquirer-1.0.4.dist-info/WHEEL +6 -0
- pyinquirer-1.0.4.dist-info/licenses/LICENSE +19 -0
- pyinquirer-1.0.4.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
`rawlist` type question
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import print_function, unicode_literals
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
from prompt_toolkit.application import Application
|
|
9
|
+
from prompt_toolkit.key_binding.manager import KeyBindingManager
|
|
10
|
+
from prompt_toolkit.keys import Keys
|
|
11
|
+
from prompt_toolkit.layout.containers import Window
|
|
12
|
+
from prompt_toolkit.filters import IsDone
|
|
13
|
+
from prompt_toolkit.layout.controls import TokenListControl
|
|
14
|
+
from prompt_toolkit.layout.containers import ConditionalContainer, HSplit
|
|
15
|
+
from prompt_toolkit.layout.dimension import LayoutDimension as D
|
|
16
|
+
from prompt_toolkit.token import Token
|
|
17
|
+
|
|
18
|
+
from .. import PromptParameterException
|
|
19
|
+
from ..separator import Separator
|
|
20
|
+
from .common import default_style
|
|
21
|
+
from .common import if_mousedown
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
PY3 = sys.version_info[0] >= 3
|
|
25
|
+
|
|
26
|
+
if PY3:
|
|
27
|
+
basestring = str
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# custom control based on TokenListControl
|
|
31
|
+
|
|
32
|
+
class InquirerControl(TokenListControl):
|
|
33
|
+
def __init__(self, choices, default=None, **kwargs):
|
|
34
|
+
self.pointer_index = 0
|
|
35
|
+
self.answered = False
|
|
36
|
+
self._init_choices(choices, default)
|
|
37
|
+
super(InquirerControl, self).__init__(self._get_choice_tokens,
|
|
38
|
+
**kwargs)
|
|
39
|
+
|
|
40
|
+
def _init_choices(self, choices, default=None):
|
|
41
|
+
# helper to convert from question format to internal format
|
|
42
|
+
self.choices = [] # list (key, name, value)
|
|
43
|
+
searching_first_choice = True
|
|
44
|
+
key = 1 # used for numeric keys
|
|
45
|
+
for i, c in enumerate(choices):
|
|
46
|
+
if isinstance(c, Separator):
|
|
47
|
+
self.choices.append(c)
|
|
48
|
+
else:
|
|
49
|
+
if isinstance(c, dict):
|
|
50
|
+
name = c['name']
|
|
51
|
+
self.choices.append((key, name, c.get('value', name)))
|
|
52
|
+
else:
|
|
53
|
+
self.choices.append((key, c, c))
|
|
54
|
+
key += 1
|
|
55
|
+
if searching_first_choice:
|
|
56
|
+
self.pointer_index = i # found the first choice
|
|
57
|
+
searching_first_choice = False
|
|
58
|
+
if default is not None:
|
|
59
|
+
self._select_default(default)
|
|
60
|
+
|
|
61
|
+
def _select_default(self, default):
|
|
62
|
+
# the default is the value or the index of a choice
|
|
63
|
+
for i, c in enumerate(self.choices):
|
|
64
|
+
if not isinstance(c, Separator) and c[2] == default:
|
|
65
|
+
self.pointer_index = i
|
|
66
|
+
return
|
|
67
|
+
if isinstance(default, int) and not isinstance(default, bool) and \
|
|
68
|
+
0 <= default < len(self.choices) and \
|
|
69
|
+
not isinstance(self.choices[default], Separator):
|
|
70
|
+
self.pointer_index = default
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def choice_count(self):
|
|
74
|
+
return len(self.choices)
|
|
75
|
+
|
|
76
|
+
def _get_choice_tokens(self, cli):
|
|
77
|
+
tokens = []
|
|
78
|
+
T = Token
|
|
79
|
+
|
|
80
|
+
def _append(index, line):
|
|
81
|
+
if isinstance(line, Separator):
|
|
82
|
+
tokens.append((T.Separator, ' %s\n' % line))
|
|
83
|
+
else:
|
|
84
|
+
key = line[0]
|
|
85
|
+
line = line[1]
|
|
86
|
+
pointed_at = (index == self.pointer_index)
|
|
87
|
+
|
|
88
|
+
@if_mousedown
|
|
89
|
+
def select_item(cli, mouse_event):
|
|
90
|
+
# bind option with this index to mouse event
|
|
91
|
+
self.pointer_index = index
|
|
92
|
+
|
|
93
|
+
if pointed_at:
|
|
94
|
+
tokens.append((T.Selected, ' %d) %s' % (key, line),
|
|
95
|
+
select_item))
|
|
96
|
+
else:
|
|
97
|
+
tokens.append((T, ' %d) %s' % (key, line),
|
|
98
|
+
select_item))
|
|
99
|
+
|
|
100
|
+
tokens.append((T, '\n'))
|
|
101
|
+
|
|
102
|
+
# prepare the select choices
|
|
103
|
+
for i, choice in enumerate(self.choices):
|
|
104
|
+
_append(i, choice)
|
|
105
|
+
tokens.append((T, ' Answer: %d' % self.choices[self.pointer_index][0]))
|
|
106
|
+
return tokens
|
|
107
|
+
|
|
108
|
+
def get_selected_value(self):
|
|
109
|
+
# get value not label
|
|
110
|
+
return self.choices[self.pointer_index][2]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def question(message, **kwargs):
|
|
114
|
+
# TODO extract common parts for list, checkbox, rawlist, expand
|
|
115
|
+
if not 'choices' in kwargs:
|
|
116
|
+
raise PromptParameterException('choices')
|
|
117
|
+
# this does not implement default, use checked...
|
|
118
|
+
# TODO
|
|
119
|
+
#if 'default' in kwargs:
|
|
120
|
+
# raise ValueError('rawlist does not implement \'default\' '
|
|
121
|
+
# 'use \'checked\':True\' in choice!')
|
|
122
|
+
qmark = kwargs.pop('qmark', '?')
|
|
123
|
+
choices = kwargs.pop('choices', None)
|
|
124
|
+
default = kwargs.pop('default', None)
|
|
125
|
+
if len(choices) > 9:
|
|
126
|
+
raise ValueError('rawlist supports only a maximum of 9 choices!')
|
|
127
|
+
|
|
128
|
+
# TODO style defaults on detail level
|
|
129
|
+
style = kwargs.pop('style', default_style)
|
|
130
|
+
|
|
131
|
+
ic = InquirerControl(choices, default)
|
|
132
|
+
|
|
133
|
+
def get_prompt_tokens(cli):
|
|
134
|
+
tokens = []
|
|
135
|
+
T = Token
|
|
136
|
+
|
|
137
|
+
tokens.append((T.QuestionMark, qmark))
|
|
138
|
+
tokens.append((T.Question, ' %s ' % message))
|
|
139
|
+
if ic.answered:
|
|
140
|
+
tokens.append((T.Answer, ' %s' % ic.get_selected_value()))
|
|
141
|
+
return tokens
|
|
142
|
+
|
|
143
|
+
# assemble layout
|
|
144
|
+
layout = HSplit([
|
|
145
|
+
Window(height=D.exact(1),
|
|
146
|
+
content=TokenListControl(get_prompt_tokens)
|
|
147
|
+
),
|
|
148
|
+
ConditionalContainer(
|
|
149
|
+
Window(ic),
|
|
150
|
+
filter=~IsDone()
|
|
151
|
+
)
|
|
152
|
+
])
|
|
153
|
+
|
|
154
|
+
# key bindings
|
|
155
|
+
manager = KeyBindingManager.for_prompt()
|
|
156
|
+
|
|
157
|
+
@manager.registry.add_binding(Keys.ControlQ, eager=True)
|
|
158
|
+
@manager.registry.add_binding(Keys.ControlC, eager=True)
|
|
159
|
+
def _(event):
|
|
160
|
+
raise KeyboardInterrupt()
|
|
161
|
+
|
|
162
|
+
# add key bindings for choices
|
|
163
|
+
for i, c in enumerate(ic.choices):
|
|
164
|
+
if not isinstance(c, Separator):
|
|
165
|
+
def _reg_binding(i, keys):
|
|
166
|
+
# trick out late evaluation with a "function factory":
|
|
167
|
+
# http://stackoverflow.com/questions/3431676/creating-functions-in-a-loop
|
|
168
|
+
@manager.registry.add_binding(keys, eager=True)
|
|
169
|
+
def select_choice(event):
|
|
170
|
+
ic.pointer_index = i
|
|
171
|
+
_reg_binding(i, '%d' % c[0])
|
|
172
|
+
|
|
173
|
+
@manager.registry.add_binding(Keys.Enter, eager=True)
|
|
174
|
+
def set_answer(event):
|
|
175
|
+
ic.answered = True
|
|
176
|
+
event.cli.set_return_value(ic.get_selected_value())
|
|
177
|
+
|
|
178
|
+
return Application(
|
|
179
|
+
layout=layout,
|
|
180
|
+
key_bindings_registry=manager.registry,
|
|
181
|
+
mouse_support=True,
|
|
182
|
+
style=style
|
|
183
|
+
)
|
PyInquirer/separator.py
ADDED
PyInquirer/utils.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from __future__ import print_function
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from pygments import highlight, lexers, formatters
|
|
7
|
+
|
|
8
|
+
__version__ = '0.1.2'
|
|
9
|
+
|
|
10
|
+
PY3 = sys.version_info[0] >= 3
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def format_json(data):
|
|
14
|
+
return json.dumps(data, sort_keys=True, indent=4)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def colorize_json(data):
|
|
18
|
+
if PY3:
|
|
19
|
+
if isinstance(data, bytes):
|
|
20
|
+
data = data.decode('UTF-8')
|
|
21
|
+
else:
|
|
22
|
+
if not isinstance(data, unicode):
|
|
23
|
+
data = unicode(data, 'UTF-8')
|
|
24
|
+
colorful_json = highlight(data,
|
|
25
|
+
lexers.JsonLexer(),
|
|
26
|
+
formatters.TerminalFormatter())
|
|
27
|
+
return colorful_json
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def print_json(data):
|
|
31
|
+
#colorful_json = highlight(unicode(format_json(data), 'UTF-8'),
|
|
32
|
+
# lexers.JsonLexer(),
|
|
33
|
+
# formatters.TerminalFormatter())
|
|
34
|
+
print(colorize_json(format_json(data)))
|
examples/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from PyInquirer import style_from_dict, Token
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
custom_style_1 = style_from_dict({
|
|
5
|
+
Token.Separator: '#cc5454',
|
|
6
|
+
Token.QuestionMark: '#673ab7 bold',
|
|
7
|
+
Token.Selected: '#cc5454', # default
|
|
8
|
+
Token.Pointer: '#673ab7 bold',
|
|
9
|
+
Token.Instruction: '', # default
|
|
10
|
+
Token.Answer: '#f44336 bold',
|
|
11
|
+
Token.Question: '',
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
custom_style_2 = style_from_dict({
|
|
16
|
+
Token.Separator: '#6C6C6C',
|
|
17
|
+
Token.QuestionMark: '#FF9D00 bold',
|
|
18
|
+
#Token.Selected: '', # default
|
|
19
|
+
Token.Selected: '#5F819D',
|
|
20
|
+
Token.Pointer: '#FF9D00 bold',
|
|
21
|
+
Token.Instruction: '', # default
|
|
22
|
+
Token.Answer: '#5F819D bold',
|
|
23
|
+
Token.Question: '',
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
custom_style_3 = style_from_dict({
|
|
28
|
+
Token.QuestionMark: '#E91E63 bold',
|
|
29
|
+
Token.Selected: '#673AB7 bold',
|
|
30
|
+
Token.Instruction: '', # default
|
|
31
|
+
Token.Answer: '#2196f3 bold',
|
|
32
|
+
Token.Question: '',
|
|
33
|
+
})
|
examples/checkbox.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
* Checkbox question example
|
|
4
|
+
* run example by typing `python example/checkbox.py` in your console
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import print_function, unicode_literals
|
|
7
|
+
|
|
8
|
+
from pprint import pprint
|
|
9
|
+
|
|
10
|
+
from PyInquirer import style_from_dict, Token, prompt, Separator
|
|
11
|
+
|
|
12
|
+
from examples import custom_style_2
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
questions = [
|
|
16
|
+
{
|
|
17
|
+
'type': 'checkbox',
|
|
18
|
+
'qmark': '😃',
|
|
19
|
+
'message': 'Select toppings',
|
|
20
|
+
'name': 'toppings',
|
|
21
|
+
'choices': [
|
|
22
|
+
Separator('= The Meats ='),
|
|
23
|
+
{
|
|
24
|
+
'name': 'Ham'
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
'name': 'Ground Meat'
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
'name': 'Bacon'
|
|
31
|
+
},
|
|
32
|
+
Separator('= The Cheeses ='),
|
|
33
|
+
{
|
|
34
|
+
'name': 'Mozzarella',
|
|
35
|
+
'checked': True
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
'name': 'Cheddar'
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
'name': 'Parmesan'
|
|
42
|
+
},
|
|
43
|
+
Separator('= The usual ='),
|
|
44
|
+
{
|
|
45
|
+
'name': 'Mushroom'
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
'name': 'Tomato'
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
'name': 'Pepperoni'
|
|
52
|
+
},
|
|
53
|
+
Separator('= The extras ='),
|
|
54
|
+
{
|
|
55
|
+
'name': 'Pineapple'
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
'name': 'Olives',
|
|
59
|
+
'disabled': 'out of stock'
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
'name': 'Extra cheese'
|
|
63
|
+
}
|
|
64
|
+
],
|
|
65
|
+
'validate': lambda answer: 'You must choose at least one topping.' \
|
|
66
|
+
if len(answer) == 0 else True
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
answers = prompt(questions, style=custom_style_2)
|
|
71
|
+
pprint(answers)
|
examples/confirm.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
* Confirm question example
|
|
4
|
+
* run example by typing `python example/confirm.py` in your console
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import print_function
|
|
7
|
+
|
|
8
|
+
from pprint import pprint
|
|
9
|
+
|
|
10
|
+
from PyInquirer import prompt
|
|
11
|
+
|
|
12
|
+
from examples import custom_style_1
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
questions = [
|
|
16
|
+
{
|
|
17
|
+
'type': 'confirm',
|
|
18
|
+
'message': 'Do you want to continue?',
|
|
19
|
+
'name': 'continue',
|
|
20
|
+
'default': True,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
'type': 'confirm',
|
|
24
|
+
'message': 'Do you want to exit?',
|
|
25
|
+
'name': 'exit',
|
|
26
|
+
'default': False,
|
|
27
|
+
},
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
answers = prompt(questions, style=custom_style_1)
|
|
31
|
+
pprint(answers)
|
|
32
|
+
|
examples/editor.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
* Editor prompt example
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import print_function, unicode_literals
|
|
6
|
+
|
|
7
|
+
from PyInquirer import style_from_dict, Token, prompt, print_json
|
|
8
|
+
from PyInquirer import Validator, ValidationError
|
|
9
|
+
from examples import custom_style_2
|
|
10
|
+
from pprint import pprint
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
questions = [
|
|
14
|
+
{
|
|
15
|
+
'type': 'editor',
|
|
16
|
+
'name': 'bio',
|
|
17
|
+
'message': 'Please write a short bio of at least 3 lines.',
|
|
18
|
+
'default': 'Hello World',
|
|
19
|
+
'validate': lambda text: len(text.split('\n')) >= 3 or 'Must be at least 3 lines.',
|
|
20
|
+
'eargs': {
|
|
21
|
+
'editor':'default',
|
|
22
|
+
'ext':'.py'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
answers = prompt(questions, style=custom_style_2)
|
|
28
|
+
pprint(answers)
|
examples/expand.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
* example for expand question type
|
|
4
|
+
* run example by typing `python example/checkbox.py` in your console
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import print_function, unicode_literals
|
|
7
|
+
|
|
8
|
+
from PyInquirer import style_from_dict, Token, prompt, print_json, Separator
|
|
9
|
+
|
|
10
|
+
from examples import custom_style_2
|
|
11
|
+
|
|
12
|
+
# questions -
|
|
13
|
+
questions = [
|
|
14
|
+
{
|
|
15
|
+
'type': 'expand',
|
|
16
|
+
'message': 'Conflict on `file.js`: ',
|
|
17
|
+
'name': 'overwrite',
|
|
18
|
+
'default': 'a',
|
|
19
|
+
'choices': [
|
|
20
|
+
{
|
|
21
|
+
'key': 'y',
|
|
22
|
+
'name': 'Overwrite',
|
|
23
|
+
'value': 'overwrite'
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
'key': 'a',
|
|
27
|
+
'name': 'Overwrite this one and all next',
|
|
28
|
+
'value': 'overwrite_all'
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
'key': 'd',
|
|
32
|
+
'name': 'Show diff',
|
|
33
|
+
'value': 'diff'
|
|
34
|
+
},
|
|
35
|
+
Separator(),
|
|
36
|
+
{
|
|
37
|
+
'key': 'x',
|
|
38
|
+
'name': 'Abort',
|
|
39
|
+
'value': 'abort'
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
answers = prompt(questions, style=custom_style_2)
|
|
46
|
+
print_json(answers)
|
examples/hierarchical.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
hierarchical prompt usage example
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import print_function, unicode_literals
|
|
6
|
+
|
|
7
|
+
from PyInquirer import style_from_dict, Token, prompt
|
|
8
|
+
|
|
9
|
+
from examples import custom_style_2
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def ask_direction():
|
|
13
|
+
directions_prompt = {
|
|
14
|
+
'type': 'list',
|
|
15
|
+
'name': 'direction',
|
|
16
|
+
'message': 'Which direction would you like to go?',
|
|
17
|
+
'choices': ['Forward', 'Right', 'Left', 'Back']
|
|
18
|
+
}
|
|
19
|
+
answers = prompt(directions_prompt)
|
|
20
|
+
return answers['direction']
|
|
21
|
+
|
|
22
|
+
# TODO better to use while loop than recursion!
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def main():
|
|
26
|
+
print('You find yourself in a small room, there is a door in front of you.')
|
|
27
|
+
exit_house()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def exit_house():
|
|
31
|
+
direction = ask_direction()
|
|
32
|
+
if (direction == 'Forward'):
|
|
33
|
+
print('You find yourself in a forest')
|
|
34
|
+
print('There is a wolf in front of you; a friendly looking dwarf to the right and an impasse to the left.')
|
|
35
|
+
encounter1()
|
|
36
|
+
else:
|
|
37
|
+
print('You cannot go that way. Try again')
|
|
38
|
+
exit_house()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def encounter1():
|
|
42
|
+
direction = ask_direction()
|
|
43
|
+
if (direction == 'Forward'):
|
|
44
|
+
print('You attempt to fight the wolf')
|
|
45
|
+
print('Theres a stick and some stones lying around you could use as a weapon')
|
|
46
|
+
encounter2b()
|
|
47
|
+
elif (direction == 'Right'):
|
|
48
|
+
print('You befriend the dwarf')
|
|
49
|
+
print('He helps you kill the wolf. You can now move forward')
|
|
50
|
+
encounter2a()
|
|
51
|
+
else:
|
|
52
|
+
print('You cannot go that way')
|
|
53
|
+
encounter1()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def encounter2a():
|
|
57
|
+
direction = ask_direction()
|
|
58
|
+
if direction == 'Forward':
|
|
59
|
+
output = 'You find a painted wooden sign that says:'
|
|
60
|
+
output += ' \n'
|
|
61
|
+
output += ' ____ _____ ____ _____ \n'
|
|
62
|
+
output += '(_ _)( _ )( _ \\( _ ) \n'
|
|
63
|
+
output += ' )( )(_)( )(_) ))(_)( \n'
|
|
64
|
+
output += ' (__) (_____)(____/(_____) \n'
|
|
65
|
+
print(output)
|
|
66
|
+
else:
|
|
67
|
+
print('You cannot go that way')
|
|
68
|
+
encounter2a()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def encounter2b():
|
|
72
|
+
prompt({
|
|
73
|
+
'type': 'list',
|
|
74
|
+
'name': 'weapon',
|
|
75
|
+
'message': 'Pick one',
|
|
76
|
+
'choices': [
|
|
77
|
+
'Use the stick',
|
|
78
|
+
'Grab a large rock',
|
|
79
|
+
'Try and make a run for it',
|
|
80
|
+
'Attack the wolf unarmed'
|
|
81
|
+
]
|
|
82
|
+
}, style=custom_style_2)
|
|
83
|
+
print('The wolf mauls you. You die. The end.')
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if __name__ == '__main__':
|
|
87
|
+
main()
|
examples/input.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
* Input prompt example
|
|
4
|
+
* run example by writing `python example/input.py` in your console
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import print_function, unicode_literals
|
|
7
|
+
import re
|
|
8
|
+
from pprint import pprint
|
|
9
|
+
|
|
10
|
+
from PyInquirer import style_from_dict, Token, prompt
|
|
11
|
+
from PyInquirer import Validator, ValidationError
|
|
12
|
+
|
|
13
|
+
from examples import custom_style_2
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class PhoneNumberValidator(Validator):
|
|
17
|
+
def validate(self, document):
|
|
18
|
+
ok = re.match(r'^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$', document.text)
|
|
19
|
+
if not ok:
|
|
20
|
+
raise ValidationError(
|
|
21
|
+
message='Please enter a valid phone number',
|
|
22
|
+
cursor_position=len(document.text)) # Move cursor to end
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
questions = [
|
|
26
|
+
{
|
|
27
|
+
'type': 'input',
|
|
28
|
+
'name': 'first_name',
|
|
29
|
+
'message': 'What\'s your first name',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
'type': 'input',
|
|
33
|
+
'name': 'last_name',
|
|
34
|
+
'message': 'What\'s your last name',
|
|
35
|
+
'default': lambda answers: 'Smith' if answers['first_name'] == 'Dave' else 'Doe',
|
|
36
|
+
'validate': lambda val: val == 'Doe' or 'is your last name Doe?'
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
'type': 'input',
|
|
40
|
+
'name': 'phone',
|
|
41
|
+
'message': 'What\'s your phone number',
|
|
42
|
+
'validate': PhoneNumberValidator
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
answers = prompt(questions, style=custom_style_2)
|
|
47
|
+
pprint(answers)
|
examples/list.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
list prompt example
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import print_function, unicode_literals
|
|
6
|
+
|
|
7
|
+
from pprint import pprint
|
|
8
|
+
|
|
9
|
+
from PyInquirer import style_from_dict, Token, prompt, Separator
|
|
10
|
+
|
|
11
|
+
from examples import custom_style_2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_delivery_options(answers):
|
|
16
|
+
options = ['bike', 'car', 'truck']
|
|
17
|
+
if answers['size'] == 'jumbo':
|
|
18
|
+
options.append('helicopter')
|
|
19
|
+
return options
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
questions = [
|
|
23
|
+
{
|
|
24
|
+
'type': 'list',
|
|
25
|
+
'name': 'theme',
|
|
26
|
+
'message': 'What do you want to do?',
|
|
27
|
+
'choices': [
|
|
28
|
+
'Order a pizza',
|
|
29
|
+
'Make a reservation',
|
|
30
|
+
Separator(),
|
|
31
|
+
'Ask for opening hours',
|
|
32
|
+
{
|
|
33
|
+
'name': 'Contact support',
|
|
34
|
+
'disabled': 'Unavailable at this time'
|
|
35
|
+
},
|
|
36
|
+
'Talk to the receptionist'
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
'type': 'list',
|
|
41
|
+
'name': 'size',
|
|
42
|
+
'message': 'What size do you need?',
|
|
43
|
+
'choices': ['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
|
|
44
|
+
'filter': lambda val: val.lower()
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
'type': 'list',
|
|
48
|
+
'name': 'delivery',
|
|
49
|
+
'message': 'Which vehicle you want to use for delivery?',
|
|
50
|
+
'choices': get_delivery_options,
|
|
51
|
+
},
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
answers = prompt(questions, style=custom_style_2)
|
|
55
|
+
pprint(answers)
|
examples/password.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
* password question example
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import print_function, unicode_literals
|
|
6
|
+
|
|
7
|
+
from PyInquirer import style_from_dict, Token, prompt, print_json
|
|
8
|
+
|
|
9
|
+
from examples import custom_style_2
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
questions = [
|
|
13
|
+
{
|
|
14
|
+
'type': 'password',
|
|
15
|
+
'message': 'Enter your git password',
|
|
16
|
+
'name': 'password'
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
answers = prompt(questions, style=custom_style_2)
|
|
21
|
+
print_json(answers)
|