Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: Center align util method #402

Open
bamdadfr opened this issue Nov 14, 2023 · 0 comments
Open

Improvement: Center align util method #402

bamdadfr opened this issue Nov 14, 2023 · 0 comments

Comments

@bamdadfr
Copy link

bamdadfr commented Nov 14, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant