local overseer = require("overseer") -------------------------------------------------------------------------------- -- Core builder -------------------------------------------------------------------------------- local function build_task(params) local executable = vim.fn.fnamemodify(params.executable, ":p") local watch_dir = vim.fn.fnamemodify(executable, ":h") local basename = vim.fn.fnamemodify(executable, ":t") if vim.fn.filereadable(executable) == 0 then vim.notify("Executable not found: " .. executable, vim.log.levels.ERROR) return nil end if vim.fn.executable(executable) ~= 1 then vim.notify("File is not executable: " .. executable, vim.log.levels.ERROR) return nil end local script = string.format( [[ #!/bin/bash set -euo pipefail set -m EXECUTABLE="%s" WATCH_DIR="%s" BASENAME="%s" echo "[overseer] Executable: $EXECUTABLE" echo "[overseer] Watch dir: $WATCH_DIR" echo "[overseer] Basename: $BASENAME" cleanup() { if [ -n "${PID:-}" ] && kill -0 "$PID" 2>/dev/null; then echo "[overseer] Stopping process group $PID..." kill -TERM -"$PID" 2>/dev/null || true for i in {1..20}; do kill -0 "$PID" 2>/dev/null || break sleep 0.1 done if kill -0 "$PID" 2>/dev/null; then kill -KILL -"$PID" 2>/dev/null || true fi wait "$PID" 2>/dev/null || true fi PID="" } trap cleanup EXIT run() { echo "[overseer] >>> Starting $BASENAME..." "$EXECUTABLE" & PID=$! } run exec 3< <(inotifywait -m -e close_write,moved_to --format '%%f' "$WATCH_DIR") INOTIFY_PID=$! trap 'kill $INOTIFY_PID 2>/dev/null || true; wait $INOTIFY_PID 2>/dev/null || true; cleanup' EXIT while read -r file <&3; do if [ "$file" = "$BASENAME" ]; then sleep 0.1 cleanup run fi done kill $INOTIFY_PID 2>/dev/null || true wait $INOTIFY_PID 2>/dev/null || true echo "[overseer] Watch ended" ]], executable, watch_dir, basename ) return { cmd = { "bash", "-c", script }, cwd = watch_dir, name = string.format("Run & Watch [%s]", basename), components = { "default", { "on_output_quickfix", open = false, close = true }, }, } end -------------------------------------------------------------------------------- -- Scan for executables -------------------------------------------------------------------------------- local function scan_executables(root) local candidates = {} local seen = {} local function try_add(path) if not seen[path] and vim.fn.filereadable(path) == 1 and vim.fn.executable(path) == 1 then seen[path] = true table.insert(candidates, path) end end -- Root directory (non-recursive) local handle = vim.uv.fs_scandir(root) if handle then while true do local name, typ = vim.uv.fs_scandir_next(handle) if not name then break end if typ == "file" then try_add(root .. "/" .. name) end end end -- Common build directories (depth 1) for _, sub in ipairs({ "build", "bin", "target/debug", "target/release", "cmake-build-debug", "cmake-build-release", "out", "dist", }) do local dir = root .. "/" .. sub if vim.fn.isdirectory(dir) == 1 then local h = vim.uv.fs_scandir(dir) if h then while true do local name, typ = vim.uv.fs_scandir_next(h) if not name then break end if typ == "file" then try_add(dir .. "/" .. name) end end end end end -- Newest first table.sort(candidates, function(a, b) local sa = vim.uv.fs_stat(a) local sb = vim.uv.fs_stat(b) if not sa or not sb then return false end return sa.mtime.sec > sb.mtime.sec end) while #candidates > 8 do table.remove(candidates) end return candidates end -------------------------------------------------------------------------------- -- Template provider -------------------------------------------------------------------------------- local provider = { generator = function(search) local templates = {} local cwd = search.dir or vim.fn.getcwd() for _, exe in ipairs(scan_executables(cwd)) do local name = vim.fn.fnamemodify(exe, ":t") table.insert(templates, { name = string.format("Run & Watch [%s]", name), desc = string.format("Run %s and restart when the binary changes", exe), builder = function() return build_task({ executable = exe }) end, tags = { overseer.TAG.RUN }, }) end table.insert(templates, { name = "Run & Watch [Custom]", desc = "Provide the path to an executable to run and watch", builder = function(params) return build_task(params) end, params = { executable = { desc = "Path to executable", type = "string", order = 1, validate = function(v) if v == "" or v == nil then return false, "Executable path is required" end local p = vim.fn.fnamemodify(v, ":p") if vim.fn.filereadable(p) == 0 then return false, "File does not exist" end if vim.fn.executable(p) ~= 1 then return false, "File is not executable" end return true end, }, }, tags = { overseer.TAG.RUN }, }) return templates end, } -------------------------------------------------------------------------------- -- Telescope integration -------------------------------------------------------------------------------- provider.pick_executable_and_run = function() local has_telescope, telescope = pcall(require, "telescope.builtin") if not has_telescope then vim.notify("Telescope.nvim is required", vim.log.levels.ERROR) return end telescope.find_files({ prompt_title = "Select executable to run & watch", find_command = { "find", ".", "-maxdepth", "3", "-type", "f", "-executable" }, attach_mappings = function(prompt_bufnr, _) local actions = require("telescope.actions") local action_state = require("telescope.actions.state") actions.select_default:replace(function() actions.close(prompt_bufnr) local selection = action_state.get_selected_entry() if not selection then return end local path = vim.fn.fnamemodify(selection.path or selection.value, ":p") local task_defn = build_task({ executable = path }) if not task_defn then return end local task = overseer.new_task(task_defn) task:start() end) return true end, }) end return provider