From 6f913b6d4a60b062689f0708a9699920b4ee6d39 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 7 Oct 2024 06:20:50 -0400 Subject: [PATCH] [+] Add functionality to print font logo --- hyfetch/font_logo.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ hyfetch/main.py | 6 ++++++ 2 files changed, 50 insertions(+) create mode 100644 hyfetch/font_logo.py diff --git a/hyfetch/font_logo.py b/hyfetch/font_logo.py new file mode 100644 index 00000000..0aa98401 --- /dev/null +++ b/hyfetch/font_logo.py @@ -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 + diff --git a/hyfetch/main.py b/hyfetch/main.py index 41e16ac1..c4e37f6b 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -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 @@ -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 @@ -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))