127 lines
3.1 KiB
Markdown
127 lines
3.1 KiB
Markdown
# Recipes
|
|
|
|
Have a cool recipe to share? Open a pull request and add it to this doc!
|
|
|
|
<!-- TOC -->
|
|
|
|
- [Toggle file detail view](#toggle-file-detail-view)
|
|
- [Show CWD in the winbar](#show-cwd-in-the-winbar)
|
|
- [Hide gitignored files and show git tracked hidden files](#hide-gitignored-files-and-show-git-tracked-hidden-files)
|
|
|
|
<!-- /TOC -->
|
|
|
|
## Toggle file detail view
|
|
|
|
```lua
|
|
local detail = false
|
|
require("oil").setup({
|
|
keymaps = {
|
|
["gd"] = {
|
|
desc = "Toggle file detail view",
|
|
callback = function()
|
|
detail = not detail
|
|
if detail then
|
|
require("oil").set_columns({ "icon", "permissions", "size", "mtime" })
|
|
else
|
|
require("oil").set_columns({ "icon" })
|
|
end
|
|
end,
|
|
},
|
|
},
|
|
})
|
|
```
|
|
|
|
## Show CWD in the winbar
|
|
|
|
```lua
|
|
-- Declare a global function to retrieve the current directory
|
|
function _G.get_oil_winbar()
|
|
local bufnr = vim.api.nvim_win_get_buf(vim.g.statusline_winid)
|
|
local dir = require("oil").get_current_dir(bufnr)
|
|
if dir then
|
|
return vim.fn.fnamemodify(dir, ":~")
|
|
else
|
|
-- If there is no current directory (e.g. over ssh), just show the buffer name
|
|
return vim.api.nvim_buf_get_name(0)
|
|
end
|
|
end
|
|
|
|
require("oil").setup({
|
|
win_options = {
|
|
winbar = "%!v:lua.get_oil_winbar()",
|
|
},
|
|
})
|
|
```
|
|
|
|
## Hide gitignored files and show git tracked hidden files
|
|
|
|
```lua
|
|
-- helper function to parse output
|
|
local function parse_output(proc)
|
|
local result = proc:wait()
|
|
local ret = {}
|
|
if result.code == 0 then
|
|
for line in vim.gsplit(result.stdout, "\n", { plain = true, trimempty = true }) do
|
|
-- Remove trailing slash
|
|
line = line:gsub("/$", "")
|
|
ret[line] = true
|
|
end
|
|
end
|
|
return ret
|
|
end
|
|
|
|
-- build git status cache
|
|
local function new_git_status()
|
|
return setmetatable({}, {
|
|
__index = function(self, key)
|
|
local ignore_proc = vim.system(
|
|
{ "git", "ls-files", "--ignored", "--exclude-standard", "--others", "--directory" },
|
|
{
|
|
cwd = key,
|
|
text = true,
|
|
}
|
|
)
|
|
local tracked_proc = vim.system({ "git", "ls-tree", "HEAD", "--name-only" }, {
|
|
cwd = key,
|
|
text = true,
|
|
})
|
|
local ret = {
|
|
ignored = parse_output(ignore_proc),
|
|
tracked = parse_output(tracked_proc),
|
|
}
|
|
|
|
rawset(self, key, ret)
|
|
return ret
|
|
end,
|
|
})
|
|
end
|
|
local git_status = new_git_status()
|
|
|
|
-- Clear git status cache on refresh
|
|
local refresh = require("oil.actions").refresh
|
|
local orig_refresh = refresh.callback
|
|
refresh.callback = function(...)
|
|
git_status = new_git_status()
|
|
orig_refresh(...)
|
|
end
|
|
|
|
require("oil").setup({
|
|
view_options = {
|
|
is_hidden_file = function(name, bufnr)
|
|
local dir = require("oil").get_current_dir(bufnr)
|
|
local is_dotfile = vim.startswith(name, ".") and name ~= ".."
|
|
-- if no local directory (e.g. for ssh connections), just hide dotfiles
|
|
if not dir then
|
|
return is_dotfile
|
|
end
|
|
-- dotfiles are considered hidden unless tracked
|
|
if is_dotfile then
|
|
return not git_status[dir].tracked[name]
|
|
else
|
|
-- Check if file is gitignored
|
|
return git_status[dir].ignored[name]
|
|
end
|
|
end,
|
|
},
|
|
})
|
|
```
|