dokwork / lualine-ex

Plugin for lualine.nvim with additional components and tools
MIT License
21 stars 1 forks source link

feat: ex.relative_filename #16

Closed vladimir-popov closed 9 months ago

vladimir-popov commented 9 months ago

ex.relative_filename

This component shows a filename: a file name and a path to the current file relative to the current working directory, and may be used effectively together with ex.cwd. The filename has a prefix, which shows a file's place in the file system relative to the cwd:

File path relative to cwd Options Component example
inside cwd
outside cwd external_prefix = "/..."
outside cwd, but inside $HOME

Some path may be very long and takes significant part in the statusline. It's possible to specify the {max_length} of the filename. To achieve that the follow algorithm is used:

Example of the shorten filename with follow options { shorten: { length = 3, exclude = { 1 } } }:

Space for component enough to show ... Component example
the whole path
the path with specified options (shorten.length = 3)
the path with shorten.length = 2
the path with shorten.length = 1
the path ignoring exclude section
only the file name

The {max_length} may be a number, or a function which receives the current component value and returns a number:

Default configuration:

{
    -- The prefix which is used when the current file is outside cwd
    external_prefix = nil,

    -- The prefix which is used when the length of the filename after shorten
    -- is longer than {max_length}
    filename_only_prefix = '…/',

    -- The max length of the component value.
    -- < 0          - never shorten; 
    -- 0            - always shorten; 
    -- > 0 and  < 1 - shorten when longer than {max_length} * {vim.o.columns} 
    --                for {laststatus} == 3;
    --                and shorten when longer than 
    --                {max_length} * {vim.api.nvim_win_get_width(0)} overwise; 
    -- >= 1         - shorten when longer then N symbols;
    max_length = 0.3,

    -- The configuration of the shorten algorithm.
    shorten = { 
        -- The count of letters, which will be taken from every part of the path
        lenght = 5, 
        -- The list of indexes of filename parts, which should not be shorten at all
        -- (the file name { -1 } is always excluded)
        exclude = nil 
    },
}

_ex.relative_filename component doesn't provide options to show file states, because it easily possible to do with standard approach:_

-- readonly mode indicator example:
{
    '%{""}',
    draw_empty = true,
    icon = { '' },
    cond = function()
        return not vim.bo.modifiable
    end,
}