irongit

My wezterm configuration. Allows workspace configurations with environment variables for each new workspace pane.

105 lines2.6 KBLua
1local wezterm = require("wezterm")
2local mux = wezterm.mux
3local act = wezterm.action
4local manager = {}
5
6local M = {}
7
8-- Generates a string of environment variable exports for a given workspace.
9--
10-- Parameters:
11-- ws (string): The workspace identifier used to retrieve environment variables.
12--
13-- Returns:
14-- string: A concatenated string of export statements for the environment variables.
15function M.env_text(ws)
16 local text = ""
17 if manager[ws].env then
18 for key, val in pairs(manager[ws].env) do
19 text = text .. "export " .. key .. "='" .. val .. "';"
20 end
21 end
22 return text
23end
24
25function M.add_env(ws, pane)
26 pane:send_text(M.env_text(ws) .. "\n")
27end
28
29function M.SplitWithEnv(direction)
30 return wezterm.action_callback(function(win, pane)
31 pane = pane:split({ direction = direction })
32 local ws = win:get_workspace()
33 if manager[ws] and manager[ws].env then
34 pane.send_text(M.env_text(ws) .. "\n")
35 end
36 end)
37end
38
39function M.add_keys(config)
40 table.insert(config.keys, {
41 key = '"',
42 mods = "CTRL|SHIFT",
43 action = M.SplitWithEnv("Bottom"),
44 })
45 table.insert(config.keys, {
46 key = "%",
47 mods = "CTRL|SHIFT",
48 action = M.SplitWithEnv("Right"),
49 })
50end
51
52local function load_pane(ws, pane, pane_config)
53 M.add_env(ws, pane)
54 pane:send_text("clear\n")
55 if pane_config.init then
56 pane:send_text(table.concat(pane_config.init, ";") .. "\n")
57 end
58end
59
60local function load_tab(ws, tab, tab_config)
61 load_pane(ws, tab:active_pane(), tab_config)
62 if tab_config.extra_panes then
63 for _, pane_config in ipairs(tab_config.extra_panes) do
64 local pane = tab:active_pane():split({ cwd = pane_config.cwd or tab_config.cwd or manager[ws].cwd })
65 load_pane(ws, pane, pane_config)
66 end
67 end
68end
69
70local function load_workspace(modname, ws)
71 local config = require(modname)
72 manager[ws] = config
73 local tab, _, window = mux.spawn_window({
74 cwd = config.cwd,
75 workspace = ws,
76 })
77 load_tab(ws, tab, config)
78 if config.extra_tabs then
79 for _, tab_config in ipairs(config.extra_tabs) do
80 tab, _, _ = window:spawn_tab({ cwd = tab_config.cwd or config.cwd })
81 load_tab(ws, tab, tab_config)
82 end
83 end
84end
85
86local function load_workspaces()
87 local wdir = wezterm.home_dir .. "/.config/wezterm/workspaces"
88 for _, v in ipairs(wezterm.read_dir(wdir)) do
89 -- setup modname agnostic of file system
90 local modname = v:match(".*%.config/wezterm/(.*)%.lua"):gsub("/", "."):gsub("\\", ".")
91 local wname = modname:match(".*%.(.*)")
92 if wname ~= "init" then
93 load_workspace(modname, wname)
94 end
95 end
96end
97
98function M.setup(config)
99 M.add_keys(config)
100 wezterm.on("gui-startup", function()
101 load_workspaces()
102 end)
103end
104
105return M