58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""gitmoji Picker for Albert
|
|
Usage: g: gitmoji name or description
|
|
Example: g: bug"""
|
|
|
|
from albertv0 import *
|
|
|
|
import os
|
|
import json
|
|
|
|
__iid__ = "PythonInterface/v0.2"
|
|
__prettyname__ = "gitmoji picker"
|
|
__version__ = "1.0.0"
|
|
__trigger__ = "g:"
|
|
__author__ = "Andreas Schneider"
|
|
__dependencies__ = []
|
|
|
|
data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "gitmojis.json")
|
|
|
|
gitmojis = []
|
|
|
|
def initialize():
|
|
with open(data_path) as f:
|
|
data = json.load(f)
|
|
|
|
gitmojis.clear()
|
|
|
|
for gitmoji in data["gitmojis"]:
|
|
gitmoji["tokens"] = [gitmoji["name"].lower()] + gitmoji["description"].lower().split()
|
|
gitmojis.append(gitmoji)
|
|
|
|
def handleQuery(query):
|
|
needles = query.string.lower().split()
|
|
|
|
matches = []
|
|
for gitmoji in gitmojis:
|
|
matchCount = count_matches(gitmoji["tokens"], needles)
|
|
if matchCount > 0:
|
|
result = dict()
|
|
result.update(gitmoji)
|
|
result["matchCount"] = matchCount
|
|
matches.append(result)
|
|
|
|
matches = sorted(matches, key=lambda data: data["matchCount"], reverse=True)
|
|
|
|
if query.isValid and query.isTriggered:
|
|
return [Item(id=match["name"], completion=match["name"], icon=match["emoji"], text=match["emoji"] + " " + match["code"], subtext=match["description"], actions=[ClipAction("Copy to clipboard", match["code"])]) for match in matches]
|
|
return []
|
|
|
|
def count_matches(tokens, needles):
|
|
count = 0
|
|
for token in tokens:
|
|
for needle in needles:
|
|
if needle in token:
|
|
count += 1
|
|
return count
|