diff --git a/clir/cli.py b/clir/cli.py index 4d072e2..a0c389b 100644 --- a/clir/cli.py +++ b/clir/cli.py @@ -58,3 +58,9 @@ def run(tag: str = "", grep: str = ""): table = CommandTable(tag=tag, grep=grep) table.run_command() +@cli.command(help="Copy command to clipboard 📋") +@click.option('-t', '--tag', help="Search by tag") +@click.option('-g', '--grep', help="Search by grep") +def cp(tag: str = "", grep: str = ""): + table = CommandTable(tag=tag, grep=grep) + table.copy_command() diff --git a/clir/utils/objects.py b/clir/utils/objects.py index be16edb..7ece9d7 100644 --- a/clir/utils/objects.py +++ b/clir/utils/objects.py @@ -2,6 +2,8 @@ import re import json import uuid +import platform +import subprocess from rich import box from rich.console import Console from rich.table import Table @@ -99,6 +101,35 @@ def run_command(self): if uid: print(f'Running command: {command}') os.system(command) + + def copy_command(self): + current_commands = self.commands + + uid = self.get_command_uid() + + command = "" + for c in current_commands: + if current_commands[c]["uid"] == uid: + command = c + + if uid: + print(f'Copying command: {command}') + if platform.system() == "Darwin": + # Verify that pbcopy is installed + if _verify_installation(package = "pbcopy"): + os.system(f'echo -n "{command}" | pbcopy') + else: + print("pbcopy is not installed, this command needs pbcopy to work properly") + return + elif platform.system() == "Linux": + # Verify that xclip is installed + if _verify_installation(package = "xclip"): + os.system(f'echo -n "{command}" | xclip -selection clipboard') + else: + print("xclip is not installed, this command needs xclip to work properly") + return + else: + print("OS not supported") # Create a function that deletes a command when passing its uid def remove_command(self): @@ -138,6 +169,22 @@ def get_command_uid(self): return "" +def _verify_installation(package: str = ""): + if package == "xclip": + try: + subprocess.run(["xclip", "-version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + return True + except: + return False + if package == "pbcopy": + try: + subprocess.run(["pbcopy", "-version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + return True + except: + return False + + return "No package specified" + def _filter_by_tag(commands: dict = {}, tag: str = ""): if commands: current_commands = commands diff --git a/pyproject.toml b/pyproject.toml index 47888bd..0cbd90a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "clir" -version = "0.2.7" +version = "0.3.0" description = "A clear and fast way to store and recover your commands" authors = ["Elkin Aguas "] license = "MIT License" diff --git a/tests/copy_command.py b/tests/copy_command.py new file mode 100644 index 0000000..e69de29