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. diff --git a/editor.py b/editor.py index 422e106..41ad538 100755 --- a/editor.py +++ b/editor.py @@ -36,24 +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 '' - - -def get_platform_editor_var(): - # TODO: Make platform specific - return "$EDITOR" + return [] def 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(): @@ -80,8 +75,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()