This commit is contained in:
sankalp committed 2024-11-05 14:09:20 +05:45
1 parent d3278f7c92
commit 50211bb1e6
12 files changed
+335 -737

No files matched your search

+62 -28
View File
@@ -1,44 +1,78 @@
import os
import subprocess
from fabric.widgets.button import Button
from fabric.widgets.label import Label
from services.sh import Sh
CACHE_DIR = os.getenv(
"XDG_CACHE_HOME", os.path.join(os.path.expanduser("~"), ".cache", "fabric")
)
BINS_PATH = os.path.join(CACHE_DIR, "binaries")
class ShellCommandManager:
def __init__(self, launcher):
self.sh_instance = Sh()
self.launcher = launcher # Store reference to Launcher
self.launcher = launcher
self.reload_binaries()
def reload_binaries(self):
"""Reload the binaries from the PATH environment variable."""
paths = os.getenv("PATH", "").split(":")
unique_bins = set()
for path in paths:
unique_bins.update(self.list_binaries_in_path(path))
self.save_binaries_to_cache(unique_bins)
def list_binaries_in_path(self, path):
if not os.path.exists(path):
return []
try:
return (
subprocess.check_output(f"ls {path}", shell=True, text=True)
.strip()
.splitlines()
)
except subprocess.CalledProcessError:
return []
def save_binaries_to_cache(self, binaries):
with open(BINS_PATH, "w") as f:
f.write("\n".join(binaries))
def show_shell_commands(self, viewport, search_query: str):
if not search_query:
return
def on_query_done(results):
if not results:
return
results = self.query_binaries(search_query)
self.display_results(viewport, results)
for bin in results:
button = Button(
child=Label(
label=bin,
h_expand=True,
v_align="center",
h_align="start",
),
on_clicked=lambda _, cmd=bin: (
self.execute_command(cmd), # Execute command and close launcher
),
name="sh-item",
)
viewport.add(button)
def query_binaries(self, filter_str):
try:
with open(BINS_PATH) as f:
return [line.strip() for line in f if filter_str in line][:16]
except FileNotFoundError:
return []
# Reload shell command list and query
self.sh_instance.reload()
results = Sh.query(search_query)
on_query_done(results)
def display_results(self, viewport, results):
for command in results:
button = Button(
child=Label(
label=command,
v_align="center",
h_align="start",
),
on_clicked=lambda _, cmd=command: self.execute_command(cmd),
name="sh-item",
)
viewport.add(button)
def execute_command(self, cmd: str):
try:
Sh.run(cmd) # Run the shell command
self.launcher.set_visible(False) # Close the launcher after execution
except Exception as e:
logger.error(f"Error executing command: {e}")
output = subprocess.check_output(cmd, shell=True, text=True)
print(output)
except subprocess.CalledProcessError as e:
print(e.output)
finally:
self.launcher.set_visible(False)