local M = {} --- Envuelve un binario (west, nrfutil...) en el entorno NCS. function M.ncs(opts, cmd) local full = { "nrfutil", "toolchain-manager", "launch" } if opts.ncs_version then vim.list_extend(full, { "--ncs-version", opts.ncs_version }) end vim.list_extend(full, { "--" }) vim.list_extend(full, cmd) return full end --- Lo único que NO pone `toolchain-manager launch` es ZEPHYR_BASE. function M.zephyr_env(opts) if opts.ncs_root and opts.ncs_version then local zephyr = opts.ncs_root .. "/" .. opts.ncs_version .. "/zephyr" if vim.fn.isdirectory(zephyr) == 1 then return { ZEPHYR_BASE = zephyr } end end return {} end --- Buffer de terminal reutilizable por nombre. local function prepare_term(name) local buf = vim.fn.bufnr(name) if buf ~= -1 then -- Si hay un job vivo en ese terminal, lo paramos antes de reusarlo. local job = vim.b[buf].terminal_job_id if job then pcall(vim.fn.jobstop, job) vim.wait(200, function() return vim.b[buf].terminal_job_id == nil end) end else buf = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(buf, name) vim.bo[buf].bufhidden = "hide" end vim.cmd("botright split") vim.api.nvim_win_set_buf(0, buf) -- dentro de prepare_term(name), justo antes del return vim.keymap.set("t", "", "", { buffer = buf, silent = true }) vim.keymap.set("t", "", function() local j = vim.b[buf].terminal_job_id if j then pcall(vim.fn.jobstop, j) end vim.cmd("stopinsert") end, { buffer = buf, silent = true, desc = "hego: detener proceso" }) return buf end --- Ejecuta en un buffer de terminal (interactivo, ANSI, progreso). --- Misma firma que la versión anterior: no hay que tocar las llamadas. function M.run_async(cmd, o) o = o or {} local buf = prepare_term(o.title or "hego://run") -- Fusionamos con el entorno heredado: jobstart REEMPLAZA si le pasas env. local env = vim.tbl_extend("force", vim.fn.environ(), o.env or {}) local job = vim.fn.jobstart(cmd, { term = true, cwd = o.cwd, env = env, on_exit = function(_, code) vim.schedule(function() vim.b[buf].hego_exit = code if o.on_exit then o.on_exit({ code = code }) end end) end, }) if job <= 0 then vim.notify("[hego] no se pudo lanzar el comando", vim.log.levels.ERROR) return end -- Arrancamos en terminal-insert; Esc-Esc para salir (LazyVim ya lo mapea). vim.cmd("startinsert") return job end return M