Repository navigation
Expand file tree
/
Copy pathwezterm.lua
More file actions
120 lines (102 loc) · 3.21 KB
/
Copy pathwezterm.lua
File metadata and controls
120 lines (102 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
local wezterm = require("wezterm")
local mux = wezterm.mux
local config = wezterm.config_builder()
--- Escanea `apps/` y retorna una lista de rutas absolutas a subdirectorios.
--- @param root string ruta raiz del monorepo
--- @return string[]
local function discover_apps(root)
local apps_dir = root .. "/apps"
local handle = io.popen('ls -1d "' .. apps_dir .. '"/*/ 2>/dev/null')
if not handle then
return {}
end
local dirs = {}
for line in handle:lines() do
-- Eliminar trailing slash
local dir = line:gsub("/$", "")
table.insert(dirs, dir)
end
handle:close()
return dirs
end
--- Divide los panes en un grid
--- n=1 -> 1 solo pane (sin splits)
--- n=2 -> 2 columnas
--- n=3 -> fila superior 2 cols, fila inferior 1
--- n=4 -> 2x2
--- n=5 -> fila superior 3, fila inferior 2
--- n=6 -> 3x2
--- Retorna una lista ordenada de panes listos para recibir comandos.
--- @param first_pane userdata el pane inicial de la ventana
--- @param n integer cantidad total de apps
--- @return userdata[]
local function make_grid(first_pane, n)
if n <= 0 then
return {}
end
if n == 1 then
return { first_pane }
end
-- Calcular filas y columnas
local rows = math.ceil(math.sqrt(n))
local cols = math.ceil(n / rows)
local row_panes = { first_pane }
for _ = 2, rows do
local last_row = row_panes[#row_panes]
local new = last_row:split({ direction = "Bottom" })
table.insert(row_panes, new)
end
-- Para cada fila, calcular cuantas columnas le corresponden y hacer splits verticales.
local all_panes = {}
local assigned = 0
for i, row_pane in ipairs(row_panes) do
local cols_in_row = math.min(cols, n - assigned)
if i < rows then
cols_in_row = cols
end
local col_panes = { row_pane }
for _ = 2, cols_in_row do
local last_col = col_panes[#col_panes]
local new = last_col:split({ direction = "Right" })
table.insert(col_panes, new)
end
for _, p in ipairs(col_panes) do
table.insert(all_panes, p)
assigned = assigned + 1
end
end
return all_panes
end
-- Interceptar el startup de la GUI
wezterm.on("gui-startup", function(cmd)
local target_dir = os.getenv("WEZTERM_TARGET_DIR")
local dev_cmd = os.getenv("WEZTERM_DEV_CMD") or "pnpm dev"
-- Fallback
if not target_dir or target_dir == "" then
mux.spawn_window(cmd or {})
return
end
local apps = discover_apps(target_dir)
-- Si no se encuentran apps, abrir en el directorio raiz sin splits.
if #apps == 0 then
mux.spawn_window({ cwd = target_dir })
return
end
-- Crear la ventana con el cwd de la primera app.
local tab, first_pane, window = mux.spawn_window({ cwd = apps[1] })
window:gui_window():maximize()
-- Generar grid y asignar cada app a un pane.
local panes = make_grid(first_pane, #apps)
for i, pane in ipairs(panes) do
local app_dir = apps[i]
if i > 1 then
pane:send_text("cd " .. wezterm.shell_quote_arg(app_dir) .. "\n")
end
-- Inyectar el nombre de la app como titulo y ejecutar el comando.
local app_name = app_dir:match("([^/]+)$")
pane:send_text("printf '\\e]0;" .. app_name .. "\\a'\n")
pane:send_text(dev_cmd .. "\n")
end
end)
config.default_cwd = wezterm.home_dir
return config