# -*- coding: utf-8 -*- """gitmoji Picker for Albert Usage: g: gitmoji name or description Examples: g:bug g:*""" 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): if not query.isValid or not query.isTriggered: return [] if query.string == "*": matches = gitmojis else: 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) if not query.isValid: return [] matches = sorted(matches, key=lambda data: data["matchCount"], reverse=True) if query.isValid: 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["emoji"])]) for match in matches] else: return [] def count_matches(tokens, needles): count = 0 for token in tokens: for needle in needles: if needle in token: count += 1 return count