فهرست منبع

Merge branch 'new-widget'

- Expand
- Phone number
DricomDragon 5 سال پیش
والد
کامیت
f50c2d7b05
2فایلهای تغییر یافته به همراه74 افزوده شده و 0 حذف شده
  1. 46 0
      expand.py
  2. 28 0
      phone_number.py

+ 46 - 0
expand.py

@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+"""
+* example for expand question type
+* run example by typing `python example/checkbox.py` in your console
+"""
+from __future__ import print_function, unicode_literals
+
+from PyInquirer import style_from_dict, Token, prompt, print_json, Separator
+
+from examples import custom_style_2
+
+# questions - 
+questions = [
+    {
+        'type': 'expand',
+        'message': 'Conflict on `file.js`: ',
+        'name': 'overwrite',
+        'default': 'a',
+        'choices': [
+            {
+                'key': 'y',
+                'name': 'Overwrite',
+                'value': 'overwrite'
+            },
+            {
+                'key': 'a',
+                'name': 'Overwrite this one and all next',
+                'value': 'overwrite_all'
+            },
+            {
+                'key': 'd',
+                'name': 'Show diff',
+                'value': 'diff'
+            },
+            Separator(),
+            {
+                'key': 'x',
+                'name': 'Abort',
+                'value': 'abort'
+            }
+        ]
+    }
+]
+
+answers = prompt(questions, style=custom_style_2)
+print(answers)

+ 28 - 0
phone_number.py

@@ -0,0 +1,28 @@
+from PyInquirer import prompt, Validator, ValidationError
+from prompt_toolkit import document
+import regex
+
+class PhoneValidator(Validator):
+	def validate(self, document: document.Document) -> None:
+		ok = regex.match('^\+?\d[\d ]+\d$', document.text)
+		if not ok:
+			raise ValidationError(message = 'Please enter a valid phone number', cursor_position = len(document.text))
+
+widget = [
+	{
+		'type':'input',
+		'name':'number',
+		'message':'Type your phone number',
+		'validate':PhoneValidator
+	}
+]
+
+try :
+	result = prompt(widget)
+except ValueError :
+	print('Pb !!!')
+	exit()
+
+print('Your answer is')
+print(result["number"])
+print(type(result["number"]))