Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
20 changes: 7 additions & 13 deletions editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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()
Expand Down