Icons
The icons system provides Nerd Font glyph resolution with automatic emoji fallback, font detection, interactive installation prompts, and categorized icon definitions for maltty CLIs.
Icons is a sub-export of the maltty package (maltty/icons), not a separate package. It ships as middleware that decorates ctx.icons with methods for resolving icon names to glyphs, checking font availability, and triggering installation.
Key Concepts
Nerd Font vs Emoji Fallback
Every icon has two representations: a Nerd Font glyph and an emoji fallback. When the middleware initializes, it detects whether Nerd Fonts are installed on the system. All subsequent ctx.icons.get() calls resolve to the appropriate variant automatically.
- Nerd Fonts detected -- returns the Nerd Font glyph (e.g.
\uE725forbranch) - Nerd Fonts not detected -- returns the emoji fallback (e.g. the twisted arrows emoji for
branch)
This means commands never need to check font availability themselves. Call ctx.icons.get('branch') and the correct character is returned.
Font Detection
Font detection runs once during middleware initialization and the result is cached for the entire command lifecycle. The detection pipeline works as follows:
- The middleware scans platform-specific font directories for installed font files (
.ttf,.otf,.ttc,.woff,.woff2). - It checks whether any discovered font file path contains the string "Nerd" (case-insensitive).
- If at least one match is found, Nerd Fonts are considered installed.
Detection is purely filesystem-based -- it scans well-known font directories rather than invoking external tools like system_profiler or reading the Windows registry. This keeps the implementation dependency-free and consistent across platforms.
Platform-specific font directories:
Directories that do not exist are silently skipped. If the entire scan fails, the middleware treats Nerd Fonts as not installed and falls back to emoji.
Icon Categories
Icons are organized into four categories:
Auto-Setup
When autoSetup is enabled, the middleware checks for Nerd Font availability during initialization. If no Nerd Font is detected, it prompts the user to install one before any command handler executes. The auto-setup flow works as follows:
- Detection runs -- if Nerd Fonts are found, auto-setup is skipped entirely.
- If no fonts are detected and
autoSetupistrue, the install flow begins. - If no
fontis specified in options, an interactive picker is displayed so the user can choose a Nerd Font family to install. - If a
fontis specified (e.g.font: 'FiraCode'), that font is installed directly without prompting. - Installation uses the system's package manager or a direct download depending on the platform.
- On success, subsequent
ctx.icons.get()calls resolve to Nerd Font glyphs for the remainder of the session. - On failure, a warning is logged via
ctx.logger.warn()and the middleware falls back to emoji -- commands continue to work without interruption.
When forceSetup is enabled, the install prompt is shown regardless of whether fonts are already detected. This is useful for switching to a different Nerd Font family.
Module Augmentation
Importing maltty/icons automatically augments the CommandContext interface with readonly icons: IconsContext. No manual type augmentation or casting is needed -- once the middleware is registered, ctx.icons is fully typed in all command handlers.
Adding the Middleware
With Configuration
IconsOptions
IconsContext
The icons middleware decorates ctx.icons with an IconsContext object providing methods for icon resolution.
ctx.icons.get()
Resolve an icon name to its display string. Returns the Nerd Font glyph when fonts are installed, the emoji fallback otherwise. Returns an empty string when the name is not found.
ctx.icons.has()
Check whether an icon name exists in the definitions (built-in or custom).
ctx.icons.category()
Retrieve all resolved icons for a category as a record of name-to-glyph mappings.
ctx.icons.installed()
Check whether Nerd Fonts are available. When forceSetup is enabled, this always returns false to allow re-triggering the setup flow.
ctx.icons.setup()
Interactively prompt the user to install Nerd Fonts. Returns a Result tuple with true on success or an IconsError on failure. On success, subsequent get() calls resolve to Nerd Font glyphs.
IconsError
Custom Icons
Merge custom icon definitions with the built-in defaults by passing an icons record. Each entry must provide both a nerdFont glyph and an emoji fallback.
Custom definitions override built-in icons with the same name. Access them the same way:
IconDefinition
Built-in Icon Reference
The following tables list every built-in icon with its name, Nerd Font glyph codepoint, and emoji fallback.
Git
DevOps
Status
Files
Usage Patterns
Using icons in a git status command
Using icons with the logger for deployment output
Using category() to build a legend
Checking installed() to show setup hints
Troubleshooting
Icons show emoji instead of glyphs
This means Nerd Fonts were not detected on your system. To resolve:
- Enable
autoSetup: truein theicons()middleware options to trigger an interactive install prompt on next run. - Alternatively, install a Nerd Font manually from nerdfonts.com and restart your terminal.
- Verify the font file is placed in one of the scanned directories for your platform.
Unknown icon name returns an empty string
ctx.icons.get() returns an empty string when the name is not found. To resolve:
- Check the name against the built-in icon reference tables above.
- If using a custom icon, confirm it was passed in the
iconsoption of the middleware and that the name matches exactly. - Use
ctx.icons.has(name)to verify whether an icon is defined before callingget().
Auto-setup fails
When the font install fails, a warning is logged and the middleware falls back to emoji. Common causes:
- Insufficient permissions -- the install may require elevated privileges depending on the target font directory. Try running with appropriate permissions or install the font manually.
- Network issues -- font downloads require internet access. Check connectivity if the install hangs or errors.
- Unsupported platform -- if the platform is not macOS, Linux, or Windows, font directory scanning returns no results and auto-setup cannot determine where to install fonts.