-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathbackdrops.lua
129 lines (115 loc) · 4.1 KB
/
backdrops.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
local wezterm = require('wezterm')
local platform = require('utils.platform')()
local colors = require('colors.custom')
-- Seeding random numbers before generating for use
-- Known issue with lua math library
-- see: https://stackoverflow.com/questions/20154991/generating-uniform-random-numbers-in-lua
math.randomseed(os.time())
math.random()
math.random()
math.random()
local PATH_SEP = platform.is_win and '\\' or '/'
---@class BackDrops
---@field current_idx number index of current image
---@field files string[] background images
local BackDrops = {}
BackDrops.__index = BackDrops
--- Initialise backdrop controller
---@private
function BackDrops:init()
local inital = {
current_idx = 1,
files = {},
}
local backdrops = setmetatable(inital, self)
wezterm.GLOBAL.background = nil
return backdrops
end
---MUST BE RUN BEFORE ALL OTHER `BackDrops` functions
---Workaround to set the `files` after instantiating `BackDrops`.
---WezTerm's fs utilities `read_dir` and `glob` work by running on a spawned child process.
---This throws a coroutine error if either of these functions are invoked in outside of `wezterm.lua`
---in the initial load of the Terminal config
function BackDrops:set_files()
self.files = wezterm.read_dir(wezterm.config_dir .. PATH_SEP .. 'backdrops')
wezterm.GLOBAL.background = self.files[1]
return self
end
---Override the current window options for background
---@private
---@param window any WezTerm Window see: https://wezfurlong.org/wezterm/config/lua/window/index.html
function BackDrops:_set_opt(window)
local opts = {
background = {
{
source = { File = wezterm.GLOBAL.background },
},
{
source = { Color = colors.background },
height = '100%',
width = '100%',
opacity = 0.96,
},
},
}
window:set_config_overrides(opts)
end
---Convert the `files` array to a table of `InputSelector` choices
---see: https://wezfurlong.org/wezterm/config/lua/keyassignment/InputSelector.html
function BackDrops:choices()
local choices = {}
for idx, file in ipairs(self.files) do
local name = file:match('([^' .. PATH_SEP .. ']+)$')
table.insert(choices, {
id = tostring(idx),
label = name,
})
end
return choices
end
---MUST BE RUN BEFORE APPEARANCE OPTIONS ARE SET
---Select a random file and redefine the global `wezterm.GLOBAL.background` variable
---Pass in `Window` object to override the background options to apply change
---@param window any? WezTerm `Window` see: https://wezfurlong.org/wezterm/config/lua/window/index.html
function BackDrops:random(window)
self.current_idx = math.random(#self.files)
wezterm.GLOBAL.background = self.files[self.current_idx]
if window ~= nil then
self:_set_opt(window)
end
end
---Cycle the loaded `files` and select the next background
---@param window any WezTerm `Window` see: https://wezfurlong.org/wezterm/config/lua/window/index.html
function BackDrops:cycle_forward(window)
if self.current_idx == #self.files then
self.current_idx = 1
else
self.current_idx = self.current_idx + 1
end
wezterm.GLOBAL.background = self.files[self.current_idx]
self:_set_opt(window)
end
---Cycle the loaded `files` and select the previous background
---@param window any WezTerm `Window` see: https://wezfurlong.org/wezterm/config/lua/window/index.html
function BackDrops:cycle_back(window)
if self.current_idx == 1 then
self.current_idx = #self.files
else
self.current_idx = self.current_idx - 1
end
wezterm.GLOBAL.background = self.files[self.current_idx]
self:_set_opt(window)
end
---Set a specific background from the `files` array
---@param window any WezTerm `Window` see: https://wezfurlong.org/wezterm/config/lua/window/index.html
---@param idx number index of the `files` array
function BackDrops:set_img(window, idx)
if idx > #self.files or idx < 0 then
wezterm.log_error('Index out of range')
return
end
self.current_idx = idx
wezterm.GLOBAL.background = self.files[self.current_idx]
self:_set_opt(window)
end
return BackDrops:init()