nvimdev / dashboard-nvim

vim dashboard

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improvement: Center align util method

bamdadsabbagh opened this issue · comments

Hi,

TLDR

I changed the behaviour of utils.center_align as its current behaviour does
not fit my use case. The injected string is shifted line by line rather than
indenting the whole block. I don't know if I should either:

  • open a PR, and overwrite the align function
  • open a PR, and add a new align function + user setting
  • keep it to my fork

Issue

I was playing with the multi line string rendered in the dashboard in order to
display custom ASCII art. Unfortunately, lines with numerous spaces around
are not properly rendered.

After reading utils.center_align method, I noticed that alignment is done on
a line basis instead of considering the whole block's indentation.

Please find below the code change and the different renderings.

Code change

In utils.lua:53

Before

The current code applies shifts to each line independently.

local centered_lines = {}
local fills = fill_sizes(tbl)

for i = 1, #tbl do
  local fill_line = (' '):rep(fills[i]) .. tbl[i]
  table.insert(centered_lines, fill_line)
end

After

We add a fill_min variable to indent all lines by the same amount.

local centered_lines = {}
local fills = fill_sizes(tbl)
local fill_min = math.min(unpack(fills))

for i = 1, #tbl do
  local fill_line = (' '):rep(fill_min) .. tbl[i]
  table.insert(centered_lines, fill_line)
end

Please find below this wonderful spider I downloaded
here.

Raw string (input)

                   /\
                  /  \
                 |  _ \                  _
                 | / \ \                / \
                 |/   \ \              /   \
                 /     \ |        /\  /     \
                /|      \| ~  ~  /  \/       \
        _______/_|_______\(o)(o)/___/\_____   \
       /      /  |       (______)     \    \   \_
      /      /   |                     \    \
     /      /    |                      \    \
    /      /     |                       \    \
   /     _/      |                        \    \
  /             _|                         \    \_
_/                                          \
                                             \
                                              \_
                                             ~tahl~

Neovim render (before)

before

Neovim render (after)

after