Skip to content

Commit

Permalink
feat: add method for constructing background options + stop usage of …
Browse files Browse the repository at this point in the history
…global variable for setting image path
  • Loading branch information
KevinSilvester committed Oct 30, 2024
1 parent bd8119b commit d8bbb11
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
23 changes: 6 additions & 17 deletions config/appearance.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
local wezterm = require('wezterm')
local gpu_adapters = require('utils.gpu_adapter')
local backdrops = require('utils.backdrops')
local colors = require('colors.custom')

return {
animation_fps = 60,
max_fps = 60,
animation_fps = 165,
max_fps = 165,
front_end = 'WebGpu',
webgpu_power_preference = 'HighPerformance',
webgpu_preferred_adapter = gpu_adapters:pick_best(),
-- webgpu_preferred_adapter = gpu_adapters:pick_manual('Dx12', 'IntegratedGpu'),

-- color scheme
colors = colors,

-- background
background = {
{
source = { File = wezterm.GLOBAL.background },
horizontal_align = 'Center',
},
{
source = { Color = colors.background },
height = '120%',
width = '120%',
vertical_offset = '-10%',
horizontal_offset = '-10%',
opacity = 0.96,
},
},
background = backdrops:create_opts(),

-- scrollbar
enable_scroll_bar = true,
Expand All @@ -46,6 +34,7 @@ return {
top = 10,
bottom = 7.5,
},
adjust_window_size_when_changing_font_size = false,
window_close_confirmation = 'NeverPrompt',
window_frame = {
active_titlebar_bg = '#090909',
Expand Down
2 changes: 1 addition & 1 deletion config/general.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ return {
exit_behavior_messaging = 'Verbose',
status_update_interval = 1000,

scrollback_lines = 5000,
scrollback_lines = 20000,

hyperlink_rules = {
-- Matches: a URL in parens: (URL)
Expand Down
54 changes: 26 additions & 28 deletions utils/backdrops.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local wezterm = require('wezterm')
local platform = require('utils.platform')
local colors = require('colors.custom')

-- Seeding random numbers before generating for use
Expand All @@ -10,7 +9,6 @@ math.random()
math.random()
math.random()

local PATH_SEP = platform.is_win and '\\' or '/'
local GLOB_PATTERN = '*.{jpg,jpeg,png,gif,bmp,ico,tiff,pnm,dds,tga}'

---@class BackDrops
Expand All @@ -31,7 +29,6 @@ function BackDrops:init()
focus_on = false,
}
local backdrops = setmetatable(inital, self)
wezterm.GLOBAL.background = nil
return backdrops
end

Expand All @@ -44,9 +41,7 @@ end
--- This throws a coroutine error if the function is invoked in outside of `wezterm.lua` in the -
--- initial load of the Terminal config.
function BackDrops:set_files()
self.files =
wezterm.glob(wezterm.config_dir .. PATH_SEP .. 'backdrops' .. PATH_SEP .. GLOB_PATTERN)
wezterm.GLOBAL.background = self.files[1]
self.files = wezterm.glob(wezterm.config_dir .. '/backdrops/' .. GLOB_PATTERN)
return self
end

Expand All @@ -58,27 +53,33 @@ function BackDrops:set_focus(focus_color)
return self
end

---Create the `background` options with the current image
---@return table
function BackDrops:create_opts()
return {
{
source = { File = self.files[self.current_idx] },
horizontal_align = 'Center',
},
{
source = { Color = colors.background },
height = '120%',
width = '120%',
vertical_offset = '-10%',
horizontal_offset = '-10%',
opacity = 0.96,
},
}
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 },
horizontal_align = 'Center',
},
{
source = { Color = colors.background },
height = '120%',
width = '120%',
vertical_offset = '-10%',
horizontal_offset = '-10%',
opacity = 0.96,
},
},
}
window:set_config_overrides(opts)
window:set_config_overrides({
background = self:create_opts(),
enable_tab_bar = window:effective_config().enable_tab_bar,
})
end

---Override the current window options for background with focus color
Expand All @@ -96,6 +97,7 @@ function BackDrops:_set_focus_opt(window)
opacity = 1,
},
},
enable_tab_bar = window:effective_config().enable_tab_bar,
}
window:set_config_overrides(opts)
end
Expand All @@ -113,12 +115,11 @@ function BackDrops:choices()
return choices
end

---Select a random file and redefine the global `wezterm.GLOBAL.background` variable
---Select a random background from the loaded `files`
---Pass in `Window` object to override the current window options
---@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)
Expand All @@ -133,7 +134,6 @@ function BackDrops:cycle_forward(window)
else
self.current_idx = self.current_idx + 1
end
wezterm.GLOBAL.background = self.files[self.current_idx]
self:_set_opt(window)
end

Expand All @@ -145,7 +145,6 @@ function BackDrops:cycle_back(window)
else
self.current_idx = self.current_idx - 1
end
wezterm.GLOBAL.background = self.files[self.current_idx]
self:_set_opt(window)
end

Expand All @@ -159,7 +158,6 @@ function BackDrops:set_img(window, idx)
end

self.current_idx = idx
wezterm.GLOBAL.background = self.files[self.current_idx]
self:_set_opt(window)
end

Expand Down

0 comments on commit d8bbb11

Please sign in to comment.