Skip to content

Commit 9186bd7

Browse files
authored
Merge pull request fmoo#12 from eugene-eeo/master
Clean up code, improve README
2 parents 38d866f + 9300eaa commit 9186bd7

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,33 @@ Examples
66

77
```python
88
import editor
9-
commit_msg = editor.edit(contents="# Enter commit message here")
9+
commit_msg = editor.edit(contents=b"# Enter commit message here")
1010
```
11+
1112
Opens an editor, prefilled with the contents, `# Enter commit message here`.
12-
When the editor is closed, returns the contents in variable `commit_msg`.
13+
When the editor is closed, returns the contents (bytes) in variable `commit_msg`.
14+
Note that the argument to `contents` needs to be a bytes object on Python 3.
1315

1416

1517
```python
16-
import editor
1718
editor.edit(file="README.txt")
1819
```
19-
Opens README.txt in an editor. Changes are saved in place.
20+
21+
Opens README.txt in an editor. Changes are saved in place. If there is
22+
a `contents` argument then the file contents will be overwritten.
23+
24+
```python
25+
editor.edit(..., use_tty=True)
26+
```
27+
28+
Opens the editor in a TTY. This is usually done in programs which output is
29+
piped to other programs. In this case the TTY is used as the editor's stdout,
30+
allowing interactive usage.
2031

2132

2233
How it Works
2334
------------
35+
2436
`editor` first looks for the ${EDITOR} environment variable. If set, it uses
2537
the value as-is, without fallbacks.
2638

@@ -33,5 +45,5 @@ For example, on Linux, `editor` will look for the following editors in order:
3345
* emacs
3446
* nano
3547

36-
When calling the `edit()` function, `editor` will open the editor in a subprocess,
37-
inheriting the parent process's stdin, stdout
48+
When calling `editor.edit`, an editor will be opened in a subprocess, inheriting
49+
the parent process's stdin, stdout.

editor.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,19 @@ def get_default_editors():
3636

3737
def get_editor_args(editor):
3838
if editor in ['vim', 'gvim', 'vim.basic', 'vim.tiny']:
39-
return '-f -o'
39+
return ['-f', '-o']
4040

4141
elif editor == 'emacs':
42-
return '-nw'
42+
return ['-nw']
4343

4444
elif editor == 'gedit':
45-
return '-w --new-window'
45+
return ['-w', '--new-window']
4646

4747
elif editor == 'nano':
48-
return '-R'
48+
return ['-R']
4949

5050
else:
51-
return ''
52-
53-
54-
def get_platform_editor_var():
55-
# TODO: Make platform specific
56-
return "$EDITOR"
51+
return []
5752

5853

5954
def get_editor():
@@ -69,7 +64,7 @@ def get_editor():
6964
return path
7065

7166
raise EditorError("Unable to find a viable editor on this system."
72-
"Please consider setting your %s variable" % get_platform_editor_var())
67+
"Please consider setting your $EDITOR variable")
7368

7469

7570
def get_tty_filename():
@@ -80,8 +75,7 @@ def get_tty_filename():
8075

8176
def edit(filename=None, contents=None, use_tty=None):
8277
editor = get_editor()
83-
args = get_editor_args(os.path.basename(os.path.realpath(editor)))
84-
args = [editor] + args.split(' ')
78+
args = [editor] + get_editor_args(os.path.basename(os.path.realpath(editor)))
8579

8680
if use_tty is None:
8781
use_tty = sys.stdin.isatty() and not sys.stdout.isatty()

0 commit comments

Comments
 (0)