Skip to content

Commit

Permalink
[F] Fix font logo matching
Browse files Browse the repository at this point in the history
  • Loading branch information
hykilpikonna committed Oct 7, 2024
1 parent 6f913b6 commit c1fbd13
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions hyfetch/font_logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,29 @@
from hyfetch.neofetch_util import get_distro_name


def levenshtein_distance(s1: str, s2: str) -> int:
# Create a matrix to hold the distances
if len(s1) < len(s2):
s1, s2 = s2, s1

previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1
deletions = current_row[j] + 1
substitutions = previous_row[j] + (c1 != c2)
current_row.append(min(insertions, deletions, substitutions))
previous_row = current_row

return previous_row[-1]


def get_font_logo() -> str:
cache = CACHE_PATH / 'font_logo_cache.txt'
if cache.exists():
return cache.read_text('utf-8')

font_logos: dict[str, str] = json.loads((Path(__file__).parent / 'data/font_logos.json').read_text('utf-8'))
font_logos = {k.lower(): v for k, v in font_logos.items()}

# Get the distro
distro = get_distro_name()

# Find most likely distro by textual similarity
distro = min(font_logos.keys(), key=lambda x: levenshtein_distance(x, distro))
distro = get_distro_name().lower()

# Find most likely distro by containing string
for k in font_logos.keys():
if k in distro:
distro = k
# If not found, try matching partial string (by splitting with " ")
if not distro:
for k in font_logos.keys():
if any(x in distro for x in k.split(' ')):
distro = k
# If still not found, give up
if not distro:
raise ValueError(f'No font logo found for distro: {distro}. The supported logos are in https://github.com/Lukas-W/font-logos')

logo = font_logos[distro]
cache.parent.mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit c1fbd13

Please sign in to comment.