Skip to content

Commit

Permalink
feat(ui): allow user-defined float options
Browse files Browse the repository at this point in the history
  • Loading branch information
kndndrj committed Apr 22, 2024
1 parent 96d430e commit 6f897ef
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 47 deletions.
5 changes: 5 additions & 0 deletions lua/dbee/api/state.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local floats = require("dbee.ui.common.floats")
local DrawerUI = require("dbee.ui.drawer")
local EditorUI = require("dbee.ui.editor")
local ResultUI = require("dbee.ui.result")
Expand Down Expand Up @@ -47,6 +48,10 @@ local function setup_ui()

setup_handler()

-- configure options for floating windows
floats.configure(m.config.float_options)

-- initiate all UI elements
m.result = ResultUI:new(m.handler, m.config.result)
m.call_log = CallLogUI:new(m.handler, m.result, m.config.call_log)
m.editor = EditorUI:new(m.handler, m.result, m.config.editor)
Expand Down
4 changes: 4 additions & 0 deletions lua/dbee/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local config = {}
---@class Config
---@field sources Source[] list of connection sources
---@field extra_helpers table<string, table<string, string>>
---@field float_options table<string, any>
---@field drawer drawer_config
---@field editor editor_config
---@field result result_config
Expand Down Expand Up @@ -55,6 +56,9 @@ config.default = {
-- ["List All"] = "select * from {{ .Table }}",
-- },
},
-- options passed to floating windows - :h nvim_open_win()
float_options = {},

-- drawer window config
drawer = {
-- these two option settings can be added to all UI elements and
Expand Down
120 changes: 73 additions & 47 deletions lua/dbee/ui/common/floats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

local M = {}

---User defined options for floating windows
---@type table<string, any>
local OPTS = {}

---Set up custom floating window parameters
---@param opts? table<string, any>
function M.configure(opts)
OPTS = vim.tbl_extend("force", {
border = "rounded",
title_pos = "center",
style = "minimal",
title = "",
zindex = 150,
}, opts or {})
end

---Merges user defined options with provided spec.
---@param spec table<string, any>
local function enrich_float_opts(spec)
return vim.tbl_extend("keep", spec, OPTS)
end

---@alias prompt { name: string, default: string }[] list of lines with optional defaults to display as prompt

--- highlight the prompt keys
Expand Down Expand Up @@ -31,9 +53,9 @@ local function highlight_prompt(prompt, winid)
end

---@param prompt prompt
---@param opts? { width: integer, height: integer, title: string, border: string|string[], callback: fun(result: table<string, string>) } optional parameters
function M.prompt(prompt, opts)
opts = opts or {}
---@param spec? { title: string, callback: fun(result: table<string, string>) }
function M.prompt(prompt, spec)
spec = spec or {}

-- create lines to display
---@type string[]
Expand All @@ -42,15 +64,15 @@ function M.prompt(prompt, opts)
table.insert(display_prompt, p.name .. ": " .. (p.default or ""))
end

local win_width = opts.width or 100
local win_height = opts.height or #display_prompt
local win_width = 100
local win_height = #display_prompt
local ui_spec = vim.api.nvim_list_uis()[1]
local x = math.floor((ui_spec["width"] - win_width) / 2)
local y = math.floor((ui_spec["height"] - win_height) / 2)

-- create new buffer
local bufnr = vim.api.nvim_create_buf(false, false)
local name = opts.title or tostring(os.clock())
local name = spec.title or tostring(os.clock())
vim.api.nvim_buf_set_name(bufnr, name)
vim.api.nvim_buf_set_option(bufnr, "filetype", "dbee")
vim.api.nvim_buf_set_option(bufnr, "buftype", "acwrite")
Expand All @@ -61,21 +83,22 @@ function M.prompt(prompt, opts)
vim.api.nvim_buf_set_option(bufnr, "modified", false)

-- open window
local winid = vim.api.nvim_open_win(bufnr, true, {
relative = "editor",
width = win_width,
height = win_height,
col = x,
row = y,
border = opts.border or "rounded",
title = opts.title or "",
title_pos = "center",
style = "minimal",
})
local winid = vim.api.nvim_open_win(
bufnr,
true,
enrich_float_opts {
relative = "editor",
width = win_width,
height = win_height,
col = x,
row = y,
title = spec.title or "",
}
)
-- apply the highlighting of keys to window
highlight_prompt(prompt, winid)

local callback = opts.callback or function() end
local callback = spec.callback or function() end

-- set callbacks
vim.api.nvim_create_autocmd("BufWriteCmd", {
Expand Down Expand Up @@ -134,38 +157,39 @@ function M.prompt(prompt, opts)
end

---@param file string file to edit
---@param opts? { width: integer, height: integer, title: string, border: string|string[], callback: fun() } optional parameters
function M.editor(file, opts)
opts = opts or {}
---@param spec? { title: string, callback: fun() } required parameters for float.
function M.editor(file, spec)
spec = spec or {}

local ui_spec = vim.api.nvim_list_uis()[1]
local win_width = opts.width or (ui_spec["width"] - 50)
local win_height = opts.height or (ui_spec["height"] - 10)
local win_width = ui_spec["width"] - 50
local win_height = ui_spec["height"] - 10
local x = math.floor((ui_spec["width"] - win_width) / 2)
local y = math.floor((ui_spec["height"] - win_height) / 2)

-- create new dummy buffer
local tmp_buf = vim.api.nvim_create_buf(false, true)

-- open window
local winid = vim.api.nvim_open_win(tmp_buf, true, {
relative = "editor",
width = win_width,
height = win_height,
col = x,
row = y,
border = opts.border or "rounded",
title = opts.title or "",
title_pos = "center",
style = "minimal",
})
local winid = vim.api.nvim_open_win(
tmp_buf,
true,
enrich_float_opts {
title = spec.title or "",
relative = "editor",
width = win_width,
height = win_height,
col = x,
row = y,
}
)

-- open the file
vim.cmd("e " .. file)
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "delete")

local callback = opts.callback or function() end
local callback = spec.callback or function() end

-- set callbacks
vim.api.nvim_create_autocmd("BufWritePost", {
Expand Down Expand Up @@ -250,18 +274,20 @@ function M.hover(relative_winid, contents)
end

-- open window
local winid = vim.api.nvim_open_win(bufnr, false, {
zindex = 150,
relative = "win",
win = relative_winid,
width = win_width,
height = win_height,
col = col,
row = cursor_row - 1,
anchor = anchor,
border = "rounded",
style = "minimal",
})

local winid = vim.api.nvim_open_win(
bufnr,
false,
enrich_float_opts {
relative = "win",
win = relative_winid,
width = win_width,
height = win_height,
col = col,
row = cursor_row - 1,
anchor = anchor,
}
)

return function()
pcall(vim.api.nvim_win_close, winid, true)
Expand Down

0 comments on commit 6f897ef

Please sign in to comment.