From 56a06a32c9e29512d0a406aa53a508cbbb36cd64 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 20 Mar 2023 01:33:21 -0500 Subject: [PATCH] fix(#4): refactor to reduce some boilerplate --- Makefile | 9 +++------ README.md | 6 +++--- bin/get-font-files | 30 ---------------------------- bin/patch-monolisa => patch-monolisa | 23 +++++++++++++++------ 4 files changed, 23 insertions(+), 45 deletions(-) delete mode 100755 bin/get-font-files rename bin/patch-monolisa => patch-monolisa (94%) diff --git a/Makefile b/Makefile index fd1ab0b..9eb81c3 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,10 @@ -include .env ARGS ?= -c -NF_SRC := $(shell ./bin/get-font-files src) -FONT_FLAGS := $(shell ./bin/get-font-files MonoLisa 'otf,ttf,woff,woff2') - patch: ./bin/font-patcher ## apply nerd fonts patch |> -gs b_magenta -ms bold - @./bin/patch-monolisa \ - $(FONT_FLAGS) \ - $(ARGS) + @./patch-monolisa \ + $(ARGS) \ + -f MonoLisa/ update-fonts: ## move fonts and update fc-cache $(call msg,Adding Fonts To System) diff --git a/README.md b/README.md index 340ed00..1e77ccb 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ tested w/ MonoLisa v2.003 ## Dependencies - `python` -- `make` +- `make` (optional) - `fontforge` OR `docker` @@ -52,7 +52,7 @@ you can easily apply the nerd font patches with `make`. To patch all font types use the default `patch` rule. ```bash -make +make # or ./patch-monolisa -f MonoLisa -c ``` By default the complete (`-c`) flag is passed to the font-patcher script to include all icons/symbols. @@ -62,7 +62,7 @@ You can change this by specifying the `ARGS` at runtime. ARGS="-c -w" make patch ``` -See `./bin/patch-monolisa --help` and `./bin/font-patcher --help` for available `ARGS`. +See `./patch-monolisa --help` and `./bin/font-patcher --help` for available `ARGS`. You can find your patched fonts in the `patched/` directory diff --git a/bin/get-font-files b/bin/get-font-files deleted file mode 100755 index da9f108..0000000 --- a/bin/get-font-files +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python3 - -import sys -from pathlib import Path - -EXTS = ["otf", "ttf", "woff", "woff2"] - - -def find_files(search_path, exts=None): - return ( - [f for ext in exts for f in search_path.glob(f"**/*.{ext}")] - if exts - else [f for f in search_path.rglob("*") if f.is_file()] - ) - - -def main(): - if len(sys.argv) == 1: - print("please specify directory to search") - exit(1) - - search_path = Path(sys.argv[1]) - exts = sys.argv[2].split(",") if len(sys.argv) == 3 else EXTS - - for f in find_files(search_path, exts): - sys.stdout.write(f"-f '{f}' ") - - -if __name__ == "__main__": - main() diff --git a/bin/patch-monolisa b/patch-monolisa similarity index 94% rename from bin/patch-monolisa rename to patch-monolisa index 5d9c3c5..47f42e2 100755 --- a/bin/patch-monolisa +++ b/patch-monolisa @@ -10,7 +10,7 @@ import time from pathlib import Path from typing import List, Set -FONT_SRC = (Path(__file__).parent.parent / "MonoLisa").absolute() +FONT_SRC = (Path(__file__).parent / "MonoLisa").absolute() class Color: @@ -159,7 +159,6 @@ def patch_single_font( f"{color.yellow}:::{color.end} Patching font " f"{color.bold}{font_path.name}{color.end}... " ): - run_cmd(cmd, font_path, verbose) echo(f"{rel_path} patched!", hue="green") @@ -205,6 +204,17 @@ def patch_font_dir_docker( echo(f"{font_dir_path.name}/ fonts patched!", hue="green") +def get_font_files(p): + exts = ["otf", "ttf", "woff", "woff2"] + + if p.is_file(): + return (p,) + + if p.is_dir(): + return (f for ext in exts for f in p.glob(f"**/*.{ext}")) + return () + + def echo(msg: str, header=False, hue="cyan") -> None: if header: print(f"==>{color.magenta} {msg} {color.end}<==") @@ -213,7 +223,6 @@ def echo(msg: str, header=False, hue="cyan") -> None: def main(): - echo("MonoLisa NerdFont Patcher", header=True) args, fp_args = get_args() fp_args = " ".join(fp_args) @@ -223,12 +232,15 @@ def main(): echo("Patching the following files") for fontfile in args.font_path: sys.stdout.write(f" {color.magenta}->{color.end} {fontfile}\n") + + fontfiles = [f for p in args.font_path for f in get_font_files(p)] + if args.docker: echo("==> DOCKER MODE ENABLED") - for font_dir in collect_files_by_dir(args.font_path): + for font_dir in collect_files_by_dir(fontfiles): patch_font_dir_docker(font_dir, args.output, fp_args, args.verbose) else: - for fontfile in args.font_path: + for fontfile in fontfiles: patch_single_font(Path(fontfile), args.output, fp_args, args.verbose) echo("fonts are patched", hue="green") @@ -236,6 +248,5 @@ def main(): if __name__ == "__main__": - color = Color() main()