Skip to content

Commit 8124e04

Browse files
committed
WIP: checking for fonts
1 parent 42a5d1a commit 8124e04

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

lib/pretty/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
(libraries
44
ansifmt
55
shell
6+
str
67
;; Internal dependencies
78
extra))

lib/pretty/icon.ml

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,58 @@ type t = {
1212
issue_char : string;
1313
}
1414

15-
let fc_list_cmd = {| fc-list | grep 'Hack Nerd Font Mono' |}
15+
let hack_nerd_font = "Hack Nerd Font Mono"
16+
let fc_list_cmd = Printf.sprintf "fc-list | grep '%s'" hack_nerd_font
17+
let uname_cmd = {| uname |}
1618

17-
(** Use fc-list to check if Hack Nerd Font is installed - this might be a little
18-
brittle, but it's simple - update if it becomes an issue. **)
19-
let nerd_font_installed () =
20-
fc_list_cmd |> Shell.proc_stdout |> String.trim |> String.length > 0
19+
type os_name =
20+
| Linux
21+
| MacOS
22+
23+
let uname_result =
24+
Shell.proc_stdout uname_cmd |> String.trim |> function
25+
| "Linux" -> Some Linux
26+
| "Darwin" -> Some MacOS
27+
| _ -> None
28+
29+
(* Based on the list of possible folders here: https://github.com/adrg/xdg/blob/master/README.md#other-directories*)
30+
let mac_font_dirs =
31+
[
32+
"~/Library/Fonts";
33+
"/Library/Fonts";
34+
"/System/Library/Fonts";
35+
"/Network/Library/Fonts";
36+
]
37+
38+
let string_contains str substr =
39+
let re = Str.regexp_string substr in
40+
try
41+
ignore (Str.search_forward re str 0);
42+
true
43+
with Not_found -> false
44+
45+
let rec font_exists_in_dirs font_name = function
46+
| [] -> false
47+
| dir :: dirs ->
48+
if Sys.file_exists dir then
49+
let files = Sys.readdir dir in
50+
if Array.exists (fun file -> string_contains file font_name) files then
51+
true
52+
else font_exists_in_dirs font_name dirs
53+
else font_exists_in_dirs font_name dirs
54+
55+
(* We only support mac and linux right now - if the system is unix based and
56+
it's linux, we can use the fc-list cmd, otherwise on mac we look in common
57+
directories manually for our font *)
58+
let nerd_font_installed =
59+
match uname_result with
60+
| Some Linux ->
61+
Shell.proc_stdout fc_list_cmd |> String.trim |> String.length > 0
62+
| Some MacOS -> font_exists_in_dirs hack_nerd_font mac_font_dirs
63+
| None -> false (* do we want this be false or true if we can't tell? *)
2164

2265
let icons =
23-
if nerd_font_installed () then
66+
if nerd_font_installed then
2467
{
2568
closed_char = "\u{ebda}";
2669
open_char = "\u{ea64}";

0 commit comments

Comments
 (0)