Skip to content

component

windwp edited this page Jul 5, 2021 · 34 revisions

Component

you can define a component like this.

time_count = 1

local count = {
  hl_colors = {
     countRed     = {'black', 'red'},
     countGreen  = {'black', 'green'}
  },
  hl = function(hl_colors)
      -- a hl function change the highlight color of text
      if time_count > 50 then
        return hl_colors.countRed
      end
      return hl_colors.countGreen
  end,
  text = function(bufnr)
    time_count = time_count + 1
    if time_count > 99 then
      time_count = 1
    end
    return ' ' .. vim.fn.strftime("%H:%M:%S") .. ' '
  end,
}

You can also return a list of child component . It is better way with that syntax

basic.section_y = {
   hl_colors = airline_colors.b,
   text = function()
       if check_width() then
           return {
               { sep.left_filled, state.mode[2] .. 'Sep' },
               { b_components.file_type({ icon = true }), state.mode[2] },
               { ' ', '' },
           }
       end
       return { { sep.left_filled, state.mode[2] .. 'Sep' } }
   end,
}

utility

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

-- 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 cache_on_buffer function on child component too. But remember it use a same function with a buffer_name_variable so if you pass a different function to a same variable it only use a first function.

child component

child component can be a function too.

comps.modified = {
    hl_colors = {
        default = { 'white', 'red' },
        before = { 'white', 'red' },
        after = { 'white', 'red' },
        bg = { 'black', 'green' },
        red = { 'red', 'black' },
        green = { 'green', 'black' },
    },
    text = function()
        return {
            { sep_ani, 'before' },
            {
                function()
                    if vim.bo.modified or vim.bo.modifiable == false then
                        return ''
                    end
                    return ''
                end,
                function(hl_data)
                    if vim.bo.modified then
                        return hl_data.green
                    elseif vim.bo.modifiable == false or vim.bo.readonly == true then
                        return hl_data.red
                    end
                    return hl_data.bg
                end,
            },
            { sep.slant_right, 'after' },
        }
    end
}

child component highlight can be a function.
but it need to use hl_data an return a hl_name

```lua
comps.modified = {
    hl_colors = {
        default = { 'white', 'red' },
        after = { 'red', 'RightBg' },
        before = { 'white', 'red' },
        bg = { 'black', 'green' },
        red = { 'red', 'black' },
        green = { 'green', 'black' },
    },
    text = function()
        return {
            { sep.slant_right, 'before' },
            {
                function()
                    if vim.bo.modified or vim.bo.modifiable == false then
                        return ''
                    end
                    return ''
                end,
                function(hl_data)
                    if vim.bo.modified then
                        return hl_data.green
                    elseif vim.bo.modifiable == false or vim.bo.readonly == true then
                        return hl_data.red
                    end
                    return hl_data.bg
                end,
            },
            { sep.slant_right, 'after' },
        }
    end,
}

## Builtin component

### Basic
```lua
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
cache_file_icon same of file icon but it cache value on buffer
file_format() get file format
file_modified() get file modified

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 to work.

local lsp_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

Clone this wiki locally