Added Presets for terminal placements

This commit is contained in:
DarthMooMancer
2025-04-18 23:54:43 -04:00
parent 11ecd94afd
commit 71bc395125
7 changed files with 147 additions and 7 deletions
+2
View File
@@ -10,6 +10,7 @@ local M = {}
---@field build_attributes? string ---@field build_attributes? string
--- ---
---@class TerminalConfigs ---@class TerminalConfigs
---@field presets string
---@field right_padding integer ---@field right_padding integer
---@field left_padding integer ---@field left_padding integer
---@field bottom_padding integer ---@field bottom_padding integer
@@ -79,6 +80,7 @@ M.defaults = {
build_attributes = "", build_attributes = "",
}, },
terminal = { terminal = {
presets = "center",
right_padding = 0, right_padding = 0,
bottom_padding = 0, bottom_padding = 0,
left_padding = 0, left_padding = 0,
+2
View File
@@ -40,6 +40,8 @@ function M.run()
local function get_project_name() local function get_project_name()
return vim.fn.glob((M.get_project_root() or "") .. "/*.polydev"):match("([^/]+)%.polydev$") return vim.fn.glob((M.get_project_root() or "") .. "/*.polydev"):match("([^/]+)%.polydev$")
end end
---@return string
if not get_project_root() or not get_project_name() then return print("Error: Project root not found.") end if not get_project_root() or not get_project_name() then return print("Error: Project root not found.") end
local init_lua_path = get_project_root() .. "/lua/" .. get_project_name() .. "/init.lua" local init_lua_path = get_project_root() .. "/lua/" .. get_project_name() .. "/init.lua"
if vim.fn.filereadable(init_lua_path) == 1 then if vim.fn.filereadable(init_lua_path) == 1 then
+14
View File
@@ -1,10 +1,19 @@
---@module "Polydev.utils"
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
---@module "Polydev.configs"
local config = require("Polydev.configs") local config = require("Polydev.configs")
---@tyep table
local M = {} local M = {}
---@tyep table
M.keybinds = {} M.keybinds = {}
---@type nil|table
M.opts = nil M.opts = nil
---@param opts table
function M.setup(opts) function M.setup(opts)
M.opts = vim.tbl_deep_extend("force", {}, config.get("python"), opts or {}) M.opts = vim.tbl_deep_extend("force", {}, config.get("python"), opts or {})
for key, command in pairs(M.opts.keybinds) do for key, command in pairs(M.opts.keybinds) do
@@ -31,6 +40,7 @@ function M.setup(opts)
end end
end end
---@return string|nil
function M.get_project_root() function M.get_project_root()
local dir = vim.fn.expand("%:p:h") local dir = vim.fn.expand("%:p:h")
while dir ~= "/" do while dir ~= "/" do
@@ -42,6 +52,7 @@ function M.get_project_root()
return nil return nil
end end
---@return string|nil
function M.install_dependency() function M.install_dependency()
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module) vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
if not pip_module or pip_module == "" then if not pip_module or pip_module == "" then
@@ -55,11 +66,13 @@ end
function M.run() function M.run()
local root = M.get_project_root() local root = M.get_project_root()
if not root then if not root then
---@return nil
return print("Error: Project root not found.") return print("Error: Project root not found.")
end end
local main_file = root .. "/main.py" local main_file = root .. "/main.py"
if vim.fn.filereadable(main_file) == 0 then if vim.fn.filereadable(main_file) == 0 then
---@return nil
return print("Error: main.py not found in project root.") return print("Error: main.py not found in project root.")
end end
@@ -78,4 +91,5 @@ function M.run()
utils.open_float_terminal(venv_cmd .. "python3 " .. main_file) utils.open_float_terminal(venv_cmd .. "python3 " .. main_file)
end end
---@return table
return M return M
+66
View File
@@ -0,0 +1,66 @@
---@type table
local M = {}
---@type table|nil
local opts = {}
---@class Preset
---@field left_padding integer
---@field right_padding integer
---@field top_padding integer
---@field bottom_padding integer
---@type Preset
local centered = {
left_padding = 0,
right_padding = 0,
top_padding = 0,
bottom_padding = 0
}
---@type Preset
local center_paneled = {
left_padding = 0,
right_padding = 100,
top_padding = 0,
bottom_padding = 0,
}
---@type Preset
local right_panel = {
left_padding = 100,
right_padding = 0,
top_padding = 0,
bottom_padding = 0,
}
---@type Preset
local cornered = {
left_padding = 100,
right_padding = 0,
top_padding = 25,
bottom_padding = 0,
}
---@param preset string
---@return table|nil
function M.getPresets(preset)
if preset == "custom" then
opts = nil
elseif preset == "centered" then
opts = centered
elseif preset == "center_panel" then
opts = center_paneled
elseif preset == "right_panel" then
opts = right_panel
elseif preset == "cornered" then
opts = cornered
else
print("Preset option: " .. preset .. " does not exist")
end
return opts
end
return M
+7 -3
View File
@@ -1,6 +1,10 @@
--
--The main source of the plugin in which runs the creation of projects and files for languages
--
---@module 'Polydev.utils' ---@module 'Polydev.utils'
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
---
---@type table ---@type table
local M = {} local M = {}
@@ -84,7 +88,7 @@ return M
}, },
c = { c = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(M.opts.project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build", "/include" }) do vim.fn.mkdir(full_project_root .. path, "p") end for _, path in ipairs({ "/src", "/build", "/include" }) do vim.fn.mkdir(full_project_root .. path, "p") end
utils.write_file(full_project_root .. "/build/" .. project_name .. ".polydev", project_name) utils.write_file(full_project_root .. "/build/" .. project_name .. ".polydev", project_name)
utils.write_file(full_project_root .. "/src/main.c", [[ utils.write_file(full_project_root .. "/src/main.c", [[
@@ -269,7 +273,7 @@ M.files = {
public class %s { public class %s {
// New File // New File
} }
]], class_name) ]], file_name)
utils.write_file(root_dir .. "/src/" .. file_name .. ".java", java_content) utils.write_file(root_dir .. "/src/" .. file_name .. ".java", java_content)
end end
+29
View File
@@ -0,0 +1,29 @@
local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local conf = require("telescope.config").values
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
-- our picker function: colors
local colors = function(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "colors",
finder = finders.new_table {
results = { "red", "green", "blue" }
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
-- print(vim.inspect(selection))
vim.api.nvim_put({ selection[1] }, "", false, true)
end)
return true
end,
}):find()
end
-- to execute the function
colors(require("telescope.themes").get_dropdown{})
+27 -4
View File
@@ -1,11 +1,22 @@
---@module "Polydev.presets"
local presets = require("Polydev.presets")
---@type table
local M = {} local M = {}
---@type table
M.opts = {} M.opts = {}
---@type table|nil
local presets_opts = {}
---@param opts table
function M.setup(opts) function M.setup(opts)
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {}) M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {})
end end
---@param path string
---@param content string
function M.write_file(path, content) function M.write_file(path, content)
local file = assert(io.open(path, "w"), "Error creating file: " .. path) local file = assert(io.open(path, "w"), "Error creating file: " .. path)
file:write(content) file:write(content)
@@ -14,13 +25,24 @@ function M.write_file(path, content)
print(path .. " created successfully!") print(path .. " created successfully!")
end end
local function set_presets_opts()
if presets.getPresets(M.opts.presets) ~= nil then
presets_opts = presets.getPresets(M.opts.presets)
else
presets_opts = M.opts
end
end
---@param cmd string
---@return boolean, table
function M.open_float_terminal(cmd) function M.open_float_terminal(cmd)
set_presets_opts()
local ui = vim.api.nvim_list_uis()[1] local ui = vim.api.nvim_list_uis()[1]
local width = math.max(1, math.floor(ui.width * 0.9) - M.opts.left_padding or 0 - M.opts.right_padding or 0) local width = math.max(1, math.floor(ui.width * 0.9) - presets_opts.left_padding - presets_opts.right_padding)
local height = math.max(1, math.floor(ui.height * 0.9) - M.opts.top_padding or 0 - M.opts.bottom_padding or 0) local height = math.max(1, math.floor(ui.height * 0.9) - presets_opts.top_padding - presets_opts.bottom_padding)
local row = math.max(1, math.floor((ui.height - height) / 2) + (M.opts.top_padding or 0)) local row = math.max(1, math.floor((ui.height - height) / 2) + (presets_opts.top_padding))
local col = math.max(1, math.floor((ui.width - width) / 2) + (M.opts.left_padding or 0)) local col = math.max(1, math.floor((ui.width - width) / 2) + (presets_opts.left_padding))
local buf = vim.api.nvim_create_buf(false, true) local buf = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(buf, true, { local win = vim.api.nvim_open_win(buf, true, {
@@ -49,4 +71,5 @@ function M.open_float_terminal(cmd)
return buf, win return buf, win
end end
---@return table
return M return M