From 4b57f3e098e3581fa995069166dde084176c8d86 Mon Sep 17 00:00:00 2001 From: Eeo Jun <141bytes@gmail.com> Date: Wed, 9 Nov 2016 21:01:40 +0800 Subject: [PATCH 1/3] don't re-split arguments --- editor.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/editor.py b/editor.py index 422e106..3cf55f0 100755 --- a/editor.py +++ b/editor.py @@ -36,19 +36,19 @@ def get_default_editors(): def get_editor_args(editor): if editor in ['vim', 'gvim', 'vim.basic', 'vim.tiny']: - return '-f -o' + return ['-f', '-o'] elif editor == 'emacs': - return '-nw' + return ['-nw'] elif editor == 'gedit': - return '-w --new-window' + return ['-w', '--new-window'] elif editor == 'nano': - return '-R' + return ['-R'] else: - return '' + return [] def get_platform_editor_var(): @@ -80,8 +80,7 @@ def get_tty_filename(): def edit(filename=None, contents=None, use_tty=None): editor = get_editor() - args = get_editor_args(os.path.basename(os.path.realpath(editor))) - args = [editor] + args.split(' ') + args = [editor] + get_editor_args(os.path.basename(os.path.realpath(editor))) if use_tty is None: use_tty = sys.stdin.isatty() and not sys.stdout.isatty() From 5a32ae6180b181029283fd5d26d6f256d48d23ff Mon Sep 17 00:00:00 2001 From: Eeo Jun <141bytes@gmail.com> Date: Wed, 9 Nov 2016 21:07:15 +0800 Subject: [PATCH 2/3] remove un-needed function --- editor.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/editor.py b/editor.py index 3cf55f0..41ad538 100755 --- a/editor.py +++ b/editor.py @@ -51,11 +51,6 @@ def get_editor_args(editor): return [] -def get_platform_editor_var(): - # TODO: Make platform specific - return "$EDITOR" - - def get_editor(): # Get the editor from the environment. Prefer VISUAL to EDITOR editor = os.environ.get('VISUAL') or os.environ.get('EDITOR') @@ -69,7 +64,7 @@ def get_editor(): return path raise EditorError("Unable to find a viable editor on this system." - "Please consider setting your %s variable" % get_platform_editor_var()) + "Please consider setting your $EDITOR variable") def get_tty_filename(): From 9300eaa6937c8d12fcce936eabbf27e6377e149a Mon Sep 17 00:00:00 2001 From: Eeo Jun <141bytes@gmail.com> Date: Wed, 9 Nov 2016 21:14:32 +0800 Subject: [PATCH 3/3] update README --- README.md | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a8cf5f9..24076ab 100644 --- a/README.md +++ b/README.md @@ -6,21 +6,33 @@ Examples ```python import editor -commit_msg = editor.edit(contents="# Enter commit message here") +commit_msg = editor.edit(contents=b"# Enter commit message here") ``` + Opens an editor, prefilled with the contents, `# Enter commit message here`. -When the editor is closed, returns the contents in variable `commit_msg`. +When the editor is closed, returns the contents (bytes) in variable `commit_msg`. +Note that the argument to `contents` needs to be a bytes object on Python 3. ```python -import editor editor.edit(file="README.txt") ``` -Opens README.txt in an editor. Changes are saved in place. + +Opens README.txt in an editor. Changes are saved in place. If there is +a `contents` argument then the file contents will be overwritten. + +```python +editor.edit(..., use_tty=True) +``` + +Opens the editor in a TTY. This is usually done in programs which output is +piped to other programs. In this case the TTY is used as the editor's stdout, +allowing interactive usage. How it Works ------------ + `editor` first looks for the ${EDITOR} environment variable. If set, it uses the value as-is, without fallbacks. @@ -33,5 +45,5 @@ For example, on Linux, `editor` will look for the following editors in order: * emacs * nano -When calling the `edit()` function, `editor` will open the editor in a subprocess, -inheriting the parent process's stdin, stdout +When calling `editor.edit`, an editor will be opened in a subprocess, inheriting +the parent process's stdin, stdout.