-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
60 lines (52 loc) · 1.52 KB
/
utils.lua
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
local M = {}
function M.vim_opt_toggle(opt, on, off, name)
if on == nil then on = true end
if off == nil then off = false end
if not name then name = opt end
local is_off = vim.opt[opt]:get() == off
vim.opt[opt] = is_off and on or off
require("astronvim.utils").notify(name .. " " .. (is_off and "Enabled" or "Disabled"))
end
function M.async_run(cmd, on_finish)
local lines = { "" }
local function on_event(_, data, event)
if (event == "stdout" or event == "stderr") and data then vim.list_extend(lines, data) end
if event == "exit" then
vim.fn.setqflist({}, " ", {
title = table.concat(cmd, " "),
lines = lines,
efm = "%f:%l:%c: %t%n %m",
})
if on_finish then on_finish() end
end
end
vim.fn.jobstart(cmd, {
on_stdout = on_event,
on_stderr = on_event,
on_exit = on_event,
stdout_buffered = true,
stderr_buffered = true,
})
end
function M.toggle_qf()
local qf_exists = false
for _, win in pairs(vim.fn.getwininfo()) do
if win["quickfix"] == 1 then
qf_exists = true
break
end
end
if qf_exists then
vim.cmd.cclose()
elseif not vim.tbl_isempty(vim.fn.getqflist()) then
vim.cmd.copen()
end
end
function M.better_search(key)
return function()
local searched, error =
pcall(vim.cmd.normal, { args = { (vim.v.count > 0 and vim.v.count or "") .. key }, bang = true })
if not searched and type(error) == "string" then require("astronvim.utils").notify(error, vim.log.levels.ERROR) end
end
end
return M