-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.gleam
More file actions
156 lines (132 loc) · 3.54 KB
/
Copy pathcache.gleam
File metadata and controls
156 lines (132 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import envoy
import gleam/bit_array
import gleam/dict.{type Dict}
import gleam/int
import gleam/list
import gleam/result
import gleam/string
import image
import simplifile
import wisp
pub type CachedImage {
CachedImage(base64: String, info: image.ImageInformation)
}
pub type Glyph {
Digit(Int)
Start
End
}
pub type ThemeCache =
Dict(String, Dict(Glyph, CachedImage))
@external(erlang, "cache_ffi", "store")
pub fn store(cache: ThemeCache) -> Nil
@external(erlang, "cache_ffi", "read")
pub fn read() -> ThemeCache
pub fn load_themes() {
let enabled_themes =
envoy.get("MAYU_THEMES")
|> result.unwrap("")
|> string.split(",")
|> list.map(string.trim)
|> list.filter(fn(name) { name != "" })
let themes = read_directory_or_empty("./themes")
let selected_themes = case enabled_themes {
[] -> themes
_ -> list.filter(themes, fn(theme) { list.contains(enabled_themes, theme) })
}
selected_themes
|> list.map(fn(theme) { #(theme, load_theme(theme)) })
|> dict.from_list
}
fn read_directory_or_empty(path) -> List(String) {
case simplifile.read_directory(path) {
Ok(files) -> files
Error(_) -> {
wisp.log_error("Error reading directory " <> path)
[]
}
}
}
fn load_theme(theme) -> Dict(Glyph, CachedImage) {
let theme_directory = "./themes/" <> theme
let files = read_directory_or_empty(theme_directory)
let glyphs =
files
|> list.filter_map(fn(file) {
use glyph <- result.try(parse_glyph_filename(file))
use cached_image <- result.try(load_cached_image(
theme_directory <> "/" <> file,
))
Ok(#(glyph, cached_image))
})
|> dict.from_list
warn_if_incomplete(theme, glyphs)
glyphs
}
fn warn_if_incomplete(theme, glyphs) -> Nil {
let missing =
int.range(from: 0, to: 10, with: [], run: fn(missing, digit) {
case dict.has_key(glyphs, Digit(digit)) {
True -> missing
False -> [digit, ..missing]
}
})
|> list.reverse
case missing {
[] -> Nil
_ ->
wisp.log_warning(
"Theme "
<> theme
<> " is missing digit glyphs: "
<> { missing |> list.map(int.to_string) |> string.join(", ") },
)
}
}
fn parse_glyph_filename(file) {
case string.split(file, ".") {
["_start", _extension] -> Ok(Start)
["_end", _extension] -> Ok(End)
[digit, _extension] ->
case int.parse(digit) {
Ok(parsed_digit) if parsed_digit >= 0 && parsed_digit <= 9 ->
Ok(Digit(parsed_digit))
_ -> Error(Nil)
}
_ -> Error(Nil)
}
}
fn load_cached_image(path) {
use image_data <- result.try(
simplifile.read_bits(from: path)
|> result.map_error(fn(_) {
wisp.log_error("Error reading image file " <> path)
}),
)
use info <- result.map(
image.get_image_information(image_data)
|> result.map_error(fn(_) {
wisp.log_error("Error getting image information for " <> path)
}),
)
CachedImage(base64: bit_array.base64_encode(image_data, True), info: info)
}
pub fn get_image(cache, theme, glyph) -> Result(CachedImage, Nil) {
dict.get(cache, theme)
|> result.try(fn(theme_images) { dict.get(theme_images, glyph) })
}
const preferred_default_theme = "asoul"
pub fn default_theme(cache) -> String {
let names =
cache
|> dict.keys
|> list.sort(string.compare)
case list.contains(names, preferred_default_theme) {
True -> preferred_default_theme
False ->
names
|> list.filter(fn(name) { !string.ends_with(name, "-h") })
|> list.first
|> result.unwrap(preferred_default_theme)
}
}