♻️ refactor(clipboard, system): improve system plugin query performance and clipboard handling, invalidate cache on clipboard change, add command parsing to system plugin query

This commit is contained in:
sankalp committed 2025-06-22 14:41:14 +05:45
1 parent 26e6ed2858
commit 519f273d20
2 files changed
+133 -23

No files matched your search

+51 -10
View File
@@ -5,7 +5,8 @@ Provides system commands and actions.
import os
import time
from typing import List
import shlex
from typing import List, Union
from fabric.utils import exec_shell_command_async
import utils.icons as icons
@@ -55,27 +56,49 @@ class SystemPlugin(PluginBase):
def query(self, query_string: str) -> List[Result]:
"""Search for system commands matching the query."""
query = query_string.lower().strip()
query = query_string.strip()
if not query:
return []
results = []
# Parse the query to extract binary name and arguments
query_parts = query.split()
if not query_parts:
return []
binary_query = query_parts[0].lower()
full_command = query # Keep the original case and spacing
# Check system binaries
self._update_bin_cache()
for binary in self._bin_cache:
if query in binary.lower():
relevance = self._calculate_binary_relevance(binary, query)
if binary_query in binary.lower():
relevance = self._calculate_binary_relevance(binary, binary_query)
if relevance > 0:
# If the query starts with this binary, use the full command
if binary.lower() == binary_query:
# Exact binary match - use full command with arguments
display_command = full_command
command_to_execute = full_command
elif binary.lower().startswith(binary_query):
# Binary starts with query - suggest the binary name only
display_command = binary
command_to_execute = binary
else:
# Partial match - suggest the binary name only
display_command = binary
command_to_execute = binary
result = Result(
title=binary,
subtitle=f"Execute {binary}",
title=display_command,
subtitle=f"Execute: {display_command}",
icon_markup=icons.terminal,
action=lambda b=binary: self._execute_command([b]),
action=self._create_action(command_to_execute),
relevance=relevance,
plugin_name=self.display_name,
data={"command": [binary], "id": binary},
data={"command": command_to_execute, "id": binary},
)
results.append(result)
@@ -99,6 +122,24 @@ class SystemPlugin(PluginBase):
return 0.0
def _execute_command(self, command: List[str]):
def _create_action(self, command: Union[str, List[str]]):
"""Create an action function for the given command."""
def action():
self._execute_command(command)
return action
def _execute_command(self, command: Union[str, List[str]]):
"""Execute a system command."""
exec_shell_command_async(command)
try:
if isinstance(command, str):
# Handle string commands with arguments - split into list for proper execution
command_list = shlex.split(command)
exec_shell_command_async(command_list)
else:
# Handle list commands (backward compatibility)
exec_shell_command_async(command)
except Exception as e:
print(f"SystemPlugin: Error executing command '{command}': {e}")
import traceback
traceback.print_exc()