2019-02-06 17:53:27 +01:00
# -*- coding: utf-8 -*-
""" gitmoji Picker for Albert
Usage : g : gitmoji name or description
2019-02-08 14:31:50 +01:00
Examples :
g : bug
g : * """
2019-02-06 17:53:27 +01:00
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 ) :
2019-02-08 14:31:50 +01:00
if not query . isValid or not query . isTriggered :
return [ ]
2019-02-06 17:53:27 +01:00
2019-02-08 14:31:50 +01:00
if query . string == " * " :
matches = gitmojis
else :
needles = query . string . lower ( ) . split ( )
2019-02-06 17:53:27 +01:00
2019-02-08 14:31:50 +01:00
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 )
2019-02-06 17:53:27 +01:00
2019-02-08 14:31:50 +01:00
if not query . isValid :
return [ ]
matches = sorted ( matches , key = lambda data : data [ " matchCount " ] , reverse = True )
if query . isValid :
2019-02-12 11:14:05 +01:00
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 ]
2019-02-08 14:31:50 +01:00
else :
return [ ]
2019-02-06 17:53:27 +01:00
def count_matches ( tokens , needles ) :
count = 0
for token in tokens :
for needle in needles :
if needle in token :
count + = 1
return count