Skip to content

Commit

Permalink
Merge pull request ThePrimeagen#287 from otavioschwanck/feat/add-tabline
Browse files Browse the repository at this point in the history
Tabline with harpoon items
  • Loading branch information
ThePrimeagen authored May 17, 2023
2 parents 3476228 + 5bd1fa3 commit 8cb54c4
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ global_settings = {

-- set marks specific to each git branch inside git repository
mark_branch = false,

-- enable tabline with harpoon marks
tabline = false,
tabline_prefix = " ",
tabline_suffix = " ",
}
```

Expand Down Expand Up @@ -208,6 +213,29 @@ require("harpoon").setup({
})
```


#### Tabline

By default, the tabline will use the default theme of your theme. You can customize by editing the following highlights:

* HarpoonInactive
* HarpoonActive
* HarpoonNumberActive
* HarpoonNumberInactive

Example to make it cleaner:

```lua
vim.cmd('highlight! HarpoonInactive guibg=NONE guifg=#63698c')
vim.cmd('highlight! HarpoonActive guibg=NONE guifg=white')
vim.cmd('highlight! HarpoonNumberActive guibg=NONE guifg=#7aa2f7')
vim.cmd('highlight! HarpoonNumberInactive guibg=NONE guifg=#7aa2f7')
vim.cmd('highlight! TabLineFill guibg=NONE guifg=white')
```

Result:
![tabline](https://i.imgur.com/8i8mKJD.png)

## ⇁ Social
For questions about Harpoon, there's a #harpoon channel on [the Primagen's Discord](https://discord.gg/theprimeagen) server.
* [Discord](https://discord.gg/theprimeagen)
Expand Down
8 changes: 8 additions & 0 deletions lua/harpoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,22 @@ function M.setup(config)
["tmux_autoclose_windows"] = false,
["excluded_filetypes"] = { "harpoon" },
["mark_branch"] = false,
["tabline"] = false,
["tabline_suffix"] = " ",
["tabline_prefix"] = " ",
},
}, expand_dir(c_config), expand_dir(u_config), expand_dir(config))

-- There was this issue where the vim.loop.cwd() didn't have marks or term, but had
-- an object for vim.loop.cwd()
ensure_correct_config(complete_config)

if complete_config.tabline then
require("harpoon.tabline").setup(complete_config)
end

HarpoonConfig = complete_config

log.debug("setup(): Complete config", HarpoonConfig)
log.trace("setup(): log_key", Dev.get_log_key())
end
Expand Down
84 changes: 84 additions & 0 deletions lua/harpoon/tabline.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
local Dev = require("harpoon.dev")
local log = Dev.log

local M = {}

local function get_color(group, attr)
return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(group)), attr)
end


local function shorten_filenames(filenames)
local shortened = {}

local counts = {}
for _, file in ipairs(filenames) do
local name = vim.fn.fnamemodify(file.filename, ":t")
counts[name] = (counts[name] or 0) + 1
end

for _, file in ipairs(filenames) do
local name = vim.fn.fnamemodify(file.filename, ":t")

if counts[name] == 1 then
table.insert(shortened, { filename = vim.fn.fnamemodify(name, ":t") })
else
table.insert(shortened, { filename = file.filename })
end
end

return shortened
end

function M.setup(opts)
function _G.tabline()
local original_tabs = require('harpoon').get_mark_config().marks
local tabs = shorten_filenames(original_tabs)
local tabline = ''

for i, tab in ipairs(original_tabs) do
local is_current = string.match(vim.fn.bufname(), tab.filename) or vim.fn.bufname() == tab.filename

local label = tabs[i].filename


if is_current then
tabline = tabline ..
'%#HarpoonNumberActive#' .. (opts.tabline_prefix or ' ') .. i .. ' %*' .. '%#HarpoonActive#'
else
tabline = tabline ..
'%#HarpoonNumberInactive#' .. (opts.tabline_prefix or ' ') .. i .. ' %*' .. '%#HarpoonInactive#'
end

tabline = tabline .. label .. (opts.tabline_suffix or ' ') .. '%*'
if i < #tabs then
tabline = tabline .. '%T'
end
end

return tabline
end

vim.opt.showtabline = 2

vim.o.tabline = '%!v:lua.tabline()'

vim.api.nvim_create_autocmd("ColorScheme", {
group = vim.api.nvim_create_augroup("harpoon", { clear = true }),
pattern = { "*" },
callback = function()
local color = get_color('HarpoonActive', 'bg#')

if (color == "" or color == nil) then
vim.api.nvim_set_hl(0, "HarpoonInactive", { link = "Tabline" })
vim.api.nvim_set_hl(0, "HarpoonActive", { link = "TablineSel" })
vim.api.nvim_set_hl(0, "HarpoonNumberActive", { link = "TablineSel" })
vim.api.nvim_set_hl(0, "HarpoonNumberInactive", { link = "Tabline" })
end
end,
})

log.debug("setup(): Tabline Setup", opts)
end

return M

0 comments on commit 8cb54c4

Please sign in to comment.