Skip to content

[cliphist] Add cliphist extension #217

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
95 changes: 95 additions & 0 deletions cliphist/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import albert as al
import subprocess as sp
import time

md_iid = "3.0"
md_version = "1.0"
md_name = "ClipHist"
md_description = "Access cliphist clipboard"
md_license = "MIT"
md_url = "https://github.com/albertlauncher/python/tree/main/cliphist"
md_authors = ["@ivomac"]
md_bin_dependencies = ["cliphist"]


def delete(entry: str):
"""Delete an entry from the cliphist history."""

sp.run(
["cliphist", "delete"],
input=entry,
stdout=sp.PIPE,
text=True,
).stdout

return


def copy(entry: str):
"""Copy an entry from the cliphist history to the clipboard."""

clip_entry = sp.run(
["cliphist", "decode"],
input=entry,
stdout=sp.PIPE,
text=True,
).stdout

al.setClipboardText(clip_entry)

return


class Plugin(al.PluginInstance, al.TriggerQueryHandler):
iconUrls = ["xdg:clipboard"]

def __init__(self):
al.PluginInstance.__init__(self)
al.TriggerQueryHandler.__init__(self)

def defaultTrigger(self):
return "cl "

def handleTriggerQuery(self, query):
for _ in range(5):
time.sleep(0.02)
if not query.isValid:
return

plug_id = self.id()

matcher = al.Matcher(query.string)

# Get the list of cliphist entries
clip_history = (
sp.run(
["cliphist", "list"],
stdout=sp.PIPE,
text=True,
)
.stdout.strip()
.splitlines()
)

# cliphist items are in the format:
# <id>\t<approximate text>
splits = [(item, *item.split("\t", 1)) for item in clip_history]

query.add(
[
al.StandardItem(
id=plug_id,
iconUrls=self.iconUrls,
text=text,
subtext=idx,
actions=[
al.Action("copy", "Copy", lambda u=item: copy(u)),
al.Action("delete", "Delete", lambda u=item: delete(u)),
],
)
for item, idx, text in splits
if matcher.match(item)
]
)

return