Skip to content

Commit

Permalink
feat(doom)!: Center the dashboard on the vertical (vertical_center)
Browse files Browse the repository at this point in the history
To disable this add `vertical_center = false` in the doom `configs`
  • Loading branch information
polirritmico committed Aug 7, 2024
1 parent 4dada1d commit d5cfa1b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ config = {
},
},
footer = {},
vertical_center = true, -- Center the Dashboard on the vertical (from top to bottom)
}
```

Expand Down
95 changes: 64 additions & 31 deletions lua/dashboard/theme/doom.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
local api, keymap = vim.api, vim.keymap
local utils = require('dashboard.utils')

local function center_entry_list(config)
local vert_offset

local function gen_center_icons_and_descriptions(config)
local lines = {}
local center = config.center
or {
{ desc = 'Please config your own center section', key = 'p' },
}

local counts = {}
for _, item in pairs(center) do
Expand Down Expand Up @@ -47,27 +46,19 @@ local function center_entry_list(config)
lines[i] = lines[i]:sub(1, #lines[i] - count)
end

api.nvim_buf_set_lines(config.bufnr, vert_offset, -1, false, lines)
return lines
end

local function gen_center(config)
local lines = center_entry_list(config)

local first_line = api.nvim_buf_line_count(config.bufnr)
api.nvim_buf_set_lines(config.bufnr, first_line, -1, false, lines)

if not config.center then
return
end

local function gen_center_highlights_and_keys(config, lines)
local ns = api.nvim_create_namespace('DashboardDoom')
local seed = 0
local pos_map = {}
local position_map = {}
for i = 1, #lines do
if lines[i]:find('%w') then
local idx = i == 1 and i or i - seed
seed = seed + 1
pos_map[i] = idx
position_map[i] = idx
local _, scol = lines[i]:find('%s+')
local ecol = scol + (config.center[idx].icon and #config.center[idx].icon or 0)

Expand All @@ -76,7 +67,7 @@ local function gen_center(config)
config.bufnr,
0,
config.center[idx].icon_hl or 'DashboardIcon',
first_line + i - 1,
vert_offset + i - 1,
0,
ecol
)
Expand All @@ -86,7 +77,7 @@ local function gen_center(config)
config.bufnr,
0,
config.center[idx].desc_hl or 'DashboardDesc',
first_line + i - 1,
vert_offset + i - 1,
ecol,
-1
)
Expand All @@ -100,45 +91,61 @@ local function gen_center(config)
string.format(config.center[idx].key_format or ' [%s]', config.center[idx].key),
config.center[idx].key_hl or 'DashboardKey',
})
api.nvim_buf_set_extmark(config.bufnr, ns, first_line + i - 1, 0, {
api.nvim_buf_set_extmark(config.bufnr, ns, vert_offset + i - 1, 0, {
virt_text_pos = 'eol',
virt_text = virt_tbl,
})
end
end
end
return position_map
end

local function gen_center_base(config)
if not config.center then
local msg = utils.center_align({ 'Please config your own center section', '' })
api.nvim_buf_set_lines(config.bufnr, vert_offset, -1, false, msg)
return
end

local line = api.nvim_buf_get_lines(config.bufnr, first_line, first_line + 1, false)[1]
local lines = gen_center_icons_and_descriptions(config)
return lines
end

local function move_cursor_behaviour(config, bottom_linenr)
local line = api.nvim_buf_get_lines(config.bufnr, vert_offset, vert_offset + 1, false)[1]
local col = line:find('%w')
local col_width = api.nvim_strwidth(line:sub(1, col))
col = col and col - 1 or 9999
api.nvim_win_set_cursor(config.winid, { first_line + 1, col })
api.nvim_win_set_cursor(config.winid, { vert_offset + 1, col })

local bottom = api.nvim_buf_line_count(config.bufnr)
vim.defer_fn(function()
local before = 0
if api.nvim_get_current_buf() ~= config.bufnr then
return
end

local dashboard_group = api.nvim_create_augroup('DashboardDoomCursor', { clear = true })
api.nvim_create_autocmd('CursorMoved', {
buffer = config.bufnr,
group = dashboard_group,
callback = function()
local buf = api.nvim_win_get_buf(0)
if vim.api.nvim_get_option_value('filetype', { buf = buf }) ~= 'dashboard' then
return
end

local curline = api.nvim_win_get_cursor(0)[1]
if curline < first_line + 1 then
curline = bottom - 1
elseif curline > bottom - 1 then
curline = first_line + 1
if curline < vert_offset + 1 then
curline = bottom_linenr - 1
elseif curline > bottom_linenr - 1 then
curline = vert_offset + 1
elseif not api.nvim_get_current_line():find('%w') then
curline = curline + (before > curline and -1 or 1)
end
before = curline

-- FIX: #422: In Lua the length of a string is the number of bytes not
-- NOTE: #422: In Lua the length of a string is the number of bytes not
-- the number of characters.
local curline_str = api.nvim_buf_get_lines(config.bufnr, curline - 1, curline, false)[1]
local strwidth = api.nvim_strwidth(curline_str:sub(1, col + 1))
Expand All @@ -150,10 +157,12 @@ local function gen_center(config)
end,
})
end, 0)
end

local function confirm_key(config, position_map)
keymap.set('n', config.confirm_key or '<CR>', function()
local curline = api.nvim_win_get_cursor(0)[1]
local index = pos_map[curline - first_line]
local index = position_map[curline - vert_offset]
if index and config.center[index].action then
if type(config.center[index].action) == 'string' then
local dump = loadstring(config.center[index].action)
Expand Down Expand Up @@ -229,13 +238,37 @@ local function gen_footer(config)
else
lines = load_packages(config)
end
utils.add_update_footer_command(config.bufnr, lines) -- ?
utils.add_update_footer_command(config.bufnr, lines)
return #lines
end

local function vertical_center(config)
if config.vertical_center == false then
return
end

local size = math.floor(vim.o.lines / 2)
- math.ceil(api.nvim_buf_line_count(config.bufnr) / 2)
- 2
local fill = utils.generate_empty_table(size)
api.nvim_buf_set_lines(config.bufnr, 0, 0, false, fill)

vert_offset = vert_offset + (size < 0 and 0 or size)
end

local function theme_instance(config)
require('dashboard.theme.header').generate_header(config)
gen_center(config)
gen_footer(config)
vert_offset = api.nvim_buf_line_count(config.bufnr)

local lines = gen_center_base(config)
local footer_size = gen_footer(config)
vertical_center(config)
local actions_position_map = gen_center_highlights_and_keys(config, lines)

local last_entry_pos = api.nvim_buf_line_count(config.bufnr) - footer_size
move_cursor_behaviour(config, last_entry_pos)
confirm_key(config, actions_position_map)

vim.bo[config.bufnr].modifiable = false
vim.bo[config.bufnr].modified = false
--defer until next event loop
Expand Down

0 comments on commit d5cfa1b

Please sign in to comment.