Refactored mani.py

This commit is contained in:
Vangelis Kostalas 2019-05-08 14:13:40 +03:00
parent af0421e8df
commit 4c542c2d26
3 changed files with 38 additions and 28 deletions

View file

@ -8,8 +8,6 @@
version="1.1"
id="svg8">
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-66.505356,-78.373996)">
<text

Before

Width:  |  Height:  |  Size: 1 KiB

After

Width:  |  Height:  |  Size: 998 B

5
images/unicode2.svg Normal file
View file

@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" width="28.73" height="29.16" viewBox="0 0 7.602 7.715">
<text style="line-height:125%;text-align:center" x="70.304" y="86.089" font-weight="400" font-size="10.583" font-family="sans-serif" letter-spacing="0" word-spacing="0" text-anchor="middle" stroke-width=".265" transform="translate(-66.505 -78.374)" textLength="100%">
<tspan x="70.304" y="86.089" font-family="{font}">{symbol}</tspan>
</text>
</svg>

After

Width:  |  Height:  |  Size: 462 B

59
main.py
View file

@ -3,6 +3,8 @@ import sys
sys.path.append("/home/zen/.local/lib/python2.7/site-packages/")
import os
from os.path import join
import time
import codecs
from ulauncher.search.SortedList import SortedList
@ -14,9 +16,22 @@ from ulauncher.api.shared.action.RenderResultListAction import RenderResultListA
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
if sys.version_info[0] >= 3:
unichr = chr
FILE_PATH = os.path.dirname(sys.argv[0])
class UnicodeChar:
def __init__(self, name, block, code):
self.name = name
self.block = block
self.code = code
self.character = unichr(int(code, 16))
def get_search_name(self):
return self.name
class SearchableItem:
def __init__(self, name, item):
self._name = name
@ -33,45 +48,37 @@ class UnicodeCharExtension(Extension):
class KeywordQueryEventListener(EventListener):
def __init__(self):
print('======'*40)
f = open(os.path.dirname(sys.argv[0]) + "/unicode_list.txt", "r")
data = f.readlines()
self.data = {}
# self.names = []
self.items = []
self.items_ = []
# self.blocks = []
for item in data:
item = item.strip()
name, code, block = item.split("\t")
self.data[name + " " + block] = (name, block, code)
self.items.append(name + " " + block)
self.items_.append(SearchableItem(name , (name, block, code)))
# self.names.append(name)
# self.blocks.append(block)
self._load_character_table()
def _load_character_table(self):
self.character_list = []
with open(join(FILE_PATH, "unicode_list.txt"), "r") as f:
for line in f.readlines():
name, code, block = line.strip().split('\t')
character = UnicodeChar(name, block, code)
self.character_list.append(character)
def on_event(self, event, extension):
items = []
arg = event.get_argument()
if arg:
m_ = SortedList(arg, min_score=60, limit=10)
m_.extend(self.items_)
for m in m_:
name, block, code = m._item
image_path = self.create_icon_image(code)
m_.extend(self.character_list)
for char in m_:
image_path = self._get_character_icon(char.code)
items.append(
ExtensionResultItem(
icon=image_path,
name=name + " - " + unichr(int(code, 16)),
description=block,
on_enter=CopyToClipboardAction(unichr(int(code, 16))),
name=char.name + " - " + char.character,
description=char.block + " - " + char.code,
on_enter=CopyToClipboardAction(char.character),
)
)
return RenderResultListAction(items)
def create_icon_image(self, code):
def _get_character_icon(self, code):
path = sys.argv[0] + "images/cache/icon_%s.svg" % code
if os.path.isfile(path):
return path
@ -79,7 +86,7 @@ class KeywordQueryEventListener(EventListener):
def load_icon_template():
with open(os.path.join(FILE_PATH, "images/unicode.svg"), "r") as i:
with open(os.path.join(FILE_PATH, "images/unicode2.svg"), "r") as i:
return i.read()