Skip to content

Commit

Permalink
feat: config option to customize floating window title (#482)
Browse files Browse the repository at this point in the history
* replace cwd path in actual path

* move get_title to utils

* add documentation

* rename

* add method doc

* add comment

* fallback to 0 for winid

* add missing property definition for relative_win_title

* only replace when at the start of the path

* simplify

* minor change

* add entry point to customize floating window title for oil-buffer

* remove config parameter

* cleanup

* add documentation

* move get_win_title to top and pass winid as parameter

* add get_win_title to type definition for oil.setup

* remove empty line

* adjust comment

---------

Co-authored-by: Philipp Oeschger <[email protected]>
  • Loading branch information
PhilippOesch and Philipp Oeschger authored Oct 16, 2024
1 parent ccab9d5 commit 5d2dfae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
2 changes: 2 additions & 0 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ CONFIG *oil-confi
win_options = {
winblend = 0,
},
-- optionally override the oil buffers window title with custom function: fun(path: string): string
get_win_title = nil,
-- preview_split: Split direction: "auto", "left", "right", "above", "below".
preview_split = "auto",
-- This is the config that will be passed to nvim_open_win.
Expand Down
4 changes: 4 additions & 0 deletions lua/oil/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ local default_config = {
win_options = {
winblend = 0,
},
-- optionally override the oil buffers window title with custom function: fun(winid: integer): string
get_win_title = nil,
-- preview_split: Split direction: "auto", "left", "right", "above", "below".
preview_split = "auto",
-- This is the config that will be passed to nvim_open_win.
Expand Down Expand Up @@ -332,6 +334,7 @@ local M = {}
---@field max_height integer
---@field border string|string[]
---@field win_options table<string, any>
---@field get_win_title fun(winid: integer): string
---@field preview_split "auto"|"left"|"right"|"above"|"below"
---@field override fun(conf: table): table

Expand All @@ -341,6 +344,7 @@ local M = {}
---@field max_height? integer
---@field border? string|string[] Window border
---@field win_options? table<string, any>
---@field get_win_title? fun(winid: integer): string
---@field preview_split? "auto"|"left"|"right"|"above"|"below" Direction that the preview command will split the window
---@field override? fun(conf: table): table

Expand Down
15 changes: 1 addition & 14 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,6 @@ M.open_float = function(dir)
})
)

---Recalculate the window title for the current buffer
local function get_title()
local src_buf = vim.api.nvim_win_get_buf(winid)
local title = vim.api.nvim_buf_get_name(src_buf)
local scheme, path = util.parse_url(title)
if config.adapters[scheme] == "files" then
assert(path)
local fs = require("oil.fs")
title = vim.fn.fnamemodify(fs.posix_to_os_path(path), ":~")
end
return title
end

table.insert(
autocmds,
vim.api.nvim_create_autocmd("BufWinEnter", {
Expand All @@ -324,7 +311,7 @@ M.open_float = function(dir)
col = cur_win_opts.col,
width = cur_win_opts.width,
height = cur_win_opts.height,
title = get_title(),
title = util.get_title(winid),
})
end
end,
Expand Down
35 changes: 22 additions & 13 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -360,28 +360,37 @@ M.is_floating_win = function(winid)
return vim.api.nvim_win_get_config(winid or 0).relative ~= ""
end

---Recalculate the window title for the current buffer
---@param winid nil|integer
---@return string
M.get_title = function(winid)
if config.float.get_win_title ~= nil then
return config.float.get_win_title(winid or 0)
end

local src_buf = vim.api.nvim_win_get_buf(winid or 0)
local title = vim.api.nvim_buf_get_name(src_buf)
local scheme, path = M.parse_url(title)

if config.adapters[scheme] == "files" then
assert(path)
local fs = require("oil.fs")
title = vim.fn.fnamemodify(fs.posix_to_os_path(path), ":~")
end
return title
end

local winid_map = {}
M.add_title_to_win = function(winid, opts)
opts = opts or {}
opts.align = opts.align or "left"
if not vim.api.nvim_win_is_valid(winid) then
return
end
local function get_title()
local src_buf = vim.api.nvim_win_get_buf(winid)
local title = vim.api.nvim_buf_get_name(src_buf)
local scheme, path = M.parse_url(title)
if config.adapters[scheme] == "files" then
assert(path)
local fs = require("oil.fs")
title = vim.fn.fnamemodify(fs.posix_to_os_path(path), ":~")
end
return title
end
-- HACK to force the parent window to position itself
-- See https://github.com/neovim/neovim/issues/13403
vim.cmd.redraw()
local title = get_title()
local title = M.get_title(winid)
local width = math.min(vim.api.nvim_win_get_width(winid) - 4, 2 + vim.api.nvim_strwidth(title))
local title_winid = winid_map[winid]
local bufnr
Expand Down Expand Up @@ -429,7 +438,7 @@ M.add_title_to_win = function(winid, opts)
if vim.api.nvim_win_get_buf(winid) ~= winbuf then
return
end
local new_title = get_title()
local new_title = M.get_title(winid)
local new_width =
math.min(vim.api.nvim_win_get_width(winid) - 4, 2 + vim.api.nvim_strwidth(new_title))
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { " " .. new_title .. " " })
Expand Down

0 comments on commit 5d2dfae

Please sign in to comment.