Skip to content

Commit

Permalink
feat(util): better way to handle borders
Browse files Browse the repository at this point in the history
  • Loading branch information
loctvl842 committed May 27, 2023
1 parent 7250039 commit b5c5cf7
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lua/tvl/config/lsp/diagnostics.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Util = require("tvl.util")
vim.g.diagnostics_enabled = true

local diagnostics = {
Expand All @@ -21,7 +22,7 @@ local diagnostics = {
focusable = false,
style = "minimal",
-- border = "rounded",
border = { "", "", "", "", "", "", "", "" }, -- [ top top top - right - bottom bottom bottom - left ]
border = Util.generate_borderchars("thick", "tl-t-tr-r-bl-b-br-l"),
source = "always",
header = "",
prefix = "",
Expand Down
4 changes: 0 additions & 4 deletions lua/tvl/config/lsp/servers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ local servers = {
},
workspace = {
checkThirdParty = false,
library = {
[vim.fn.expand("$VIMRUNTIME/lua")] = false,
[vim.fn.stdpath("config") .. "/lua"] = false,
},
},
completion = {
callSnippet = "Replace",
Expand Down
3 changes: 2 additions & 1 deletion lua/tvl/config/neo-tree.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
local neotree = require("neo-tree")
local icons = require("tvl.core.icons")
local Util = require("tvl.util")

neotree.setup({
close_if_last_window = true, -- Close Neo-tree if it is the last window left in the tab
popup_border_style = { "", "", "", "", "", "", "", "" },
popup_border_style = Util.generate_borderchars("thick", "tl-t-tr-r-bl-b-br-l"),
-- popup_border_style = "rounded",
enable_git_status = true,
enable_diagnostics = true,
Expand Down
32 changes: 32 additions & 0 deletions lua/tvl/core/icons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,36 @@ return {
Pencil = "",
Bug = "",
},
borders = {
empty = {
top = " ",
right = " ",
bottom = " ",
left = " ",
top_left = " ",
top_right = " ",
bottom_right = " ",
bottom_left = " ",
},
thin = {
top = "",
right = "",
bottom = "",
left = "",
top_left = "🭽",
top_right = "🭾",
bottom_right = "🭿",
bottom_left = "🭼",
},
thick = {
top = "",
right = "",
bottom = "",
left = "",
top_left = "",
top_right = "",
bottom_right = "",
bottom_left = "",
},
},
}
25 changes: 20 additions & 5 deletions lua/tvl/core/resources/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,28 @@ return {
version = false, -- telescope did only one release, so use HEAD for now
opts = {
defaults = {
prompt_prefix = "",
selection_caret = "",
prompt_prefix = "",
selection_caret = "",
entry_prefix = " ",
borderchars = {
prompt = { "", " ", "", "", "", " ", "", "" },
results = { "", " ", "", "", "", " ", " ", "" },
preview = { "", "", "", "", "", "", "", "" },
prompt = require("tvl.util").generate_borderchars(
"thick",
nil,
{ top = "", top_left = "", right = " ", top_right = " ", bottom_right = " " }
),
results = require("tvl.util").generate_borderchars(
"thick",
nil,
{ top = "", top_left = "", right = " ", top_right = " ", bottom_right = " " }
),
preview = require("tvl.util").generate_borderchars(
"thick",
nil,
{ top = "", top_left = "", top_right = "" }
),
},
dynamic_preview_title = true,
hl_result_eol = true,
sorting_strategy = "ascending",
file_ignore_patterns = {
".git/",
Expand Down
15 changes: 7 additions & 8 deletions lua/tvl/core/resources/ui.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local Util = require("tvl.util")

return {
{
"rcarriga/nvim-notify",
Expand Down Expand Up @@ -25,9 +27,8 @@ return {
end,
},
init = function()
local util = require("tvl.util")
if not util.has("noice.nvim") then
util.on_very_lazy(function()
if not Util.has("noice.nvim") then
Util.on_very_lazy(function()
vim.notify = require("notify")
end)
end
Expand Down Expand Up @@ -208,9 +209,7 @@ return {
direction = "float",
autochdir = false,
float_opts = {
border = { "", "", "", "", "", "", "", "" }, -- [ top top top - right - bottom bottom bottom - left ]
-- border = { " ", "▁", " ", "▎", " ", "▔", " ", "▕" }, -- [ top top top - right - bottom bottom bottom - left ]
-- border = "single",
border = Util.generate_borderchars("thick", "tl-t-tr-r-bl-b-br-l"),
winblend = 0,
},
highlights = {
Expand Down Expand Up @@ -323,10 +322,10 @@ return {
lazy = false,
opts = {
input = {
border = { "", "", "", "", "", "", "", "" }, -- [ top top top - right - bottom bottom bottom - left ]
border = Util.generate_borderchars("thick", "tl-t-tr-r-bl-b-br-l"),
win_options = { winblend = 0 },
},
select = { telescope = require("tvl.util").telescope_theme() },
select = { telescope = Util.telescope_theme("cursor") },
},
init = function()
---@diagnostic disable-next-line: duplicate-set-field
Expand Down
86 changes: 84 additions & 2 deletions lua/tvl/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ end
M.telescope_theme = function(type)
if type == nil then
return {
borderchars = { "", "", "", "", "", "", "", "" },
borderchars = M.generate_borderchars("thick"),
layout_config = {
width = 80,
height = 0.5,
Expand All @@ -84,7 +84,7 @@ M.telescope_theme = function(type)
end
return require("telescope.themes")["get_" .. type]({
cwd = M.get_root(),
borderchars = { "", "", "", "", "", "", "", "" },
borderchars = M.generate_borderchars("thick", nil, { top = "", top_left = "", top_right = "" }),
})
end

Expand Down Expand Up @@ -143,4 +143,86 @@ M.capabilities = function(ext)
)
end

M.notify = function(msg, level, opts)
opts = opts or {}
level = vim.log.levels[level:upper()]
if type(msg) == "table" then
msg = table.concat(msg, "\n")
end
local nopts = { title = "Nvim" }
if opts.once then
return vim.schedule(function()
vim.notify_once(msg, level, nopts)
end)
end
vim.schedule(function()
vim.notify(msg, level, nopts)
end)
end

---@class BorderIcons
---@field top string
---@field right string
---@field bottom string
---@field left string
---@field top_left string
---@field top_right string
---@field bottom_right string
---@field bottom_left string

--- @param type "thin" | "thick" | "empty" | nil
--- @param order "t-r-b-l-tl-tr-br-bl" | "tl-t-tr-r-bl-b-br-l" | nil
--- @param opts BorderIcons | nil
M.generate_borderchars = function(type, order, opts)
if order == nil then
order = "t-r-b-l-tl-tr-br-bl"
end
local border_icons = require("tvl.core.icons").borders
--- @type BorderIcons
local border = vim.tbl_deep_extend("force", border_icons[type or "empty"], opts or {})

local borderchars = {}

local extractDirections = (function()
local index = 1
return function()
if index == nil then
return nil
end
-- Find the next occurence of `char`
local nextIndex = string.find(order, "-", index)
-- Extract the first direction
local direction = string.sub(order, index, nextIndex and nextIndex - 1)
-- Update the index to nextIndex
index = nextIndex and nextIndex + 1 or nil
return direction
end
end)()

local mappings = {
t = "top",
r = "right",
b = "bottom",
l = "left",
tl = "top_left",
tr = "top_right",
br = "bottom_right",
bl = "bottom_left",
}
local direction = extractDirections()
while direction do
if mappings[direction] == nil then
M.notify(string.format("Invalid direction '%s'", direction), "error")
end
borderchars[#borderchars + 1] = border[mappings[direction]]
direction = extractDirections()
end

if #borderchars ~= 8 then
M.notify(string.format("Invalid order '%s'", order), "error")
end

return borderchars
end

return M

0 comments on commit b5c5cf7

Please sign in to comment.