From 5d2dfae655b9b689bd4017b3bdccd52cbee5b92f Mon Sep 17 00:00:00 2001 From: Philipp Oeschger <49947694+PhilippOesch@users.noreply.github.com> Date: Wed, 16 Oct 2024 04:22:31 +0200 Subject: [PATCH] feat: config option to customize floating window title (#482) * 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 --- doc/oil.txt | 2 ++ lua/oil/config.lua | 4 ++++ lua/oil/init.lua | 15 +-------------- lua/oil/util.lua | 35 ++++++++++++++++++++++------------- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/doc/oil.txt b/doc/oil.txt index 2c1a1073..43c52d26 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -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. diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 065dbbf6..5ccde173 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -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. @@ -332,6 +334,7 @@ local M = {} ---@field max_height integer ---@field border string|string[] ---@field win_options table +---@field get_win_title fun(winid: integer): string ---@field preview_split "auto"|"left"|"right"|"above"|"below" ---@field override fun(conf: table): table @@ -341,6 +344,7 @@ local M = {} ---@field max_height? integer ---@field border? string|string[] Window border ---@field win_options? table +---@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 diff --git a/lua/oil/init.lua b/lua/oil/init.lua index e8b4ddc5..8c04d8bc 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -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", { @@ -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, diff --git a/lua/oil/util.lua b/lua/oil/util.lua index aa717ade..f5c63e2a 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -360,6 +360,26 @@ 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 {} @@ -367,21 +387,10 @@ M.add_title_to_win = function(winid, opts) 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 @@ -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 .. " " })