Skip to content

Commit

Permalink
Merge pull request #15 from Garulf/Use-material-deign-fonts
Browse files Browse the repository at this point in the history
Use material deign fonts
  • Loading branch information
Garulf authored Jan 2, 2022
2 parents 84053e3 + ed5fa8a commit 5fd3042
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 81 deletions.
34 changes: 0 additions & 34 deletions .github/workflows/pr-black.yml

This file was deleted.

Binary file added plugin/MaterialDesignIconsDesktop.ttf
Binary file not shown.
1 change: 0 additions & 1 deletion plugin/icons.json

This file was deleted.

84 changes: 40 additions & 44 deletions plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,68 @@
import json
import os
import webbrowser
from pathlib import Path
from difflib import SequenceMatcher as sm


from flox import Flox
from flox import Flox, Clipboard, ICON_BROWSER

import pyperclip

ICON_FOLDER = "./icons/"
META_FILE = "meta.json"
META_PATH = Path(Path.cwd(), 'plugin', META_FILE)
MAX_RESULTS = 100
SVG_FILE = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24" viewBox="0 0 24 24"><path d="{}" /></svg>'
MDI_URL = "https://materialdesignicons.com/icon/"


class MDI(Flox):
class MDI(Flox, Clipboard):
def __init__(self):
with open("./plugin/icons.json", "r") as f:
with open(META_PATH, "r") as f:
self.icons = json.load(f)
if not os.path.exists(ICON_FOLDER):
os.mkdir(ICON_FOLDER)
super().__init__()

def create_icon(self, icon_name):
if not os.path.isfile(f"{ICON_FOLDER}{icon_name}.svg"):
for icon in self.icons["icons"]:
if icon["name"] == icon_name:
with open(f"{ICON_FOLDER}{icon['name']}.svg", "w") as f:
f.write(SVG_FILE.format(icon["data"]))
return f"{ICON_FOLDER}{icon_name}.svg"

def filter(self, icon):

self.add_item(
title=icon["name"],
subtitle="Press ENTER to copy to clipboard (SHIFT+ENTER for more options)",
icon=self.create_icon(icon["name"]),
context=[icon["name"]],
method="copy_to_clipboard",
parameters=[icon["name"]],
)

def context_menu(self, data):
name = data[0]
self.add_item(
title="View icon on website",
subtitle=f"{MDI_URL}{data[0]}",
icon=self.create_icon("web"),
subtitle=f"{MDI_URL}{name}",
icon=ICON_BROWSER,
method="open_web",
parameters=[data[0]],
)
return self._results

def query(self, query):
# names = [icon['name'] for icon in icons['icons']]
q = query.lower()

for icon in self.icons["icons"]:
# If only one char search by first letter only
if len(q) < 2 and icon["name"].startswith(q):
self.filter(icon)
elif len(q) > 1 and q in icon["name"] or q in icon["aliases"]:
self.filter(icon)
if len(self._results) >= MAX_RESULTS:
break
def codepoint_to_char(self, codepoint):
return chr(int(codepoint, 16))

return self._results
def match(self, query, icon_name, aliases):
query = query.lower()
score = []
for alias in aliases:
_ = sm(lambda x: x==" " or x=="-", query, alias.lower())
score.append(_.ratio() * 100)
_ = sm(lambda x: x==" " or x=="-", query, icon_name.lower())
score.append(_.ratio() * 100)
return max(score)

def copy_to_clipboard(self, icon_name):
pyperclip.copy(icon_name)
def query(self, query):
for icon in self.icons:
score = self.match(query, icon["name"], icon['aliases'])
if score > 50 or query == '':
self.add_item(
title=icon["name"],
subtitle=", ".join(icon['aliases']),
glyph=self.codepoint_to_char(icon["codepoint"]),
font_family=str(
Path(self.plugindir).joinpath(
'plugin',
"#Material Design Icons Desktop"
)
),
score=int(score),
context=[icon["name"]],
method=self.put,
parameters=[icon["name"]],
)

def open_web(self, icon_name):
webbrowser.open(f"{MDI_URL}{icon_name}")
Expand Down
1 change: 1 addition & 0 deletions plugin/meta.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
git+git://github.com/garulf/[email protected]#egg=flox
pyperclip==1.8.2
git+git://github.com/garulf/[email protected]#egg=flox

0 comments on commit 5fd3042

Please sign in to comment.