Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically know if the current font has the powerline patch

Tags:

vim

zsh

fonts

fish

I use vim with vim-airline that can use patched fonts for additional awesomeness.

vim-airline

I also use fish shell (if you want read zsh here since it is equivalent for the analysis) with a theme that also uses patched fonts.

I save my dotfiles online and I use them in different systems. But in some of them I don't have a patched font. I would really love to have generic dotfiles that for example do not use a theme that needs patched font if the current font is not patched. Also in my vimrc I disabled the use of patched characters for vim-airline because of this.

Is there a way to programmatically know if the font that is currently in use is a patched one so that is possible to write generic dotfiles that fit perfectly in all the situations?

like image 422
enrico.bacis Avatar asked Oct 29 '25 05:10

enrico.bacis


2 Answers

In Vim, you would need to directly ask your terminal emulator what font it is currently using. The exact method will certainly be different from terminal to terminal and from OS to OS. If that's even possible.

If you have it on all your systems, you could use lsof to find what font is currently used by a given program:

$ lsof | grep iTerm | grep '\.\(otf\|ttf\)'

In GVim/MacVim, you would check if the current font name matches some pattern with something like:

if &guifont =~? 'powerline'
    " do something
else
    " do something else
endif

And that's assuming your font's filename contains that pattern.

But it's impossible to guarantee that an actual "powerline font" has powerline in its file name and it's equally impossible to guarantee that a font with powerline in its file name is an actual "powerline font".

It would be safer to check for the presence of the Powerline string somewhere in the font with something like:

$ xxd ~/Downloads/FuraMono-Bold\ Powerline.otf | grep Powerline

But that's assuming:

  • xxd is available,
  • you were able to derive the path of the actual font from whatever info you managed to gather from your terminal emulator or from GVim/MacVim.

That's a lot of assumptions and seemingly a lot of work for something as superficial as a "powerline font".

like image 74
romainl Avatar answered Nov 01 '25 13:11

romainl


You can do this the other way around and only enable Powerline fonts for known machines in your .vimrc e.g.

let hostname = substitute(system('hostname'), '\n', '', '')
let patched_font_hosts = ['host1', 'host2', 'host3']   " List of know patched hosts
if(index(patched_font_hosts, hostname) >= 0)
  let g:airline_powerline_fonts = 1
endif

It's easier to know which hosts you have patched and it's not a really a problem if the list is not up to date.

like image 40
Paul Netherwood Avatar answered Nov 01 '25 13:11

Paul Netherwood