| 201 | self.loop.process_input([key]) |
| 202 | |
| 203 | async def running(self) -> None: |
| 204 | if not sys.stdout.isatty(): |
| 205 | print( |
| 206 | "Error: mitmproxy's console interface requires a tty. " |
| 207 | "Please run mitmproxy in an interactive shell environment.", |
| 208 | file=sys.stderr, |
| 209 | ) |
| 210 | sys.exit(1) |
| 211 | |
| 212 | detected_encoding = urwid.detected_encoding.lower() |
| 213 | if os.name != "nt" and detected_encoding and "utf" not in detected_encoding: |
| 214 | print( |
| 215 | f"mitmproxy expects a UTF-8 console environment, not {urwid.detected_encoding!r}. " |
| 216 | f"Set your LANG environment variable to something like en_US.UTF-8.", |
| 217 | file=sys.stderr, |
| 218 | ) |
| 219 | # Experimental (04/2022): We just don't exit here and see if/how that affects users. |
| 220 | # sys.exit(1) |
| 221 | urwid.set_encoding("utf8") |
| 222 | |
| 223 | signals.call_in.connect(self.sig_call_in) |
| 224 | self.ui = window.Screen() |
| 225 | self.ui.set_terminal_properties(256) |
| 226 | self.set_palette(None) |
| 227 | self.options.subscribe( |
| 228 | self.set_palette, ["console_palette", "console_palette_transparent"] |
| 229 | ) |
| 230 | |
| 231 | loop = asyncio.get_running_loop() |
| 232 | if isinstance(loop, getattr(asyncio, "ProactorEventLoop", tuple())): |
| 233 | # fix for https://bugs.python.org/issue37373 |
| 234 | loop = AddThreadSelectorEventLoop(loop) # type: ignore |
| 235 | self.loop = urwid.MainLoop( |
| 236 | urwid.SolidFill("x"), |
| 237 | event_loop=urwid.AsyncioEventLoop(loop=loop), |
| 238 | screen=self.ui, |
| 239 | handle_mouse=self.options.console_mouse, |
| 240 | ) |
| 241 | self.window = window.Window(self) |
| 242 | self.loop.widget = self.window |
| 243 | self.window.refresh() |
| 244 | |
| 245 | self.loop.start() |
| 246 | |
| 247 | await super().running() |
| 248 | |
| 249 | async def done(self): |
| 250 | self.loop.stop() |