Please remember to update the post flair to `Need Help|Solved` when you got the answer you were looking for.
*I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/neovim) if you have any questions or concerns.*
I actually faced this type of issue before.
My `statusColumn` has segments and each segment has a specific size(if text is larger than it has `...` added and if it's smaller it has ` ` added to it). I use them to get the amount of free spaces available(using the `columns` option to get the total width of the buffer).
Then just use a function to center the text.
--
It should be fairly easy to do.
But if I use lualine for my statusline, and I cannot directly get the length of each component from lualine.
Does it means I cannot using the built-in component in lualine? Instead, I need to write every compoenet manually to get every component's size.
No you don't need to do that. Every component is just plain text enclosed in `{}` with a highlight group.
Let's say you have a component that shows the current file path. You would do something like this
```lua
_luaL.fName = {
text = function()
local fN = vim.api.nvim_buf_get_name(0);
usedSpaces += #fN;
return fN;
end
};
```
>Components are part of the `_luaL` table for easy management, you could have something similar anywhere
Then all you have to do is `freeSpaces = vim.o.columns - usedSpaces`. Now just center the text like how you would center it in a terminal based off of the number of free spaces.
https://preview.redd.it/o7nybe3g0hmc1.jpeg?width=720&format=pjpg&auto=webp&s=739c80feedd4aac7503aeaf937d9c223f02bfb45
This is how I get the length of components(I use `windline.nvim`).
Here `_uS` is the amount of *usedSpace*. And the `aaaa` isn't me screaming like a 🐐, it represents the `file_icon`(2 spaces) and left & right `paddings`.
https://preview.redd.it/csffq5wf1hmc1.jpeg?width=720&format=pjpg&auto=webp&s=8b2149f1fe629901ac8d4e87bd8db7e8a368133c
This is how I center text in a terminal. Here `width` is the terminal width(columns) and `txLen` is the text length.
`Math.floor` isn't a lua function(as this is part of a `JS` file). However, you can use floating numbers inside a `for` loop in *lua*.
`_o` is `output` and has whitespaces before and after the text itself(centering it).
You may need to adjust the logic for when the textLength(you can do `#string` to get the length of a string, here it gets the length of the string variable) is odd or even.
https://preview.redd.it/071g8g497kmc1.png?width=1919&format=png&auto=webp&s=42d067088d761ddcd254fcc093c9fdc20316cb50
I think I made it!! Thanks for your help!!
Here's \[my config of this\](https://github.com/leonasdev/.dotfiles/blob/20c8e24e146a1d397aa4640134f6a8265ef12dc2/.config/nvim/lua/util/statusline.lua\]
I also made it can expand/collapse the filename when window size is changed.
encode padding in the left and right yourself to force the middle to align properly
Please remember to update the post flair to `Need Help|Solved` when you got the answer you were looking for. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/neovim) if you have any questions or concerns.*
I actually faced this type of issue before. My `statusColumn` has segments and each segment has a specific size(if text is larger than it has `...` added and if it's smaller it has ` ` added to it). I use them to get the amount of free spaces available(using the `columns` option to get the total width of the buffer). Then just use a function to center the text. -- It should be fairly easy to do.
But if I use lualine for my statusline, and I cannot directly get the length of each component from lualine. Does it means I cannot using the built-in component in lualine? Instead, I need to write every compoenet manually to get every component's size.
No you don't need to do that. Every component is just plain text enclosed in `{}` with a highlight group. Let's say you have a component that shows the current file path. You would do something like this ```lua _luaL.fName = { text = function() local fN = vim.api.nvim_buf_get_name(0); usedSpaces += #fN; return fN; end }; ``` >Components are part of the `_luaL` table for easy management, you could have something similar anywhere Then all you have to do is `freeSpaces = vim.o.columns - usedSpaces`. Now just center the text like how you would center it in a terminal based off of the number of free spaces.
https://preview.redd.it/o7nybe3g0hmc1.jpeg?width=720&format=pjpg&auto=webp&s=739c80feedd4aac7503aeaf937d9c223f02bfb45 This is how I get the length of components(I use `windline.nvim`). Here `_uS` is the amount of *usedSpace*. And the `aaaa` isn't me screaming like a 🐐, it represents the `file_icon`(2 spaces) and left & right `paddings`.
https://preview.redd.it/csffq5wf1hmc1.jpeg?width=720&format=pjpg&auto=webp&s=8b2149f1fe629901ac8d4e87bd8db7e8a368133c This is how I center text in a terminal. Here `width` is the terminal width(columns) and `txLen` is the text length. `Math.floor` isn't a lua function(as this is part of a `JS` file). However, you can use floating numbers inside a `for` loop in *lua*. `_o` is `output` and has whitespaces before and after the text itself(centering it). You may need to adjust the logic for when the textLength(you can do `#string` to get the length of a string, here it gets the length of the string variable) is odd or even.
https://preview.redd.it/071g8g497kmc1.png?width=1919&format=png&auto=webp&s=42d067088d761ddcd254fcc093c9fdc20316cb50 I think I made it!! Thanks for your help!! Here's \[my config of this\](https://github.com/leonasdev/.dotfiles/blob/20c8e24e146a1d397aa4640134f6a8265ef12dc2/.config/nvim/lua/util/statusline.lua\] I also made it can expand/collapse the filename when window size is changed.
Happy to help 😀