Search code examples
vimluaneovim

vim-startify configuration in lua with custom functions


I'm trying to migrate my vimrc configuration to lua, and I'm stuck on migrating my vim-startify configuration. In particular, how can I write the gitModified and gitUntracked lists?

vimscript:

function! s:gitModified()
    let files = systemlist('git ls-files -m 2>/dev/null')
    return map(files, "{'line': v:val, 'path': v:val}")
endfunction

function! s:gitUntracked()
    let files = systemlist('git ls-files -o --exclude-standard 2>/dev/null')
    return map(files, "{'line': v:val, 'path': v:val}")
endfunction

let g:startify_lists = [
        \ { 'type': 'dir',       'header': ['   MRU '. getcwd()] },
        \ { 'type': 'sessions',  'header': ['   Sessions']       },
        \ { 'type': 'bookmarks', 'header': ['   Bookmarks']      },
        \ { 'type': function('s:gitModified'),  'header': ['   git modified']},
        \ { 'type': function('s:gitUntracked'), 'header': ['   git untracked']},
        \ { 'type': 'commands',  'header': ['   Commands']       },
        \ ]

My current lua:

vim.g.startify_lists = {
  { type = "commands", header = { "    Commands" } }, -- Commands from above
  { type = "dir", header = { "    MRU " .. vim.fn.getcwd() } }, -- MRU files from CWD
  { type = "sessions",  header = {"   Sessions"} },
  { type = "bookmarks", header = {"   Bookmarks"} },
}

Here I'm missing the two Git related items.


Solution

  • Here's what I did:

    function CommandToStartifyTable(command)
        return function()
            local cmd_output = vim.fn.systemlist(command .. " 2>/dev/null")
            local files =
                vim.tbl_map(
                function(v)
                    return {line = v, path = v}
                end,
                cmd_output
            )
            return files
        end
    end
    
    vim.g.startify_lists = {
        {type = "dir", header = {"   MRU " .. vim.fn.fnamemodify(vim.fn.getcwd(), ":t")}},
        {type = CommandToStartifyTable("git ls-files -m"), header = {"   Git modified"}},
        {type = CommandToStartifyTable("git ls-files -o --exclude-standard"), header = {"   Git untracked"}}
    }