aboutsummaryrefslogtreecommitdiffstats
path: root/lua/tar.lua
blob: cc0b732eb5b13573bb9443f742937748ccbaff55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
local M = {}
local C = {} -- config table

function M.setup(opts)
  opts = opts or {}

  if opts.seperator then
    if type(opts.seperator) == "string" then
      C.seperatorstr = opts.seperator
    elseif type(opts.seperator) == "function" then
      C.seperator = opts.seperator
    end
  else
    C.separator = M.separator
    C.seperatorstr = "|"
  end

  if opts.title then
    if type(opts.title) == "function" then
      C.title = opts.title
    end
  else
    C.title = M.title
  end

  if opts.sign then
    if type(opts.sign) == "function" then
      C.sign = opts.sign
    end
  else
    C.sign = M.sign
  end

  C.noname = opts.noname or "[No Name]"
  C.closeicon = opts.closeicon or "[x]"

  vim.opt.tabline = "%!v:lua.require\'tar\'.tabline()"
end

function M.title(bufnr)
  local fn = vim.fn.bufname(bufnr)

  if fn == '' then
    return C.noname
  else
    return vim.fn.pathshorten(vim.fn.fnamemodify(fn, ':p:~:t'))
  end
end

function M.sign(bufnr)
  if vim.fn.getbufvar(bufnr, '&modified') == 1 then
    return '[+]'
  elseif vim.fn.getbufvar(bufnr, '&readonly') == 1 then
    return '[=]'
  else
    return
  end
end

function M.separator(index)
  return (index < vim.fn.tabpagenr('$') and '%#TabLine#'..C.seperatorstr or '')
end

function M.tab(index)
  local sel = vim.fn.tabpagenr() == index
  local buflist = vim.fn.tabpagebuflist(index)
  local winnr = vim.fn.tabpagewinnr(index)
  local bufnr = buflist[winnr]
  local hl = (sel and '%#TabLineSel#' or '%#TabLine#')

  return hl..'%'..index..'T '..C.title(bufnr)..' '..(C.sign(bufnr)
    and M.sign(bufnr)..' ' or '')..C.separator(index)
end

function M.tabline()
  local line = ''
  for i = 1, vim.fn.tabpagenr('$') do
    line = line..M.tab(i)
  end
  line = line..'%#TabLineFill#%='
  if vim.fn.tabpagenr('$') > 1 then
    line = line..'%#TabLine#%999X'..C.closeicon
  end
  return line
end

return M