Skip to content

Commit

Permalink
Merge pull request #15 from lelit/master
Browse files Browse the repository at this point in the history
Fix issue #14
magmax committed Apr 25, 2016
2 parents 9252639 + a7b73e1 commit 34a3583
Showing 4 changed files with 88 additions and 28 deletions.
22 changes: 22 additions & 0 deletions examples/pre_answers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from pprint import pprint

import inquirer

if __name__ == '__main__':

questions = [
inquirer.Text('user', message='Please enter your github username', validate=lambda _, x: x != '.'),
inquirer.Password('password', message='Please enter your password'),
inquirer.Text('repo', message='Please enter the repo name', default='default'),
inquirer.Checkbox('topics', message='Please define your type of project?', choices=['common', 'backend', 'frontend'], ),
inquirer.Text('organization', message='If this is a repo from a organization please enter the organization name, if not just leave this blank'),
inquirer.Confirm('correct', message='This will delete all your current labels and create a new ones. Continue?', default=False),
]

answers = inquirer.prompt(questions)

# Ask again, using previous values as defaults

answers = inquirer.prompt(questions, answers=answers)

pprint(answers)
2 changes: 1 addition & 1 deletion inquirer/questions.py
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ def message(self):

@property
def default(self):
return self._solve(self._default)
return self.answers.get(self.name) or self._solve(self._default)

@property
def choices_generator(self):
29 changes: 2 additions & 27 deletions inquirer/render/console/_password.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
# -*- coding: utf-8 -*-

from readchar import key
from inquirer import errors
from .base import BaseConsoleRender
from ._text import Text


class Password(BaseConsoleRender):
title_inline = True

def __init__(self, *args, **kwargs):
super(Password, self).__init__(*args, **kwargs)
self.current = ''

class Password(Text):
def get_current_value(self):
return '*' * len(self.current)

def process_input(self, pressed):
if pressed == key.CTRL_C:
raise KeyboardInterrupt()

if pressed == key.ENTER:
raise errors.EndOfInput(self.current)

if pressed == key.BACKSPACE:
if len(self.current):
self.current = self.current[:-1]
return

if len(pressed) != 1:
return

self.current += pressed
63 changes: 63 additions & 0 deletions tests/acceptance/test_pre_answers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest
import pexpect
from readchar import key


expected_result = r"""\
{'correct': True,\r\n
'organization': '',\r\n
'password': 'edcba',\r\n
'repo': 'default',\r\n
'topics': \['common'\],\r\n
'user': 'abcde'}\r\n"""


class PreAnswersTest(unittest.TestCase):
def setUp(self):
self.sut = pexpect.spawn('python examples/pre_answers.py')

def test_minimal_input(self):
# user
self.sut.expect("Please enter", timeout=1)
self.sut.send('abcde')
self.sut.send(key.ENTER)
# password
self.sut.expect("Please enter", timeout=1)
self.sut.send('edcba')
self.sut.send(key.ENTER)
# repo
self.sut.expect("Please enter", timeout=1)
self.sut.send(key.ENTER)
# topics
self.sut.expect("Please define", timeout=1)
self.sut.send(key.SPACE)
self.sut.send(key.ENTER)
# organization
self.sut.expect("If this is", timeout=1)
self.sut.send(key.ENTER)
# correct
self.sut.expect("This will delete", timeout=1)
self.sut.send('y')

# again

# user
self.sut.expect("Please enter", timeout=1)
self.sut.send(key.ENTER)
# password
self.sut.expect("Please enter", timeout=1)
self.sut.send(key.ENTER)
# repo
self.sut.expect("Please enter", timeout=1)
self.sut.send(key.ENTER)
# topics
self.sut.expect("Please define", timeout=1)
self.sut.send(key.ENTER)
# organization
self.sut.expect("If this is", timeout=1)
self.sut.send(key.ENTER)
# correct
self.sut.expect("This will delete", timeout=1)
self.sut.send(key.ENTER)

self.sut.expect([expected_result, pexpect.EOF], timeout=1)

0 comments on commit 34a3583

Please sign in to comment.