-
Notifications
You must be signed in to change notification settings - Fork 0
/
wezterm.lua
141 lines (130 loc) · 4.17 KB
/
wezterm.lua
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
local wezterm = require 'wezterm'
local os = require 'os'
local io = require 'io'
-- get environment variables
local user --[[string]] = os.getenv("USER") or "h1rono"
local home --[[string]] = os.getenv("HOME") or ("/home/" .. user)
local xdg_config_home --[[string]] = os.getenv("XDG_CONFIG_HOME") or (home .. "/.config")
-- fn(string) -> boolean
local function file_exists(name)
local handle --[[file]] = io.open(name, "r")
local res --[[boolean]] = handle ~= nil
if res then
handle:close()
end
return res
end
-- fn() -> array<string>
-- returns the sorted array of installed font names
local function installed_fonts()
local cmd --[[string]] = "fc-list | awk -F ':' '{print $2}' | sort | uniq"
local handle --[[file]] = io.popen(cmd) -- I'm not sure whether this type is correct
local cmd_res --[[string]] = handle:read("*a")
handle:close()
local --[[array<string>]] res = {}
for line --[[string]] in cmd_res:gmatch("[^\n\r]*") do
local l --[[string]] = line:gsub("^%s*(.-)%s*$", "%1")
res[#res + 1] = l
end
table.sort(res, function(a, b) return a < b end)
return res
end
-- fn(string, array<string>) -> boolean
-- judges whether the font named `font_name` exists in the array `fonts`
local function font_exists(font_name, fonts)
-- binary search
local ng --[[integer]], ok --[[integer]] = 0, #fonts
while ng + 1 < ok do
local mid --[[integer]] = (ng + ok) >> 1
if fonts[mid] < font_name then
ng = mid
else
ok = mid
end
end
return fonts[ok] ~= nil and font_name == fonts[ok]
end
-- fn(nil | array<string>, array<string>) -> array<string>
local function get_font_fallback(candidates, all_fonts)
local res --[[array<string>]] = {}
for i --[[integer]] = 1, #candidates do
local cand --[[string]] = candidates[i]
if font_exists(cand, all_fonts) then
table.insert(res, cand)
end
end
return res
end
-- fn() -> wezterm.font_with_fallback
local function make_font()
local all_fonts --[[array<string>]] = wezterm.GLOBAL.fonts or installed_fonts()
local candidates --[[array<string>]] = {
-- my favorite fonts
"SF Mono",
"Menlo",
"FirgeNerd Console", -- https://github.com/yuru7/Firge
}
local fallback --[[array<string>]] = get_font_fallback(candidates, all_fonts)
return wezterm.font_with_fallback(fallback)
end
-- type WezTermBackgroundLayer
-- ... https://wezfurlong.org/wezterm/config/lua/config/background.html#layer-definition
-- fn(array<string>) -> nil | WezTermBackgroundLayer
local function bg_image(candidates)
-- candidates is an array of background image paths
local bg_image_path --[[nil | string]]
for i = 1, #candidates do
local bg_candidate --[[string]] = candidates[i]
if file_exists(bg_candidate) then
bg_image_path = bg_candidate
break
end
end
if bg_image_path == nil then
return nil
end
return {
source = { File = bg_image_path },
opacity = 0.32,
hsb = {
hue = 1.0,
saturation = 1.0,
brightness = 0.5,
},
vertical_align = "Middle",
horizontal_align = "Center",
width = "Cover",
height = "Cover",
}
end
-- fn() -> array<WezTermBackgroundLayer>
local function make_background()
local res --[[array<WezTermBackgroundLayer>]] = {
{ -- base background color
source = { Color = "#282C34" },
opacity = 0.7,
width = "100%",
height = "100%",
},
}
local img --[[nil | WezTermBackgroundLayer]] = bg_image {
home .. "/Pictures/bg.png",
home .. "/Pictures/bg.jpeg",
home .. "/Pictures/bg.jpg",
home .. "/bg.png",
home .. "/.bg.jpeg",
home .. "/.bg.jpg",
xdg_config_home .. "/bg.png",
xdg_config_home .. "/bg.jpeg",
xdg_config_home .. "/bg.jpg"
}
if img ~= nil then
res[2] = img
end
return res
end
wezterm.GLOBAL.fonts = wezterm.GLOBAL.fonts or installed_fonts()
return {
font = make_font(),
background = make_background()
}