Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use notepad++ by default on Windows (#2051)

NTFSvolumedd13f402c21d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 38fd5343f..22cb2d47c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -33,7 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed twitter.com and x.com from blocked domains
- Updated primary URL to pixhost.cc (Pixhost)
-- Always include album id in folder name when a gallery's title is "Untitled Gallery" (Pixhost)
+- Always include album id in folder name when a gallery's title is `Untitled Gallery` (Pixhost)
+- On Windows, use Notepad++ as default text editor (if available) when `$EDITOR` is not set
### Removed
@@ -45,7 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
This affected sites where multiple files may share identical URL path and query, with the fragment being what uniquely identifies each file (Mega.nz, Koofr, Transfer.it)
- Download URL extraction (BuzzHeavier)
- Download of files with no hash information (Filester)
-- Parsing of $EDITOR for paths with spaces
+- Parsing of `$EDITOR` for paths with spaces
## [10.2.1] - 2026-07-21
diff --git a/cyberdrop_dl/utils/text_editor.py b/cyberdrop_dl/utils/text_editor.py
index 2d5a91a3a..dabd2dfab 100644
--- a/cyberdrop_dl/utils/text_editor.py
+++ b/cyberdrop_dl/utils/text_editor.py
@@ -6,71 +6,96 @@ import os
import shutil
import subprocess
import sys
-from pathlib import Path
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, final
if TYPE_CHECKING:
from collections.abc import Sequence
+ from pathlib import Path
+
+type CMD = Sequence[str]
+
+# Ordered by preference
+_UNIX_TEXT_EDITORS: tuple[CMD, ...] = (
+ ("micro", "-keymenu", "true"),
+ ("nano",),
+ ("vim",),
+)
+
+
+@final
+class OSDefaultCMD(str):
+ __slots__ = ()
-_TEXT_EDITORS = "micro", "nano", "vim" # Ordered by preference
-_SSH = "SSH_CONNECTION" in os.environ
-_HAS_DESKTOP_ENVIROMENT = any(var in os.environ for var in ("DISPLAY", "WAYLAND_DISPLAY"))
-_CUSTOM_EDITOR = os.environ.get("EDITOR")
-_YAML_MIMETYPE = "application/yaml"
-_TEXT_MIMETYPE = "text/plain"
logger = logging.getLogger(__name__)
def open(file_path: Path) -> None: # noqa: A001
"""Opens file in the OS's text editor."""
-
- cmd = _editor_cmd()
+ cmd = editor_cmd()
if not cmd:
msg = "No default text editor found"
raise ValueError(msg)
cmd = *cmd, file_path
- bin_path, *rest = cmd
- if Path(bin_path).stem == "micro":
- cmd = bin_path, "-keymenu", "true", *rest
-
- logger.info(f"Opening '{file_path}' with '{bin_path}'...")
+ bin_path = cmd[0]
+ msg = f"the OS's default editor ('{bin_path}')" if type(bin_path) is OSDefaultCMD else f"'{bin_path}'"
+ logger.info(f"Opening '{file_path}' with {msg}...")
_ = subprocess.call(cmd, stderr=subprocess.DEVNULL)
@functools.cache
-def _editor_cmd() -> Sequence[str] | None:
- import shlex
+def editor_cmd() -> CMD | None:
+ return _editor_cmd()
- if _CUSTOM_EDITOR:
- if shutil.which(_CUSTOM_EDITOR):
- return (_CUSTOM_EDITOR,)
- cmd = shlex.split(_CUSTOM_EDITOR)
- if cmd and shutil.which(cmd[0]):
+def _editor_cmd() -> CMD | None:
+ if editor := os.environ.get("EDITOR"):
+ if cmd := shutil.which(editor):
return cmd
- msg = f"Editor '{_CUSTOM_EDITOR}' from env bar $EDITOR is not available. Ignoring"
+ import shlex
+
+ cmd, *args = shlex.split(editor)
+ if cmd and (bin_path := shutil.which(cmd[0])):
+ return bin_path, *args
+
+ msg = f"Editor '{editor}' from env var $EDITOR is not available. Ignoring"
logger.warning(msg)
if sys.platform == "darwin":
- return "open", "-t", "-n", "-W"
+ return OSDefaultCMD("open"), "-t", "-n", "-W"
if sys.platform == "win32":
- return ("notepad.exe",)
+ return _find_win_editor()
+
+ return _find_unix_editor()
+
+
+def _find_win_editor() -> CMD | None:
+ for path in map(
+ os.path.expandvars,
+ [
+ "%PROGRAMFILES%/Notepad++/notepad++.exe",
+ "%PROGRAMFILES(X86)%/Notepad++/notepad++.exe",
+ "notepad++.exe",
+ ],
+ ):
+ if notepad_pp := shutil.which(path):
+ return notepad_pp, "-multiInst", "-noPlugin", "-notabbar", "-nosession"
- if _HAS_DESKTOP_ENVIROMENT and not _SSH and _set_xdg_yaml_default_if_none():
- return ("xdg-open",)
+ if notepad := shutil.which("notepad.exe"):
+ return (notepad,)
- if fallback_editor := _find_text_editor():
- return (fallback_editor,)
+def _find_unix_editor() -> CMD | None:
+ has_desktop_enviroment = any(var in os.environ for var in ("DISPLAY", "WAYLAND_DISPLAY"))
+ if has_desktop_enviroment and "SSH_CONNECTION" not in os.environ and _set_xdg_yaml_default_if_none():
+ return (OSDefaultCMD("xdg-open"),)
-def _find_text_editor() -> str | None:
- for editor in _TEXT_EDITORS:
- if bin_path := shutil.which(editor):
- return bin_path
+ for bin_path, *args in _UNIX_TEXT_EDITORS:
+ if full_path := shutil.which(bin_path):
+ return full_path, *args
def _set_xdg_yaml_default_if_none() -> bool:
@@ -79,17 +104,20 @@ def _set_xdg_yaml_default_if_none() -> bool:
Returns `True` if a default app is now associated, `False` if setting the default failed
"""
+ yaml_mime = "application/yaml"
+ text_mime = "text/plain"
- def xdg_query_default(mimetype: str) -> str:
- cmd = "xdg-mime", "query", "default", mimetype
- process = subprocess.run(cmd, capture_output=True, text=True, check=False)
- return process.stdout.strip()
-
- if xdg_query_default(_YAML_MIMETYPE):
+ if _xdg_query_default(yaml_mime):
return True
- default_text_app = xdg_query_default(_TEXT_MIMETYPE)
+ default_text_app = _xdg_query_default(text_mime)
if not default_text_app:
return False
- return subprocess.call(["xdg-mime", "default", default_text_app, _YAML_MIMETYPE]) == 0
+ return subprocess.call(["xdg-mime", "default", default_text_app, yaml_mime]) == 0
+
+
+def _xdg_query_default(mimetype: str) -> str:
+ cmd = "xdg-mime", "query", "default", mimetype
+ process = subprocess.run(cmd, capture_output=True, text=True, check=False)
+ return process.stdout.strip()
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 2c4bbaf80..727d6cd23 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import platform
+import sys
from pathlib import Path
from typing import Any
@@ -9,6 +10,7 @@ import pytest
from cyberdrop_dl.exceptions import InvalidExtensionError, NoExtensionError
from cyberdrop_dl.filepath import get_filename_and_ext
from cyberdrop_dl.url_objects import AbsoluteHttpURL
+from cyberdrop_dl.utils import text_editor
from cyberdrop_dl.utils._url import fix_multi_slashes, parse_http_url
@@ -148,3 +150,34 @@ def test_fix_multi_slashes(url: str, expected: str) -> None:
def test_parse_http(url: str, origin: str | None, expected: str, *, trim: bool) -> None:
result = parse_http_url(url, AbsoluteHttpURL(origin) if origin else None, trim=trim)
assert result.human_repr() == expected
+
+
+class TestTextEditor:
+ @pytest.mark.skipif(sys.platform != "win32", reason="Windows only test")
+ def test_win_default(self, tmp_cwd: Path) -> None:
+ cmd = text_editor._find_win_editor()
+ assert cmd == text_editor._editor_cmd()
+ assert cmd == ("C:\\Windows\\system32\\notepad.exe",)
+ np = tmp_cwd / "notepad++.exe"
+ np.write_text("test")
+ cmd = text_editor._find_win_editor()
+ assert cmd
+ assert cmd[1:] == (
+ "-multiInst",
+ "-noPlugin",
+ "-notabbar",
+ "-nosession",
+ )
+
+ @pytest.mark.skipif(sys.platform != "darwin", reason="MAC OS test only")
+ def test_mac_os_default(self) -> None:
+ cmd = text_editor._editor_cmd()
+ assert cmd
+ assert cmd == ("open", "-t", "-n", "-W")
+ assert type(cmd[0]) is text_editor.OSDefaultCMD
+
+ @pytest.mark.skipif(sys.platform in ("darwin", "win32"), reason="Linux test only")
+ def test_unix_default(self) -> None:
+ cmd = text_editor._find_unix_editor()
+ assert cmd
+ assert cmd == text_editor._editor_cmd()