Neovim sign gutter, designed to be mostly VCS agnostic
1local M = {}
2
3local logging = require "vclib.logging"
4
5local DEFAULT_TIMEOUT_MS = 2000
6local TERMINAL_WIDTH = 10000
7
8--- Print a message to the user if verbose mode is enabled.
9M.verbose = logging.verbose_logger "vcsigns"
10
11function M.run_with_timeout(cmd, opts, callback)
12 M.verbose("Running command: " .. table.concat(cmd, " "))
13 local merged_opts = vim.tbl_deep_extend(
14 "force",
15 { timeout = DEFAULT_TIMEOUT_MS, env = { COLUMNS = TERMINAL_WIDTH } },
16 opts
17 )
18 if callback == nil then
19 return vim.system(cmd, merged_opts)
20 end
21
22 return vim.system(cmd, merged_opts, function(out)
23 if out.code == 124 then
24 M.verbose("Command timed out: " .. table.concat(cmd, " "))
25 return
26 end
27 vim.schedule(function()
28 callback(out)
29 end)
30 end)
31end
32
33function M.file_dir(bufnr)
34 return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":p:h")
35end
36
37--- Slice a table to get a subtable.
38---@param tbl table The table to slice.
39---@param start integer The starting index (1-based).
40---@param count integer The number of elements to take.
41---@return table A new table containing the sliced elements.
42function M.slice(tbl, start, count)
43 local result = {}
44 for i = start, start + count - 1 do
45 if i >= 1 and i <= #tbl then
46 table.insert(result, tbl[i])
47 end
48 end
49 return result
50end
51
52return M