nvim-lualine / lualine.nvim

A blazing fast and easy to configure neovim statusline plugin written in pure lua.
MIT License
5.89k stars 459 forks source link

Feat: Conditon for extensions #247

Closed HiPhish closed 2 years ago

HiPhish commented 3 years ago

Requested feature

Currently Lualine only looks at the file type in order to determine whether to use an extension. I would like more control by being able to specify a condition boolean thunk as well:

local my_extension = {
    sections = {
        lualine_a = {'filename'}
    },
    filetypes = {'help'}
    condition = function()
        return vim.bo.buftype == 'help'
    end
}

Motivation

Some buffers can be opened like any normal file for editing, or they can be opened for reading only. Help files are the prime example: when writing a help file I want all the usual status line components, but when I open the file by using the :help command I would like to load the extension instead.

I already have in my Vim configuration in after/ftplugin/help.vim the following to hide line numbers and format characters when reading documentation (where they are only distracting) but to keep them when writing documentation:

if &buftype == 'help'
    setlocal foldcolumn=0 nonumber
    finish
endif

setlocal conceallevel=0

I also have similar settings for info and markdown buffers as well by checking for buftype nofile instead.

hoob3rt commented 3 years ago

can't this already be achieved with

local my_extension = {
  sections = {
    lualine_a = {
      {'filename', condition = function() return vim.bo.buftype == 'help' end}
    }
  },
  filetypes = {'help'}
}

Granted this can get pretty rough if you want to create a sophisticated extension.

I like it so I will try creating a pr with it sooner than later. This change should also affect disabled_filetypes. We can change it to condition as well for more control

HiPhish commented 3 years ago

I don't think your example would be sufficient, but I could be wrong. Let's say my default status line contains the mode, file name and position. Let's say I want help files to show the position and file name.

default = {
    sections = {
        lualine_a = {'mode'}
        lualine_b = {'filename'}
        lualine_z = {'position'}
    }
}

extension = {
    sections = {
        lualine_a = {'position'}
        lualine_z = {'filename'}
    }
}

If the condition does not match I want to fall back on the default. Your example looks like it only turns off individual components, but not the entire extension itself. I would have to repeat the default configuration inside the extension configuration:

extension = {
    sections = {
        lualine_a = {
            {'position', condition = is_readonly},
            {'mode', condition = function() return not is_readonly() end}
        },
        lualine_b = {
            {'filename', condition = function() return not is_readonly() end}
        },
        lualine_z = {
            {'filename', condition = is_readonly},
            {'position', condition = function() return not is_readonly() end}
        }
    }
}

Please correct me if I am wrong.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

artem-nefedov commented 1 year ago

I would be interested in this as well. Specifically, my usecase is I want to make an extension that works in terminal buffers only, but terminal buffers don't have a filetype.