2018-10-27 18:58:52 +05:30
|
|
|
import os
|
|
|
|
|
import tempfile
|
2018-11-15 15:41:01 +05:30
|
|
|
|
2018-11-12 14:06:45 +05:30
|
|
|
from core.config import defaultEditor
|
2018-10-27 18:58:52 +05:30
|
|
|
|
2018-11-16 21:13:45 +05:30
|
|
|
|
2018-10-27 18:58:52 +05:30
|
|
|
def prompt(default=None):
|
2018-11-16 21:13:45 +05:30
|
|
|
# try assigning default editor, if fails, use default
|
2018-11-12 14:06:45 +05:30
|
|
|
editor = os.environ.get('EDITOR', defaultEditor)
|
2018-11-16 21:13:45 +05:30
|
|
|
# create a temporary file and open it
|
2018-10-27 18:58:52 +05:30
|
|
|
with tempfile.NamedTemporaryFile(mode='r+') as tmpfile:
|
2018-11-16 21:13:45 +05:30
|
|
|
if default: # if prompt should have some predefined text
|
2018-10-27 18:58:52 +05:30
|
|
|
tmpfile.write(default)
|
|
|
|
|
tmpfile.flush()
|
|
|
|
|
child_pid = os.fork()
|
|
|
|
|
is_child = child_pid == 0
|
|
|
|
|
|
|
|
|
|
if is_child:
|
2018-11-16 21:13:45 +05:30
|
|
|
# opens the file in the editor
|
2018-10-27 18:58:52 +05:30
|
|
|
os.execvp(editor, [editor, tmpfile.name])
|
|
|
|
|
else:
|
2018-11-16 21:13:45 +05:30
|
|
|
os.waitpid(child_pid, 0) # wait till the editor gets closed
|
2018-10-27 18:58:52 +05:30
|
|
|
tmpfile.seek(0)
|
2018-11-16 21:13:45 +05:30
|
|
|
return tmpfile.read().strip() # read the file
|