REFACTOR: I spent all day on it but I am heavily refactoring the API and
organization because it was terrible before and hard to maintain. Now it should be much easier and there is still of a design decisions to figure out
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
local M = {}
|
||||
|
||||
---@param bufnr? integer Default is 0 (current buffer)
|
||||
---@return string|nil
|
||||
function M.get_project_root(bufnr)
|
||||
bufnr = bufnr or 0
|
||||
local root_markers = {
|
||||
".git",
|
||||
"pyproject.toml",
|
||||
"setup.py",
|
||||
"Makefile",
|
||||
"CMakeLists",
|
||||
"Cargo.toml",
|
||||
}
|
||||
return vim.fs.root(bufnr, root_markers) or vim.uv.cwd()
|
||||
end
|
||||
---
|
||||
---@param cmd table
|
||||
---@return integer, integer?
|
||||
function M.terminal(cmd, opts)
|
||||
opts = opts or {}
|
||||
if opts.terminal.mode == "floating" then
|
||||
local ui = vim.api.nvim_list_uis()[1]
|
||||
local width, height, row, col
|
||||
|
||||
if opts.terminal ~= nil then
|
||||
width = math.max(
|
||||
1,
|
||||
math.floor(ui.width * 0.9)
|
||||
- opts.terminal.left
|
||||
- opts.terminal.right
|
||||
)
|
||||
height = math.max(
|
||||
1,
|
||||
math.floor(ui.height * 0.9)
|
||||
- opts.terminal.top
|
||||
- opts.terminal.bottom
|
||||
)
|
||||
row = math.floor((ui.height - height) / 2) + opts.terminal.top
|
||||
col = math.floor((ui.width - width) / 2) + opts.terminal.left
|
||||
end
|
||||
if opts.terminal == nil then
|
||||
width = math.max(1, math.floor(ui.width * 0.9))
|
||||
height = math.max(1, math.floor(ui.height * 0.9))
|
||||
row = math.floor((ui.height - height) / 2)
|
||||
col = math.floor((ui.width - width) / 2)
|
||||
end
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
local win = vim.api.nvim_open_win(buf, true, {
|
||||
relative = "editor",
|
||||
width = width,
|
||||
height = height,
|
||||
row = row,
|
||||
col = col,
|
||||
style = "minimal",
|
||||
border = opts.terminal.border.enabled and opts.terminal.border.type,
|
||||
})
|
||||
vim.api.nvim_set_option_value(
|
||||
"winhighlight",
|
||||
"Normal:PolydevNormal,FloatBorder:PolydevBorder",
|
||||
{ win = win }
|
||||
)
|
||||
vim.fn.jobstart(table.concat(cmd, "/"), { term = true })
|
||||
vim.api.nvim_buf_set_keymap(
|
||||
buf,
|
||||
"n",
|
||||
"<ESC>",
|
||||
"i<C-\\><C-n>:q<CR>",
|
||||
{ noremap = true, silent = true }
|
||||
)
|
||||
return buf, win
|
||||
end
|
||||
if opts.terminal.mode == "split" then
|
||||
if opts.terminal.win.type == "vertical" then
|
||||
if opts.terminal.win.anchor == "right" then
|
||||
vim.cmd("set splitright")
|
||||
end
|
||||
end
|
||||
if opts.terminal.win.type == "horizontal" then
|
||||
if opts.terminal.win.anchor == "bottom" then
|
||||
vim.cmd("set splitbelow")
|
||||
end
|
||||
end
|
||||
|
||||
if opts.terminal.win.type == "vertical" then
|
||||
vim.cmd(opts.terminal.win.size .. " vsplit")
|
||||
end
|
||||
if opts.terminal.win.type == "horizontal" then
|
||||
vim.cmd(opts.terminal.win.size .. " split")
|
||||
end
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
|
||||
vim.api.nvim_win_set_buf(0, buf)
|
||||
|
||||
vim.fn.jobstart(table.concat(cmd, "/"), { term = true })
|
||||
vim.api.nvim_buf_set_keymap(
|
||||
buf,
|
||||
"n",
|
||||
"<ESC>",
|
||||
"i<C-\\><C-n>:q<CR>",
|
||||
{ noremap = true, silent = true }
|
||||
)
|
||||
return buf, win
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
-- M.opts = {}
|
||||
--
|
||||
-- function M.setup(opts)
|
||||
-- M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {})
|
||||
-- end
|
||||
--
|
||||
-- function M.exists(path)
|
||||
-- return (io.open(path, "r") ~= nil)
|
||||
-- end
|
||||
--
|
||||
--
|
||||
-- ---@return string
|
||||
-- function M.get_project_name()
|
||||
-- return vim.fn.fnamemodify(M.get_project_root(), ":t")
|
||||
-- end
|
||||
--
|
||||
-- ---@param path_segments table
|
||||
-- ---@param content string
|
||||
-- function M.write_file(path_segments, content)
|
||||
-- local path = table.concat(path_segments, "/")
|
||||
-- local file = assert(io.open(path, "w"), "Error creating file: " .. path)
|
||||
-- file:write(content)
|
||||
-- file:close()
|
||||
-- vim.cmd("edit " .. path)
|
||||
-- end
|
||||
--
|
||||
-- ---@param path string
|
||||
-- ---@param gitignore_lines table
|
||||
-- function M.init_git(path, gitignore_lines)
|
||||
-- vim.fn.mkdir(path, "p")
|
||||
-- vim.fn.system({ "git", "-C", path, "init" })
|
||||
--
|
||||
-- if gitignore_lines and #gitignore_lines > 0 then
|
||||
-- local contents = table.concat(gitignore_lines, "\n")
|
||||
-- M.write_file({ path, ".gitignore" }, contents)
|
||||
-- end
|
||||
-- end
|
||||
----
|
||||
-- return M
|
||||
Reference in New Issue
Block a user