Skip to content

refactor(platform): deduplicate Nerd Font detection across OS-specific functions #80

@jongio

Description

@jongio

Summary

Three nearly identical functions in internal/platform/fonts.go (lines 28–72) duplicate the same Nerd Font detection logic, differing only in which directories to scan:

  • isNerdFontInstalledWindows() (lines 28–41)
  • isNerdFontInstalledDarwin() (lines 43–52)
  • isNerdFontInstalledLinux() (lines 54–72)

Each function calls hasNerdFontFiles() on a set of OS-specific paths and returns true if any match. The scanning logic and return pattern are identical.

Why This Matters

  • Duplicate code: The same "scan a list of directories and return on first match" pattern is written 3 times.
  • Maintenance burden: Adding a new font name pattern or changing the scanning logic requires updating all 3 functions.
  • Inconsistent error handling: The Windows function does not call os.UserHomeDir() (it uses env vars), while Darwin and Linux do. Linux has an additional fc-list fallback that the other two lack, making it unclear whether that fallback should also apply to other platforms.

Current Code

`go
func isNerdFontInstalledWindows() bool {
localAppData := os.Getenv("LOCALAPPDATA")
if localAppData != "" {
if hasNerdFontFiles(filepath.Join(localAppData, "Microsoft", "Windows", "Fonts")) {
return true
}
}
winDir := os.Getenv("WINDIR")
if winDir == "" { winDir = "C:\Windows" }
return hasNerdFontFiles(filepath.Join(winDir, "Fonts"))
}

func isNerdFontInstalledDarwin() bool {
home, err := os.UserHomeDir()
if err != nil { return false }
if hasNerdFontFiles(filepath.Join(home, "Library", "Fonts")) { return true }
return hasNerdFontFiles("/Library/Fonts")
}

func isNerdFontInstalledLinux() bool {
// Similar pattern + fc-list fallback
}
`

Suggested Refactoring

Extract the common scanning logic into a single helper:

`go
func isNerdFontInstalled() bool {
for _, dir := range nerdFontSearchPaths() {
if hasNerdFontFiles(dir) {
return true
}
}
return hasFcListNerdFont() // fc-list fallback (Linux only, no-op elsewhere)
}

func nerdFontSearchPaths() []string {
switch runtime.GOOS {
case "windows":
return windowsFontPaths()
case "darwin":
return darwinFontPaths()
default:
return linuxFontPaths()
}
}
`

This reduces 44 lines of duplicated logic to a single reusable function with OS-specific path providers.

Smell Categories

  • Dispensables → Duplicate Code (3 functions with identical structure)

Metadata

Metadata

Assignees

No one assigned

    Labels

    automatedCreated by automationsmellsCode smell detection

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions