Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lua/wezterm-config/base64_encode.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
-- using relevant pieces of Ilya Kolbin's https://github.com/iskolbin/lbase64 under public domain
-- using Lua 5.1

---@class WeztermConfig.Base64Encode
local base64 = {}

---@param v integer
---@param from integer
---@param width integer
local function extract(v, from, width)
local w = 0
local flag = 2 ^ from
for i = 0, width - 1 do
local flag2 = flag + flag
local flag2 = flag * 2
if v % flag2 >= flag then
w = w + 2 ^ i
end
Expand All @@ -16,10 +20,13 @@ local function extract(v, from, width)
return w
end

---@param s62? string
---@param s63? string
---@param spad? string
---@return table<integer, integer> enocoder
function base64.makeencoder(s62, s63, spad)
local encoder = {}
for b64code, char in pairs({
[0] = 'A',
local characters = {
'A',
'B',
'C',
'D',
Expand Down Expand Up @@ -84,20 +91,25 @@ function base64.makeencoder(s62, s63, spad)
s62 or '+',
s63 or '/',
spad or '=',
}) do
encoder[b64code] = char:byte()
}
local encoder = {} ---@type table<integer, integer>
for b64code, char in ipairs(characters) do
encoder[b64code - 1] = char:byte()
end
return encoder
end

local DEFAULT_ENCODER = base64.makeencoder()
local char, concat = string.char, table.concat

---@param str string
---@param encoder? table<integer, integer>
---@param usecaching? boolean
function base64.encode(str, encoder, usecaching)
encoder = encoder or DEFAULT_ENCODER
local t, k, n = {}, 1, #str
local lastn = n % 3
local cache = {}
local t, k, n = {}, 1, str:len() ---@type string[], integer, integer
local lastn = n % 3 ---@type integer
local cache = {} ---@type table<integer, string>
for i = 1, n - lastn, 3 do
local a, b, c = str:byte(i, i + 2)
local v = a * 0x10000 + b * 0x100 + c
Expand Down
110 changes: 31 additions & 79 deletions lua/wezterm-config/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
local uv = vim.uv or vim.loop
local base64 = vim.base64 or require('wezterm-config.base64_encode')

---@class WeztermConfig
local M = {}

---For testing purposes only
Expand All @@ -13,38 +17,17 @@ function M.__test_bg_override()
opacity = 1.0,
},
}
value = vim.json.encode(value)

-- local vim_ver_ge_v0_10 = vim.version.ge(vim.version(), { 0, 10, 0 })
local uv
local base64
-- if vim_ver_ge_v0_10 == true then
-- uv = vim.uv
-- base64 = vim.base64
-- else
-- uv = vim.loop
-- base64 = require('wezterm-config.base64_encode')
-- end

if vim.uv then
uv = vim.uv
else
uv = vim.loop
end
local data = vim.json.encode(value)

if vim.base64 then
base64 = vim.base64
else
base64 = require('wezterm-config.base64_encode')
end
local stdout = uv.new_tty(1, false)
local value_b64_enc = base64.encode(value)
if not stdout then
return
end

local esc_seq
local value_b64_enc = base64.encode(data)
local esc_seq = [[\x1b]1337;SetUserVar=%s=%s\007]]
if os.getenv('TMUX') then
esc_seq = '\x1bPtmux;\x1b\x1b]1337;SetUserVar=%s=%s\007\x1b\\'
else
esc_seq = '\x1b]1337;SetUserVar=%s=%s\007'
esc_seq = [[\x1bPtmux;\x1b\x1b]1337;SetUserVar=%s=%s\007\x1b\\]]
end
stdout:write(esc_seq:format(name, value_b64_enc))
stdout:close()
Expand All @@ -59,51 +42,17 @@ function M.set_wezterm_user_var(name, value)
end

local value_type = type(value)
local value_out
if value_type == 'boolean' or value_type == 'number' then
value_out = vim.json.encode(tostring(value))
else
-- NOTE: remember that config.background is like { { source = { File = '...' } }, ... }
-- looks like the outermost pair(s) of curly braces get converted/interpreted as array []
-- by vim.json.encode()
value_out = vim.json.encode(value)
end
-- NOTE: remember that config.background is like { { source = { File = '...' } }, ... }
-- looks like the outermost pair(s) of curly braces get converted/interpreted as array []
-- by vim.json.encode()
local data = vim.list_contains({ 'boolean', 'number' }, value_type) and tostring(value) or value
local value_out = vim.json.encode(data)

-- NOTE: v0.10 adds vim.version.ge() and vim.version.le() so shouldn't use it yet since v0.10 isn't out
-- NOTE: v0.10 renames vim.loop to vim.uv and vim.base64 module is added
-- https://neovim.io/doc/user/news.html
-- local vim_ver_ge_v0_10 = vim.version.ge(vim.version(), { 0, 10, 0 })
local uv
local base64
-- if vim_ver_ge_v0_10 == true then
-- uv = vim.uv
-- base64 = vim.base64
-- else
-- uv = vim.loop
-- base64 = require('wezterm-config.base64_encode')
-- end

-- if vim.uv then
-- uv = vim.uv
-- else
-- uv = vim.loop
-- end
--
-- if vim.base64 then
-- base64 = vim.base64
-- else
-- base64 = require('wezterm-config.base64_encode')
-- end

if vim.version().minor >= 10 then
uv = vim.uv
base64 = vim.base64
else
uv = vim.loop
base64 = require('wezterm-config.base64_encode')
local stdout = uv.new_tty(1, false)
if not stdout then
return
end

local stdout = uv.new_tty(1, false)
local value_b64_enc = base64.encode(value_out)

-- people have asked Wez about stuff like this before, to which he's linked
Expand All @@ -112,31 +61,34 @@ function M.set_wezterm_user_var(name, value)
-- https://github.com/folke/zen-mode.nvim/pull/61 which itself references a Reddit thread
-- https://www.reddit.com/r/neovim/comments/xn1q75/how_to_use_chansend/
-- Folke has kindly allowed me to adapt the code here
local esc_seq
local esc_seq = [[\x1b]1337;SetUserVar=%s=%s\007]]
if os.getenv('TMUX') then
-- unclear to me why using \033 isn't interpreted the same as \x1b
-- there are some files in nvim that seem like they could explain or have
-- something to do with nvim-specific interpretation, but I don't understand them
esc_seq = '\x1bPtmux;\x1b\x1b]1337;SetUserVar=%s=%s\007\x1b\\'
else
esc_seq = '\x1b]1337;SetUserVar=%s=%s\007'
esc_seq = [[\x1bPtmux;\x1b\x1b]1337;SetUserVar=%s=%s\007\x1b\\]]
end
stdout:write(esc_seq:format(name, value_b64_enc))
stdout:close()
end

---Setup plugin
---@param config table | nil
---@param config? WeztermConfigOpts
function M.setup(config)
---@class WeztermConfigOpts
local default_config = {
append_wezterm_to_rtp = false,
}
M.config = vim.tbl_deep_extend('force', default_config, config or {})

if M.config.append_wezterm_to_rtp == true then
-- vim.fn.stdpath('config') is typically $HOME/.config/nvim
local wezterm_config = vim.fn.stdpath('config'):gsub('nvim', 'wezterm')
vim.opt.rtp:append(wezterm_config)
if not M.config.append_wezterm_to_rtp then
return
end

-- vim.fn.stdpath('config') is typically $HOME/.config/nvim
local wezterm_config = vim.fn.stdpath('config'):gsub('nvim', 'wezterm')
if not vim.list_contains(vim.split(vim.o.rtp, ',', { trimempty = true, plain = true }), wezterm_config) then
vim.o.rtp = vim.o.rtp .. (vim.o.rtp:sub(-1) == ',' and ',' or '') .. wezterm_config
end
end

Expand Down
13 changes: 8 additions & 5 deletions plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
-- simple workaround to avoid nvim trying to load this module
-- https://github.com/wez/wezterm/issues/4533#issuecomment-1874094722
-- is there a better alt?
if vim ~= nil then
if vim then
return
end

local wezterm = require('wezterm')
local wezterm = require('wezterm') ---@type Wezterm
local M = {}

---@param var string
Expand All @@ -30,10 +30,10 @@ end
---make the appropriate changes to the given overrides table;
---for use within a callback function in Wezterm config
---for the 'user-var-changed' event
---@param overrides table
---@param overrides table<string, boolean|string|number|table>
---@param name string
---@param value string
---@return table
---@return table<string, boolean|string|number|table> overrides
function M.override_user_var(overrides, name, value)
if not is_shell_integ_user_var(name) then
-- returns tbl if successfully parsed
Expand All @@ -49,17 +49,20 @@ function M.override_user_var(overrides, name, value)

local parsed_val = wezterm.json_parse(value)
if type(parsed_val) == 'table' then
---@cast parsed_val table
overrides[name] = parsed_val
else
---@cast parsed_val string
if parsed_val == 'true' or parsed_val == 'false' then
-- convert to bool
parsed_val = parsed_val == 'true'
elseif string.match(parsed_val, '^%d*%.?%d+$') then
elseif parsed_val:match('^%d*%.?%d+$') then
-- convert to number
parsed_val = tonumber(parsed_val)
end
overrides[name] = parsed_val
end
overrides[name] = parsed_val
end
return overrides
end
Expand Down