-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: console_loggings.py / restart_in_safe_mode.py
Signed-off-by: Jack Cherng <[email protected]>
- Loading branch information
Showing
2 changed files
with
39 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import subprocess | ||
from collections.abc import Iterable | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
import sublime | ||
import sublime_plugin | ||
|
||
|
||
def _close_application(pid: int, post_close_cmd: Iterable[str | Path] | None = None, **kwargs: Any) -> None: | ||
if sublime.platform() == "windows": | ||
cmd = ["taskkill", "/f", "/pid", str(pid)] | ||
# do not create a window for the process | ||
startupinfo = subprocess.STARTUPINFO() # type: ignore | ||
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # type: ignore | ||
else: | ||
cmd = ["kill", "-9", str(pid)] | ||
startupinfo = None # type: ignore | ||
|
||
if post_close_cmd: | ||
cmd.append("&&") | ||
cmd.extend(map(str, post_close_cmd)) | ||
|
||
subprocess.call(cmd, shell=True, startupinfo=startupinfo, **kwargs) | ||
|
||
|
||
class RestartInSafeModeCommand(sublime_plugin.ApplicationCommand): | ||
def run(self) -> None: | ||
def run(self, *, restart: bool = True) -> None: | ||
if sublime.ok_cancel_dialog( | ||
"In order to restart in safe mode, the current Sublime Text has to be closed." | ||
+ " Be careful, unsaved changes may lose. Are you sure to continue?" | ||
): | ||
self.go_safe_mode() | ||
self.open_safe_mode(close_self=restart) | ||
|
||
@staticmethod | ||
def go_safe_mode() -> None: | ||
_close_application( | ||
os.getppid(), # plugin_host's parent is the Sublime Text process | ||
post_close_cmd=(sublime.executable_path(), "--safe-mode"), | ||
) | ||
def open_safe_mode(*, close_self: bool = True) -> None: | ||
if sublime.platform() == "windows": | ||
startupinfo = subprocess.STARTUPINFO() # type: ignore | ||
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # type: ignore | ||
else: | ||
startupinfo = None # type: ignore | ||
|
||
subprocess.Popen([sublime.executable_path(), "--safe-mode"], shell=True, startupinfo=startupinfo) | ||
|
||
if close_self: | ||
sublime.run_command("exit") |