Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active May 24, 2019 22:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaykul/67ef712c798fb2bbc693d9c77f469fe7 to your computer and use it in GitHub Desktop.
Save Jaykul/67ef712c798fb2bbc693d9c77f469fe7 to your computer and use it in GitHub Desktop.
NerdFonts as Entities (thanks to Pansies)

Using Nerd Fonts in PowerShell

I've been recommending people use NerdFonts in their terminals for a while, but using the extra characters ends up being rather a pain. I have to look up characters in their web cheat-sheet, and then I either end up with a whole bunch of "$([char]0xf1ed)$([char]0xf00d)" in my profile scripts, which is impossible to read, or I have to translate the hex to decimal so I can type the ALT codes in my editor...

So today I fixed it, using PANSIES (the PowerShell ANSI Escape Sequences module).

PANSIES has &entities;

In PANSIES output (i.e. via the Write-Host, or by using New-Text in a string), you can embed named entities like in html. But unlike HTML, Pansies entities are extensible. So I wrote a little script to re-use the css names for the nerd-font characters and generate entities.

Now I can write things like:

Read-Host (Text "Do you use $fg:OrangeRed&nf-fa-firefox;$fg:clear, $fg:SteelBlue&nf-fa-chrome;$fg:clear or $fg:DodgerBlue&nf-fa-edge;$fg:clear")

if([PoshCode.Pansies.Entities]::ExtendedCharacters.ContainsKey("nf-custom-c")) { return }
Invoke-RestMethod https://raw.githubusercontent.com/ryanoasis/nerd-fonts/master/css/nerd-fonts-generated.css |
Select-String '(?m)^\.(nf-[^:]+):[^{]+{\s+content: "\\([^"]{4})"[^}]+}' -AllMatches | ForEach-Object Matches |
ForEach-Object { [PoshCode.Pansies.Entities]::ExtendedCharacters.Add($_.Groups[1], ([char][int]"0x$($_.Groups[2])") ) }
@Jaykul
Copy link
Author

Jaykul commented May 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment