Skip to content

Commit

Permalink
Add copy command to clipboard for Linux and macos
Browse files Browse the repository at this point in the history
  • Loading branch information
elkinaguas committed Nov 13, 2023
1 parent 35fb473 commit a1851ad
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions clir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
47 changes: 47 additions & 0 deletions clir/utils/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT License"
Expand Down
Empty file added tests/copy_command.py
Empty file.

0 comments on commit a1851ad

Please sign in to comment.