Skip to content

Commit 782f0c8

Browse files
committed
Added font support
Needs testing before merge
1 parent 9ce616b commit 782f0c8

2 files changed

Lines changed: 107 additions & 35 deletions

File tree

README.md

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,63 @@ Using [folke/lazy.nvim](https://github.com/folke/lazy.nvim)
3030

3131
### Wezterm
3232

33-
Wezterm has a [built-in system for incorporating remote plugins](https://github.com/wez/wezterm/commit/e4ae8a844d8feaa43e1de34c5cc8b4f07ce525dd). Place the following in your Wezterm config file to get the Wezterm side of the plugin running. This will pull down the repo into your local Wezterm plugin directory.
33+
You can either integrate the plugin directly in your `wezterm.lua` or keep it in a separate file.
34+
35+
#### Option 1: Direct Integration
36+
37+
In your `wezterm.lua`:
3438

3539
```lua
3640
local wezterm = require('wezterm')
37-
local wezterm_config_nvim = wezterm.plugin.require('https://github.com/winter-again/wezterm-config.nvim')
38-
-- rest of your config
39-
```
41+
local config = {}
4042

41-
Crucially, add this snippet so that Wezterm will know how to respond to the config overrides that the Neovim side will send:
43+
-- Import the override_user_var function from the plugin
44+
local override_user_var = require('plugin/init').override_user_var
4245

43-
```lua
44-
wezterm.on('user-var-changed', function(window, pane, name, value)
46+
wezterm.on("user-var-changed", function(window, pane, name, value)
4547
local overrides = window:get_config_overrides() or {}
46-
overrides = wezterm_config_nvim.override_user_var(overrides, name, value)
48+
overrides = override_user_var(overrides, name, value)
4749
window:set_config_overrides(overrides)
4850
end)
51+
52+
return config
4953
```
5054

51-
### Putting it all together
55+
#### Option 2: Separate Configuration
5256

53-
Simple key-value style (like `config.font_size` or `config.hide_tab_bar_if_only_one_tab`) config overrides should work out-of-the-box. Here's an example of how to override Wezterm's font size from inside of Neovim. Note how the first argument to `require('wezterm-config').set_wezterm_user_var()` is simply the name of the corresponding config option in [Wezterm's config struct](https://wezfurlong.org/wezterm/config/lua/config/index.html):
57+
If you prefer to keep the plugin configuration separate, create a file named `wezterm_plugin.lua`:
58+
59+
```lua
60+
local wezterm = require('wezterm')
61+
local M = {}
62+
63+
M.override_user_var = require('plugin/init').override_user_var
64+
65+
M.setup = function(config)
66+
wezterm.on("user-var-changed", function(window, pane, name, value)
67+
local overrides = window:get_config_overrides() or {}
68+
overrides = M.override_user_var(overrides, name, value)
69+
window:set_config_overrides(overrides)
70+
end)
71+
return config
72+
end
73+
74+
return M
75+
```
76+
77+
Then in your main `wezterm.lua`:
78+
79+
```lua
80+
local wezterm = require('wezterm')
81+
local config = {}
82+
83+
local wezterm_plugin = require('wezterm_plugin')
84+
config = wezterm_plugin.setup(config)
85+
86+
return config
87+
```
88+
89+
### Usage Examples
5490

5591
```lua
5692
-- in Neovim
@@ -102,13 +138,44 @@ return M
102138

103139
## Tips
104140

105-
You can use Wezterm's built-in functionality for updating plugins. If you have `config.automatically_reload_config` set to true (the default), then the plugin *should* be updated on startup and/or on saving your config. Otherwise, you could also set a keymap to trigger reloading the config and update plugins:
141+
For more complex configuration options that take Lua tables as their values (like `background`), you can pass them as JSON strings:
106142

107143
```lua
108144
wezterm.plugin.update_all()
109145
```
110146

111-
You might find it helpful to be able to clear your config overrides, especially if there's been a mistake in an override resulting in some internal Wezterm error or you just want to restore defaults. This is how you can setup a Wezterm keymap to do this:
147+
### tmux
148+
149+
The plugin should play nicely with [tmux](https://github.com/tmux/tmux). Make sure the following setting is in your tmux conf file, [as advised by Wez](https://wezfurlong.org/wezterm/recipes/passing-data.html#user-vars).
150+
151+
```
152+
set -g allow-passthrough on
153+
```
154+
155+
## Debugging
156+
157+
### Logging
158+
159+
To enable detailed logging for debugging, you can modify the event handler to include logging statements:
160+
161+
```lua
162+
wezterm.on("user-var-changed", function(window, pane, name, value)
163+
local overrides = window:get_config_overrides() or {}
164+
wezterm.log_info("--- User Var Changed ---")
165+
wezterm.log_info("Name:", name, "| Value:", value, "| Type of value:", type(value))
166+
wezterm.log_info("Overrides before change:", overrides)
167+
168+
overrides = override_user_var(overrides, name, value)
169+
170+
wezterm.log_info("Final overrides before applying to window:", overrides)
171+
window:set_config_overrides(overrides)
172+
wezterm.log_info("--- End User Var Changed ---")
173+
end)
174+
```
175+
176+
### Clearing Overrides
177+
178+
You might want to clear your config overrides, especially if there's been a mistake in an override. Add this to your Wezterm config:
112179

113180
```lua
114181
wezterm.on('clear-overrides', function(window, pane)
@@ -128,11 +195,3 @@ local override_keymap = {
128195
table.insert(config.keys, override_keymap)
129196
```
130197

131-
### tmux
132-
133-
The plugin should play nicely with [tmux](https://github.com/tmux/tmux). Make sure the following setting is in your tmux conf file, [as advised by Wez](https://wezfurlong.org/wezterm/recipes/passing-data.html#user-vars).
134-
135-
```
136-
set -g allow-passthrough on
137-
```
138-

plugin/init.lua

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
-- https://github.com/wez/wezterm/issues/4533#issuecomment-1874094722
44
-- is there a better alt?
55
if vim ~= nil then
6-
return
6+
return
77
end
88

9-
local wezterm = require('wezterm')
9+
local wezterm = require("wezterm")
1010
local M = {}
1111

12+
local function trim_quotes(s)
13+
return (s or ""):gsub("^['\"](.-)['\"]$", "%1")
14+
end
15+
1216
---@param var string
1317
---@return boolean
1418
local function is_shell_integ_user_var(var)
15-
local shell_integ_user_vars = {
16-
'WEZTERM_PROG',
17-
'WEZTERM_USER',
18-
'WEZTERM_HOST',
19-
'WEZTERM_IN_TMUX',
20-
}
21-
for _, val in ipairs(shell_integ_user_vars) do
22-
if val == var then
23-
return true
24-
end
25-
end
26-
return false
19+
local shell_integ_user_vars = {
20+
WEZTERM_PROG = true,
21+
WEZTERM_USER = true,
22+
WEZTERM_HOST = true,
23+
WEZTERM_IN_TMUX = true,
24+
}
25+
return shell_integ_user_vars[var] == true
2726
end
2827

2928
---Interpret the Wezterm user var that is passed in and
@@ -52,8 +51,22 @@ function M.override_user_var(overrides, name, value)
5251
end
5352
overrides[name] = parsed_val
5453
end
55-
end
5654
return overrides
55+
56+
if name == "font" then
57+
local cleaned = trim_quotes(value)
58+
local success, font_obj = pcall(wezterm.font, cleaned)
59+
if success and font_obj then
60+
if font_obj.font and font_obj.font[1] and font_obj.font[1].family then
61+
font_obj.font[1].family = trim_quotes(font_obj.font[1].family)
62+
end
63+
overrides.font = font_obj
64+
wezterm.log_info("Applied FONT override. Cleaned value:", cleaned)
65+
else
66+
wezterm.log_error("Failed to create font object from sanitized input:", cleaned)
67+
end
68+
return overrides
69+
end
5770
end
5871

5972
return M

0 commit comments

Comments
 (0)