My wezterm configuration. Allows workspace configurations with environment variables for each new workspace pane.
| 1 | local wezterm = require("wezterm") |
| 2 | local mux = wezterm.mux |
| 3 | local act = wezterm.action |
| 4 | local manager = {} |
| 5 | |
| 6 | local 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. |
| 15 | |
| 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 |
| 23 | end |
| 24 | |
| 25 | |
| 26 | pane:send_text(M.env_text(ws) .. "\n") |
| 27 | end |
| 28 | |
| 29 | |
| 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) |
| 37 | end |
| 38 | |
| 39 | |
| 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 | }) |
| 50 | end |
| 51 | |
| 52 | local |
| 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 |
| 58 | end |
| 59 | |
| 60 | local |
| 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 |
| 68 | end |
| 69 | |
| 70 | local |
| 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 |
| 84 | end |
| 85 | |
| 86 | local |
| 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 |
| 96 | end |
| 97 | |
| 98 | |
| 99 | M.add_keys(config) |
| 100 | wezterm.on("gui-startup", function() |
| 101 | load_workspaces() |
| 102 | end) |
| 103 | end |
| 104 | |
| 105 | return M |