Skip to content

Commit

Permalink
Add module to map nerd font symbol names to codepoints
Browse files Browse the repository at this point in the history
I generated nerdfonts_data.rs with this shell script; it uses `i_all.sh`
from the nerdfonts repo to get the base mapping:

```
source ./lib/i_all.sh

echo "//! Data mapping nerd font symbol names to their char codepoints"
echo "pub const NERD_FONT_GLYPHS: &[(&str, char)] = &["
for var in "${!i@}"; do
  # trim 'i_' prefix
  glyph_name=${var#*_}
  glyph_char=${!var}
  glyph_code=$(printf "%x" "'$glyph_char'")
  echo "(\"$glyph_name\", '\u{$glyph_code}'), // $glyph_char"
done
echo "];"
```

Then intent is to use it in wezterm:

```
local wezterm = require 'wezterm'

wezterm.log_info(wezterm.nerdfonts.dev_mozilla)
```
  • Loading branch information
wez committed Jan 17, 2022
1 parent 070c403 commit 54e92b6
Show file tree
Hide file tree
Showing 4 changed files with 3,856 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {

wezterm_mod.set("target_triple", crate::wezterm_target_triple())?;
wezterm_mod.set("version", crate::wezterm_version())?;
wezterm_mod.set("nerdfonts", NerdFonts{})?;
wezterm_mod.set("home_dir", crate::HOME_DIR.to_str())?;
wezterm_mod.set(
"running_under_wsl",
Expand Down Expand Up @@ -1028,3 +1029,13 @@ pub fn truncate_right(s: &str, max_width: usize) -> String {
}
result
}

struct NerdFonts {}

impl mlua::UserData for NerdFonts {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(mlua::MetaMethod::Index, |_, _, key: String| -> mlua::Result<Option<String>> {
Ok(termwiz::nerdfonts::NERD_FONTS.get(key.as_str()).map(|c| c.to_string()))
});
}
}
2 changes: 2 additions & 0 deletions termwiz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub mod istty;
pub mod keymap;
pub mod lineedit;
mod macros;
pub mod nerdfonts;
mod nerdfonts_data;
mod readbuf;
pub mod render;
pub mod surface;
Expand Down
12 changes: 12 additions & 0 deletions termwiz/src/nerdfonts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::collections::HashMap;

lazy_static::lazy_static! {
pub static ref NERD_FONTS: HashMap<&'static str, char> = build_map();
}

fn build_map() -> HashMap<&'static str, char> {
crate::nerdfonts_data::NERD_FONT_GLYPHS
.iter()
.map(|tuple| *tuple)
.collect()
}
Loading

0 comments on commit 54e92b6

Please sign in to comment.