Skip to content

Commit 59b6ed9

Browse files
authored
Merge pull request #25 from CITGuru/dev
Dev
2 parents b113d2f + d68585d commit 59b6ed9

File tree

7 files changed

+107
-13
lines changed

7 files changed

+107
-13
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"python.pythonPath": "/usr/bin/python"
2+
"python.pythonPath": "/home/oyetoke/.virtualenvs/PyInquirer/bin/python"
33
}

PyInquirer/prompt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def prompt(questions, answers=None, **kwargs):
6363

6464
if callable(question.get('default')):
6565
_kwargs['default'] = question['default'](answers)
66-
66+
6767
application = getattr(prompts, type).question(message, **_kwargs)
6868

6969
answer = run_application(

PyInquirer/prompts/editor.py

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
`input` type question
3+
`editor` type question
44
"""
55
from __future__ import print_function, unicode_literals
66
import os
@@ -12,21 +12,24 @@
1212

1313
from .common import default_style
1414

15+
1516
# use std prompt-toolkit control
1617

1718
WIN = sys.platform.startswith('win')
1819

20+
class EditorArgumentsError(Exception):
21+
pass
22+
1923
class Editor(object):
2024

21-
def __init__(self, editor=None, env=None, require_save=True,
22-
extension='.txt'):
25+
def __init__(self, editor=None, env=None, require_save=True, extension='.txt'):
2326
self.editor = editor
2427
self.env = env
2528
self.require_save = require_save
2629
self.extension = extension
2730

2831
def get_editor(self):
29-
if self.editor is not None:
32+
if self.editor is not None and self.editor.lower() != "default":
3033
return self.editor
3134
for key in 'VISUAL', 'EDITOR':
3235
rv = os.environ.get(key)
@@ -92,10 +95,45 @@ def edit(self, text):
9295
finally:
9396
os.unlink(name)
9497

95-
98+
def edit(text=None, editor=None, env=None, require_save=True,
99+
extension='.txt', filename=None):
100+
r"""Edits the given text in the defined editor. If an editor is given
101+
(should be the full path to the executable but the regular operating
102+
system search path is used for finding the executable) it overrides
103+
the detected editor. Optionally, some environment variables can be
104+
used. If the editor is closed without changes, `None` is returned. In
105+
case a file is edited directly the return value is always `None` and
106+
`require_save` and `extension` are ignored.
107+
108+
If the editor cannot be opened a :exc:`UsageError` is raised.
109+
110+
Note for Windows: to simplify cross-platform usage, the newlines are
111+
automatically converted from POSIX to Windows and vice versa. As such,
112+
the message here will have ``\n`` as newline markers.
113+
114+
:param text: the text to edit.
115+
:param editor: optionally the editor to use. Defaults to automatic
116+
detection.
117+
:param env: environment variables to forward to the editor.
118+
:param require_save: if this is true, then not saving in the editor
119+
will make the return value become `None`.
120+
:param extension: the extension to tell the editor about. This defaults
121+
to `.txt` but changing this might change syntax
122+
highlighting.
123+
:param filename: if provided it will edit this file instead of the
124+
provided text contents. It will not use a temporary
125+
file as an indirection in that case.
126+
"""
127+
128+
editor = Editor(editor=editor, env=env, require_save=require_save,
129+
extension=extension)
130+
if filename is None:
131+
return editor.edit(text)
132+
editor.edit_file(filename)
96133

97134
def question(message, **kwargs):
98135
default = kwargs.pop('default', '')
136+
eargs = kwargs.pop('eargs', {})
99137
validate_prompt = kwargs.pop('validate', None)
100138
if validate_prompt:
101139
if issubclass(validate_prompt, Validator):
@@ -112,11 +150,38 @@ def validate(self, document):
112150
cursor_position=len(document.text))
113151
kwargs['validator'] = _InputValidator()
114152

153+
for k, v in eargs.items():
154+
if v == "" or v == " ":
155+
raise EditorArgumentsError(
156+
"Args '{}' value should not be empty".format(k)
157+
)
158+
159+
editor = eargs.get("editor", None)
160+
ext = eargs.get("ext", ".txt")
161+
env = eargs.get("env", None)
162+
text = default
163+
filename = eargs.get("filename", None)
164+
multiline = True if not editor else False
165+
save = eargs.get("save", None)
166+
167+
if editor:
168+
_text = edit(
169+
editor=editor,
170+
extension=ext,
171+
text=text,
172+
env=env,
173+
filename=filename,
174+
require_save=save
175+
)
176+
if filename:
177+
default = filename
178+
else:
179+
default = _text
180+
115181
# TODO style defaults on detail level
116182
kwargs['style'] = kwargs.pop('style', default_style)
117183
qmark = kwargs.pop('qmark', '?')
118-
119-
184+
120185
def _get_prompt_tokens(cli):
121186
return [
122187
(Token.QuestionMark, qmark),
@@ -127,5 +192,6 @@ def _get_prompt_tokens(cli):
127192
get_prompt_tokens=_get_prompt_tokens,
128193
lexer=SimpleLexer(Token.Answer),
129194
default=default,
195+
multiline=multiline,
130196
**kwargs
131197
)

README.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,32 @@ Editor - ``{type: 'editor'}``
235235
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
236236

237237
Take ``type``, ``name``, ``message``\ [, ``default``, ``filter``,
238-
``validate``] properties
238+
``validate``, ``eargs``] properties
239+
240+
### Editor Arguments - ``eargs``
241+
242+
Opens an empty or edits the default text in the defined editor. If an editor is given
243+
(should be the full path to the executable but the regular operating
244+
system search path is used for finding the executable) it overrides
245+
the detected editor. Optionally, some environment variables can be
246+
used. If the editor is closed without changes, ``None`` is returned. In
247+
case a file is edited directly the return value is always ``None`` and
248+
``save`` and ``ext`` are ignored.
249+
250+
Takes:
251+
252+
- editor: accepts ``default`` to get the default platform editor. But
253+
you can also provide the path to an editor e.g ``vi``.
254+
- ext: the extension to tell the editor about. This defaults to `.txt`
255+
but changing this might change syntax highlighting e.g ".py"
256+
- save: accepts ``True`` or ``False`` to determine to save a file.
257+
- filename: accepts the path of a file you'd like to edit.
258+
- env: accepts any given environment variables to pass to the editor
239259

240260
Launches an instance of the users preferred editor on a temporary file.
241261
Once the user exits their editor, the contents of the temporary file are
242262
read in as the result. The editor to use is determined by reading the
243-
:math:`VISUAL or `\ EDITOR environment variables. If neither of those
263+
:math:``VISUAL or ``\ EDITOR environment variables. If neither of those
244264
are present, notepad (on Windows) or vim (Linux or Mac) is used.
245265

246266
Question Properties

examples/editor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
'type': 'editor',
1515
'name': 'bio',
1616
'message': 'Please write a short bio of at least 3 lines.',
17-
# 'validate': lambda text: len(text.split('\n')) >= 3 or 'Must be at least 3 lines.'
17+
'default': 'Hello World',
18+
'validate': lambda text: len(text.split('\n')) >= 3 or 'Must be at least 3 lines.',
19+
'eargs': {
20+
'editor':'default',
21+
'ext':'.py'
22+
}
1823
}
1924
]
2025

examples/expand.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from examples import custom_style_2
1111

12-
12+
# questions -
1313
questions = [
1414
{
1515
'type': 'expand',

requirements_dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# this file contains additional dependencies (not in requirements.txt)
22
# please list all dependencies necessary to test and build this package
33

4+
-r requirements.txt
5+
46
# test-tools
57
testfixtures
68
pytest>=3.0.7
@@ -14,3 +16,4 @@ ptyprocess==0.5.1
1416
pep8>=1.7.0
1517
bumpversion>=0.5.3
1618
pypandoc>=1.3.3
19+

0 commit comments

Comments
 (0)