Skip to content

component

windwp edited this page Oct 17, 2023 · 34 revisions

Component

You can return a list of the child components.

child component have 2 fields

  • First field can be a string or function to get a text display on statusline
  • Second field return a highlight use to highlight
    • second field can be a key string from hl_colors table
    • second field can be a function return a text color need to use in hl_colors
    • second field can be a table It create new highlight with format { fg, bg, style}
    • second field can be a vim highlight group

Please never do anything complex or run an system command inside a text function. you can move it outside a text function or use a cache component. If you use a require inside a text function you should move it outside that and put it on top of config file.

basic.section = {
    hl_colors = {
        default={'white','black'},
        red_text = {'red', 'blue'}
    },
    text = function(bufnr, winid, width)
        if width>10 then
            return {
                { ' highlight is a text', 'red_text' },
                -- red_text define in hl_colors. 
                -- It is a fastest method and it doesn't need to calculate on redraw

                { ' function highlight ',function()
                    -- red_text define in hl_colors
                    return 'red_text'
                end},

                { ' search ', 'IncSearch' },-- IncSearch is a vim highlight group name

                { function() return 'function text'end, {'yellow','black'} },

                { 'highlight is a table', {'green', 'black','bold'} },

                { ' green and blue text ', {'', 'blue','bold'} },
                -- It have same foreground yellow with the previous component

                { ' red and blue text ', {'red', '','bold'} },
                -- It have same foreground blue with the previous
            }
        end
        return ''
    end,
}

add or remove component

It make you can add some component to your statusline when you press a key or some event happen.

local windline = require('windline')
  windline.add_component({
      name = 'test',
      hl_colors = {
          red = { 'red', 'NormalBg' },
      },
      text = function()
          return {
              { '🧛 ', 'red' },
              { 'new component', 'red' },
          }
      end,
  }, {
      filetype = 'default',
      -- it will add a new component before git component
      -- you can use and index number
      position = 'git',
      -- if you want to add on inactive component
      --kind ='inactive',
      autocmd=false,
      -- set it = true mean when you are on custom filetype component will add to the default statusline
      -- then remove after you leave that filetype
 
  })
  windline.remove_component({ name = 'test', filetype = 'default' })

sample

-- press `<leader>x` to add a new component or `<leader>z` to remove component
require('wlsample.test_add_component')

utility

Cache value on buffer

If you have a complex function please use that function with cache_on_buffer

--- it create a function then set a result of function to
--- `vim.b.wl_file_name`.It update value on BufEnter event otherwise it return
--- value from buffer`vim.b.wl_file_name`
local cache_utils = require('windline.cache_utils')
local cache_file_name = cache_utils.cache_on_buffer('BufEnter', 'wl_file_name', function()

end)

Sample

--- it is a filename function It shorten the first part of
--- pathname and the second part has a different color
--- if you don't use cache_on_buffer it will calculate every time you change mode
--- or redraw status line,
comps.file_name = {
    text = cache_utils.cache_on_buffer('BufEnter', 'wl_file_name', function()
        print('calc file_name')
        local path = fn.expand("%")
        local name = fn.fnamemodify(path, ':p:t')
        if string.match(path, '^fugitive') ~= nil then
            name = name .. '[git]'
            return { { name, 'git' } }
        end
        if path == "" or path == "./" then
            return '[No Name]'
        end
        local dir = path  and fn.fnamemodify(fn.pathshorten(path), ":h:h") .. "/" or ""
        local fname = path and fn.fnamemodify(path, ":h:t") .. "/" .. name or name
        if dir == "./" then
            dir = fn.fnamemodify(fname, ":h") .. "/"
            fname = name
        end
        return {
            {dir, 'dir'},
            {fname, 'path'}
        }
    end),
    hl_colors = {
        dir = {'white_light', 'blue'},
        path = { 'white', 'blue' },
        git = { 'red', 'blue' },
    },
}

you can use the cache_on_buffer function on the child component too. But remember it use the same function with a buffer_name_variable so if you send a different function with the same variable name only uses the first function.

the performance is huge improved if you use a cache version sample render statusline 1000 time with file_size(0.22s) vs cache_file_size(0.03s) you can run :WindLineBenchmark to test it render 10.000 time

Builtin component

Basic

local basic_components = require('windline.components.basic')
Component Usage
divider %= use to algin status
line_col display line and column
progress display progress
full_file_name() full file name of current path
file_name('default','unique,full,short') get unique file name or short name
cache_file_name same of file name but it cache value on buffer
file_type({icon=true,default=' '}) get file_type
cache_file_type same of file type but it cache value on buffer
file_size() get file size
cache_file_size same of file size but it cache value on buffer
file_icon get file icon
({default='',hl_colors={'red','blue'})
cache_file_icon() same of file icon but it cache value on buffer
file_format() get file format
file_modified(icon, is_local_buffer) get file modified
  • cache_file_icon() can return a string or a table if you add hl_colors
local icon_comp = b_components.cache_file_icon({ default = '', hl_colors = {'white','black_light'} })
basic.file = {
    hl_colors = {
        default = { 'white', 'black_light' },
    },
    text = function(bufnr)
        return {
             -- return a string
            {b_components.cache_file_icon({ default = '' }), 'default'},
             -- return a table
            icon_comp(bufnr),
        }
    end,
}

vim

Usage KEY
selection_count count the text on selection (work on visual mode)
search_count count the search match

Lsp

local lsp_components = require('windline.components.lsp')
Usage KEY
check_lsp() check is have lsp server
lsp_name() get lsp server name
lsp_error({show_zero=true,format="%s"}) get lsp error number
lsp_hint({show_zero=true,format="%s"}) get lsp hint number
lsp_warning({show_zero=true,format="%s"}) get lsp warning number
lsp_info({show_zero=true,format="%s"}) get lsp info number

Git

git component require gitsign.nvim to work.

local git_components = require('windline.components.git')
Usage KEY
is_git() check is git
git_branch() get git branch
diff_added({show_zero=true,format="%s"}) get diff added number
diff_changed({show_zero=true,format="%s"}) get diff changed number
diff_removed({show_zero=true,format="%s"}) get diff remove number

Git_rev

local git_rev = require('windline.components.git_rev')
git_rev.git_rev()
Usage KEY
git_rev({format=" ⇡%s⇣%s",interval=10000}) get different commit from HEAD to upstream

you can swap position by git_rev.git_rev(format_index = { 1, 2 }, format = ' %s⇣%s⇡ '),

Clone this wiki locally