Skip to content

Commit

Permalink
add input box
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatYYX committed Feb 19, 2019
1 parent 0d20e76 commit 7e53854
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions rltk/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys

SEPARATOR_LEN = 20


def prompt(text: str, *args, new_line: bool = True, **kwargs):
"""
Expand Down Expand Up @@ -33,15 +31,14 @@ def select(text: str, cases: list, default: int = None, case_sensitive: bool = F
Returns:
str: User's input.
"""
prompt('=' * SEPARATOR_LEN)
prompt(text)
case_text = []
for idx, c in enumerate(cases):
if default is not None and idx == default:
case_text.append('[{}]'.format(c[0]))
else:
case_text.append('{}'.format(c[0]))
prompt(' / '.join(case_text) + '\n')
prompt(' / '.join(case_text))
valid_cases = [c[1] for c in cases]
if default is not None:
valid_cases.append('')
Expand Down Expand Up @@ -143,3 +140,24 @@ def __exit__(self, exc_type, exc_val, exc_tb):


progress = Progress


def input_(text: str, type_: type = str):
"""
Input.
Args:
text (str): Text.
type_ (type, optional): Type of input value, defaults to `str`.
Returns:
object: User input in type `type_`.
"""
prompt(text)

while True:
user_input = input().strip()
try:
return type_(user_input)
except:
prompt('Invalid input, please retry')

0 comments on commit 7e53854

Please sign in to comment.