Skip to content

Commit 0d466f2

Browse files
committed
body editing: better editor guessing, fix mitmproxy#4798
1 parent 82529d8 commit 0d466f2

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

mitmproxy/tools/console/master.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import os.path
66
import shlex
7+
import shutil
78
import signal
89
import stat
910
import subprocess
@@ -24,7 +25,6 @@
2425
from mitmproxy.addons import eventstore
2526
from mitmproxy.addons import readfile
2627
from mitmproxy.addons import view
27-
from mitmproxy.tools.console import common
2828
from mitmproxy.tools.console import consoleaddons
2929
from mitmproxy.tools.console import defaultkeys
3030
from mitmproxy.tools.console import keymap
@@ -116,13 +116,26 @@ def uistopped(self):
116116
self.loop.screen_size = None
117117
self.loop.draw_screen()
118118

119+
def get_editor(self) -> str:
120+
# based upon https://github.com/pallets/click/blob/main/src/click/_termui_impl.py
121+
if m := os.environ.get("MITMPROXY_EDITOR"):
122+
return m
123+
if m := os.environ.get("EDITOR"):
124+
return m
125+
for editor in "sensible-editor", "nano", "vim":
126+
if shutil.which(editor):
127+
return editor
128+
if os.name == "nt":
129+
return "notepad"
130+
else:
131+
return "vi"
132+
119133
def spawn_editor(self, data):
120134
text = not isinstance(data, bytes)
121-
fd, name = tempfile.mkstemp('', "mproxy", text=text)
135+
fd, name = tempfile.mkstemp('', "mitmproxy", text=text)
122136
with open(fd, "w" if text else "wb") as f:
123137
f.write(data)
124-
# if no EDITOR is set, assume 'vi'
125-
c = os.environ.get("MITMPROXY_EDITOR") or os.environ.get("EDITOR") or "vi"
138+
c = self.get_editor()
126139
cmd = shlex.split(c)
127140
cmd.append(name)
128141
with self.uistopped():
@@ -203,7 +216,7 @@ def run(self):
203216
["console_palette", "console_palette_transparent"]
204217
)
205218
loop = asyncio.get_event_loop()
206-
if common.IS_WINDOWS:
219+
if isinstance(loop, getattr(asyncio, "ProactorEventLoop", tuple())):
207220
# fix for https://bugs.python.org/issue37373
208221
loop = AddThreadSelectorEventLoop(loop)
209222
self.loop = urwid.MainLoop(

0 commit comments

Comments
 (0)