use std::path::{Path, PathBuf};
const FONT_URL: &str = "https://raw.githubusercontent.com/ryanoasis/nerd-fonts/v3.4.0/patched-fonts/Meslo/S/Regular/MesloLGSNerdFontMono-Regular.ttf";
const FONT_DIR: &str = ".termux";
const FONT_FILE: &str = "font.ttf";
const FONT_TMP_FILE: &str = "font.ttf.croft-download";
const RELOAD_CMD: &str = "termux-reload-settings";
const MAX_FONT_BYTES: u64 = 16 * 1024 * 1024;
fn font_path(home: &Path) -> PathBuf {
home.join(FONT_DIR).join(FONT_FILE)
}
fn install_needed(termux: bool, font_exists: bool) -> bool {
termux && !font_exists
}
fn looks_like_ttf(bytes: &[u8]) -> bool {
bytes.len() >= 4
&& matches!(
&bytes[..4],
[0x00, 0x01, 0x00, 0x00] | b"true" | b"OTTO" | b"ttcf"
)
}
fn download_font() -> anyhow::Result<Vec<u8>> {
use std::io::Read;
let resp = ureq::get(FONT_URL).call()?;
let mut bytes = Vec::new();
resp.into_reader()
.take(MAX_FONT_BYTES)
.read_to_end(&mut bytes)?;
anyhow::ensure!(
looks_like_ttf(&bytes),
"downloaded body is not a font file ({} bytes)",
bytes.len()
);
Ok(bytes)
}
fn install_font(target: &Path, bytes: &[u8]) -> std::io::Result<()> {
let dir = target.parent().expect("font path always has ~/.termux");
std::fs::create_dir_all(dir)?;
let staging = dir.join(FONT_TMP_FILE);
std::fs::write(&staging, bytes)?;
std::fs::rename(&staging, target)
}
pub fn ensure_codicon_font_in_background() {
use crate::lsp::log_file;
if !crate::iterm2_inline::detect_termux() {
return;
}
let Some(home) = std::env::var_os("HOME") else {
log_file::log("termux font: $HOME unset, skipping font install");
return;
};
let target = font_path(Path::new(&home));
if !install_needed(true, target.exists()) {
return;
}
std::thread::spawn(move || {
match download_font() {
Ok(bytes) => {
if let Err(e) = install_font(&target, &bytes) {
log_file::log(&format!("termux font: install to {target:?} failed: {e}"));
return;
}
log_file::log(&format!(
"termux font: installed Meslo Nerd Font Mono to {target:?}"
));
}
Err(e) => {
log_file::log(&format!("termux font: download failed: {e}"));
return;
}
}
match std::process::Command::new(RELOAD_CMD).output() {
Ok(out) if out.status.success() => {
log_file::log("termux font: reloaded Termux settings, icons live")
}
Ok(out) => log_file::log(&format!(
"termux font: {RELOAD_CMD} exited {}; font applies on next Termux restart",
out.status
)),
Err(e) => log_file::log(&format!(
"termux font: {RELOAD_CMD} not runnable ({e}); font applies on next Termux restart"
)),
}
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn font_path_is_termux_font_ttf_under_home() {
assert_eq!(
font_path(Path::new("/data/data/com.termux/files/home")),
PathBuf::from("/data/data/com.termux/files/home/.termux/font.ttf"),
"Termux loads exactly ~/.termux/font.ttf; any other path is ignored"
);
}
#[test]
fn install_runs_only_on_termux_and_only_when_no_font_is_present() {
assert!(
install_needed(true, false),
"Termux with no custom font is the broken-icons case the install fixes"
);
assert!(
!install_needed(true, true),
"an existing font.ttf is the user's choice; never clobber it"
);
assert!(
!install_needed(false, false),
"non-Termux hosts pick fonts via setup-iterm2/setup-ghostty, not font.ttf"
);
assert!(!install_needed(false, true));
}
#[test]
fn ttf_magic_accepts_sfnt_flavors_and_rejects_garbage() {
assert!(looks_like_ttf(&[0x00, 0x01, 0x00, 0x00, 0xab]));
assert!(looks_like_ttf(b"trueXYZ"));
assert!(looks_like_ttf(b"OTTOrest"));
assert!(looks_like_ttf(b"ttcfrest"));
assert!(
!looks_like_ttf(b"<!DOCTYPE html>"),
"a 404/error page must never be installed as the terminal font"
);
assert!(!looks_like_ttf(b"\x00\x01"), "truncated header rejected");
assert!(!looks_like_ttf(b""));
}
#[test]
fn font_url_pins_release_tag_and_mono_variant() {
assert!(
FONT_URL.contains("/v3.4.0/"),
"URL must pin a release tag so installed bytes never drift"
);
assert!(
FONT_URL.ends_with("MesloLGSNerdFontMono-Regular.ttf"),
"must be the Mono variant of the same Meslo family setup-iterm2 uses"
);
assert!(
FONT_URL.starts_with("https://raw.githubusercontent.com/ryanoasis/nerd-fonts/"),
"raw file fetch, not the rate-limited GitHub REST API"
);
}
}