shortcuts / no-neck-pain.nvim

☕ Dead simple yet super extensible plugin to center the currently focused buffer to the middle of the screen.
MIT License
567 stars 13 forks source link

Auto center on screen code #259

Open rraafays opened 11 months ago

rraafays commented 11 months ago

Describe the problem

I tend to code on narrow displays (4:3, 5:4) out of preference but for my on the go setup I use your plugin to auto center code using a function I wrote. At the moment the function I wrote only works on mac os due to the way wc outputs on osx.

Describe the solution

I would love it if you could natively include this in a more proper way rather than my 5 minute scripting job, I'm not very good with lua but here is the function.

vim.api.nvim_create_user_command('Center',function()
    local command = io.popen("wc -L " .. vim.fn.expand("%:p") .. " | tr -s ' ' | cut -d ' ' -f 2")
    local width = command:read("*a")
    print(width)
    vim.cmd("NoNeckPainResize " .. width + 8)
end,{})

Essentially all I am doing is getting the length of the longest line of the current buffer via wc cutting out any irellevant output other than that value via cut and then resizing via your plugin + some extra width to be certain and account for the numberline (another thing I think your plugin should take into account by default).

Thanks :-)

shortcuts commented 11 months ago

Hey, thanks for the suggestion and using the plugin :D

So if I summarize, what you'd like is that NoNeckPain resizes to the width of the longest line in the buffer when entering in it?

Do you also expect a resize if the line gets deleted/shortened/extended?


This seems pretty specific but I can indeed understand the motivation especially if you rarely change buffers etc.

rraafays commented 11 months ago

Hey, no problem I love your plugin.

Yeah so either provide a command to resize to longest line (ill just call it center) or even better if you could create an automatic center command that would be incredible I hadn't even thought of that, I actually use my center function then I have '-' & '=' bound to your width up & down

rraafays commented 11 months ago

I would love the automatic resizing you suggested if that's possible that's pretty much a perfect solution but even centering on BufEnter would be cool with me :-)

rraafays commented 11 months ago

another thing i might add is that the arbitrary extra 8 width i'm adding to the result of the wc command is due to this plugin including the margin in the total width. I don't know if this is doable but I think this plugin would be perfect outside of my afformentioned weird usecase if the width accounted for the margin meaning that a width of 80 means 80 characters after the margin rather than it being 80 characters wide total including the margin if that makes sense.

Thanks, :-)

rraafays commented 9 months ago

update: hey, i kind of found my own solution by modifying my script, i have also made it cross platform by using the wc command via https://github.com/uutils/coreutils crate.

DOCUMENT_WIDTH = 80
RECEIPT_WIDTH = 60

vim.api.nvim_create_user_command("Center", function()
    local filetype = vim.bo.filetype
    if filetype == "xxd" or filetype == "alpha" then
        vim.cmd("NoNeckPainResize " .. DOCUMENT_WIDTH)
        return
    elseif filetype == "text" then
        vim.cmd("NoNeckPainResize " .. RECEIPT_WIDTH)
        return
    end
    local path = vim.fn.expand("%:p")
    local command = io.popen("coreutils wc -L " .. path:gsub(" ", "\\ "))
    if command ~= nil then
        local result = command:read("*a")
        for i in string.gmatch(result, "%S+") do
            if tonumber(i) ~= nil then vim.cmd("NoNeckPainResize " .. i + 2) end
        end
    end
end, {})

vim.api.nvim_create_autocmd("BufWritePost", {
    pattern = "",
    command = "Center",
})

the most notable changes are the uses of either document width or receipt width as arbitrary values to fall back on as well as autocmd to center against a newly written buffer's file.

this implementation also works with file paths that include spaces and it also assumes that no gutter, numberline or symbols line is being used as per my new config. :-)

rraafays commented 9 months ago

beautiful, centered, painless

Screenshot 2024-01-03 at 19 02 48 Screenshot 2024-01-03 at 19 03 00 Screenshot 2024-01-03 at 19 04 00 Screenshot 2024-01-03 at 19 04 20
shortcuts commented 9 months ago

Hey, that's awesome and looks super clean! I'll definitely look for a native implementation in Neovim in order to provide this as an option, thanks!!!

chelsea6502 commented 7 months ago

This auto-centering is super cool! What would make it even better is to set a maximum width, in case there are any rare super-long lines.

rraafays commented 5 months ago

huge update! figured out how to auto center i have found a way to repurpose my original center function to save a temporary file and center against that while editing, solving the original aim of this issue.

it works by saving a temporary file in the users .local dir (currently assumes youre using lunarvim) and then runs wc on that file and passing the result to NoNeckPainResize with an arbitrary addition of 2 which seems to make the centering perfect.

there are 4 autocmds used to achieve this, both CursorMoved and CursorMovedI to constantly center while editing as well as after writing with BufWritePost since formatters might change the max width of the code. finally, the Filetype autocmd is used to make sure that man pages are kept at a comfortable square width.

            vim.api.nvim_create_user_command("Center", function()
                local filetype = vim.bo.filetype
                if filetype == "TelescopePrompt" or filetype == "alpha" or filetype == "lazy" then
                    return
                end
                vim.cmd("w! ~/.cache/lvim/width")
                local command = io.popen("wc -L " .. "~/.cache/lvim/width")
                if command ~= nil then
                    local result = command:read("*a")
                    for i in string.gmatch(result, "%S+") do
                        if tonumber(i) ~= nil then vim.cmd("NoNeckPainResize " .. i + 2) end
                    end
                end
            end, {})

            vim.api.nvim_create_user_command("Square", function()
                local command = io.popen("tput lines")
                if command ~= nil then
                    local result = command:read("*a")
                    vim.cmd("NoNeckPainResize " .. result * 2)
                end
            end, {})

            vim.api.nvim_create_autocmd("CursorMovedI", {
                pattern = "",
                command = "Center",
            })

            vim.api.nvim_create_autocmd("TextChanged", {
                pattern = "",
                command = "Center",
            })

            vim.api.nvim_create_autocmd("BufWritePost", {
                pattern = "",
                command = "Center",
            })

            vim.api.nvim_create_autocmd("FileType", {
                pattern = "man",
                command = "Square",
            })
        end
rraafays commented 5 months ago

https://github.com/shortcuts/no-neck-pain.nvim/assets/75220053/27eeab29-285c-4ebd-8694-ec79d38b5446

rraafays commented 5 months ago

https://github.com/shortcuts/no-neck-pain.nvim/assets/75220053/89c25fec-e9eb-4c57-b4fb-0f6358d7ff62

rraafays commented 5 months ago

https://github.com/shortcuts/no-neck-pain.nvim/assets/75220053/6264876a-4cbc-4a09-babb-af5286d2f023

shortcuts commented 5 months ago

This is awesome @rraafays!!!!!!! I love the idea and the outcome, I've started some work on the next branch in order to make nnp more responsive and extensible, if this can't be done natively with nvim until then, I'll make sure to ease your autocmds registering!

rraafays commented 2 months ago

Hey how's it going? I've recently finished my industry year and I'll be free for about a month before I start University again.

I've not stopped using your plugin at all doubly so because of my auto center script, I was wondering if you got any progress on this? I'm not the best at lua but I'd love to contribute if I can!

(this plugin with my auto center shenanigans has been so insanely robust, eye pleasing and most importantly pain free. I even get compliments about how clean my auto centering editor looks when I share my screen at work lol)

shortcuts commented 2 months ago

Hey how's it going?

Wonderful and you?

I've recently finished my industry year and I'll be free for about a month before I start University again.

I've not stopped using your plugin at all doubly so because of my auto center script, I was wondering if you got any progress on this? I'm not the best at lua but I'd love to contribute if I can!

Congrats and thanks for keeping up with the plugin!! That's cool to hear

If you want to tackle this feature addition, please go ahead! I've planned to add it in the next major but there's still quite a lot of work to do, and I have a bit less time right now with my newborn

But your idea will definitely land in the plugin, it's super nice to use!

(this plugin with my auto center shenanigans has been so insanely robust, eye pleasing and most importantly pain free. I even get compliments about how clean my auto centering editor looks when I share my screen at work lol)

❤️‍🔥❤️‍🔥 love to hear that, if you'd like any help to get started on contributing let me know