Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions condense/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ def make_parser(argv):
else argparse.SUPPRESS
),
)

if sys.platform == "win32":
advanced_group.add_argument(
"--install-right-click-menu",
action=condense.utils.InstallContextMenu,
help=(
"install Condense to the right-click context menu for Windows Explorer and exit"
if show_all_options
else argparse.SUPPRESS
),
)

advanced_group.add_argument(
"--uninstall-right-click-menu",
action=condense.utils.UninstallContextMenu,
help=(
"uninstall Condense from the right-click context menu for Windows Explorer and exit"
if show_all_options
else argparse.SUPPRESS
),
)

output_group = parser.add_argument_group("rendering arguments")
output_group.add_argument(
Expand Down
57 changes: 57 additions & 0 deletions condense/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,63 @@ def __call__(self, parser, namespace, values, option_string=None):
items.extend(values)
setattr(namespace, self.dest, items)


class InstallContextMenu(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
super(InstallContextMenu, self).__init__(option_strings, dest, nargs=0, **kwargs)

def __call__(self, parser, namespace, values, option_string=None):
# Theoretically, we don't need to check the platform again here,
# because non-Windows platforms will not accept the --install-right-click-menu parameter at all.
# This judgment is just to make the mypy type check pass.
# The same logic applies to `UninstallContextMenu` below.
if sys.platform == "win32":
import winreg as reg

menu_name = "Open with Condense"
icon_path = None

if getattr(sys, "frozen", False):
# If this is a standalone condense.exe, the path to the condense is sys.executable
menu_command = f'C:\\windows\\system32\\cmd.exe /K "^"{sys.executable}^" ^"%1^""'
icon_path = sys.executable
else:
menu_command = f'C:\\windows\\system32\\cmd.exe /K "python -m condense ^"%1^""'

# Create `shell` if it does not exist
try:
shell_key = reg.OpenKey(reg.HKEY_CURRENT_USER, r"Software\\Classes\\*\\shell", 0, reg.KEY_SET_VALUE)
except FileNotFoundError:
shell_key = reg.CreateKey(reg.HKEY_CURRENT_USER, r"Software\\Classes\\*\\shell")
shell_key = reg.OpenKey(reg.HKEY_CURRENT_USER, r"Software\\Classes\\*\\shell", 0, reg.KEY_SET_VALUE)

reg.SetValue(shell_key, menu_name, reg.REG_SZ, menu_name)

menu_key = reg.OpenKey(shell_key, menu_name, 0, reg.KEY_SET_VALUE)
if icon_path:
reg.SetValueEx(menu_key, "Icon", 0, reg.REG_SZ, icon_path)
reg.SetValue(menu_key, "command", reg.REG_SZ, menu_command)
sys.exit(0)


class UninstallContextMenu(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
super(UninstallContextMenu, self).__init__(option_strings, dest, nargs=0, **kwargs)

def __call__(self, parser, namespace, values, option_string=None):
if sys.platform == "win32":
import winreg as reg

menu_name = "Open with Condense"

shell_key = reg.OpenKey(reg.HKEY_CURRENT_USER, r"Software\\Classes\\*\\shell")
menu_key = reg.OpenKey(shell_key, menu_name)

reg.DeleteKey(menu_key, "command")
reg.DeleteKey(shell_key, menu_name)
sys.exit(0)


def get_progress_bar(functions, disable_progress, desc="", unit=""):
pbar = tqdm.tqdm
if disable_progress:
Expand Down