Skip to content

Commit

Permalink
feat: display shortened path as title of floating window (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jan 4, 2023
1 parent 3911e16 commit 9f7c4d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ M.open_float = function(dir)
for k, v in pairs(config.float.win_options) do
vim.api.nvim_win_set_option(winid, k, v)
end
util.add_title_to_win(winid, parent_url)
vim.cmd.edit({ args = { parent_url }, mods = { keepalt = true } })
util.add_title_to_win(winid)
end

---Open oil browser for a directory
Expand Down
19 changes: 15 additions & 4 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,26 @@ M.get_editor_height = function()
end

local winid_map = {}
M.add_title_to_win = function(winid, title, opts)
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
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 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 @@ -351,10 +362,10 @@ M.add_title_to_win = function(winid, title, opts)
if vim.api.nvim_win_get_buf(winid) ~= winbuf then
return
end
local bufname = vim.api.nvim_buf_get_name(winbuf)
local new_title = get_title()
local new_width =
math.min(vim.api.nvim_win_get_width(winid) - 4, 2 + vim.api.nvim_strwidth(bufname))
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { " " .. bufname .. " " })
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 .. " " })
vim.bo[bufnr].modified = false
vim.api.nvim_win_set_width(title_winid, new_width)
local new_col = 1
Expand Down

0 comments on commit 9f7c4d7

Please sign in to comment.