Skip to content

Commit

Permalink
[+] Add functionality to print font logo
Browse files Browse the repository at this point in the history
  • Loading branch information
hykilpikonna committed Oct 7, 2024
1 parent 1077ee5 commit 6f913b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions hyfetch/font_logo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json
from pathlib import Path

from hyfetch.constants import CACHE_PATH
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'))

# 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))

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

return logo

6 changes: 6 additions & 0 deletions hyfetch/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .color_scale import Scale
from .color_util import clear_screen
from .constants import *
from .font_logo import get_font_logo
from .models import Config
from .neofetch_util import *
from .presets import PRESETS
Expand Down Expand Up @@ -347,6 +348,7 @@ def create_parser() -> argparse.ArgumentParser:

parser.add_argument('--distro', '--test-distro', dest='distro', help=f'Test for a specific distro')
parser.add_argument('--ascii-file', help='Use a specific file for the ascii art')
parser.add_argument('--print-font-logo', action='store_true', help='Print the Font Logo / Nerd Font icon of your distro and exit')

# Hidden debug arguments
# --test-print: Print the ascii distro and exit
Expand Down Expand Up @@ -391,6 +393,10 @@ def run():
print(get_distro_ascii())
return

if args.print_font_logo:
print(get_font_logo())
return

# Check if user provided alternative config path
if not args.config_file == CONFIG_PATH:
args.config_file = Path(os.path.abspath(args.config_file))
Expand Down

0 comments on commit 6f913b6

Please sign in to comment.