From 53921c4f2b2ef868b1bed70bc11ab330b0ad4a47 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 28 Nov 2024 11:53:51 -0600 Subject: readme updated and minitab fixed --- lua/minitab.lua | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ lua/minitab/init.lua | 82 ---------------------------------------------------- 2 files changed, 81 insertions(+), 82 deletions(-) create mode 100644 lua/minitab.lua delete mode 100644 lua/minitab/init.lua (limited to 'lua') diff --git a/lua/minitab.lua b/lua/minitab.lua new file mode 100644 index 0000000..17acbbf --- /dev/null +++ b/lua/minitab.lua @@ -0,0 +1,81 @@ +local M = { + moduleNames = { "init.lua", "__init__.py" }, + separatorC = "|", + modifiedC = "+", +} + +M.title = function(bufnr) + local file = vim.fn.simplify(vim.fn.bufname(bufnr)) + local tail = vim.fn.fnamemodify(file, ':t') + local buftype = vim.fn.getbufvar(bufnr, '&buftype') + local filetype = vim.fn.getbufvar(bufnr, '&filetype') + + if buftype == "help" then + return "help:" .. vim.fn.fnamemodify(tail, ':r') + elseif filetype == "netrw" then + return file == "" and "netrw" or vim.fn.fnamemodify(file, ':p:~') + elseif buftype ~= "" then + return buftype + elseif file == "" then + return filetype == "" and "[No Name]" or filetype + elseif vim.tbl_contains(M.moduleNames, tail) then + return vim.fn.fnamemodify(vim.fn.fnamemodify(file, ':p:~:h'), ':t') .. "/" .. tail -- e.g. minitab/init.lua + end + + return tail +end + +M.modified = function(bufnr) + return vim.fn.getbufvar(bufnr, '&modified') == 1 and M.modifiedC or "" +end + +M.windowCount = function(index, hl) + local nwins = #vim.tbl_filter(function(bufnr) + return vim.fn.win_gettype(vim.fn.bufwinid(bufnr)) == "" -- windows will appear or disappear at random when editing files etc for no discernible reason + end, vim.fn.tabpagebuflist(index)) + return nwins > 1 and '%#TabLineSel#' .. nwins .. hl or "" +end + +M.separator = function(index, hl) + return (index < vim.fn.tabpagenr('$') and '%#TabLine#' .. M.separatorC or "") +end + +M.cell = function(index) + local isSelected = vim.fn.tabpagenr() == index + local buflist = vim.fn.tabpagebuflist(index) + local winnr = vim.fn.tabpagewinnr(index) + local bufnr = buflist[winnr] + local hl = (isSelected and '%#TabLineSel#' or '%#TabLine#') + + local prefix = M.windowCount(index, hl) .. M.modified(bufnr) .. " " + return hl .. '%' .. index .. 'T' .. " " .. + (prefix == " " and "" or prefix) .. + M.title(bufnr) .. " " .. + M.separator(index) +end + +M.tabline = function() + local line = "" + if vim.fn.tabpagenr('$') <= 1 then + return line + end + for i = 1, vim.fn.tabpagenr('$'), 1 do + line = line .. M.cell(i) + end + return line .. '%#TabLineFill#%=%#TabLine#%999XX' +end + +local setup = function(opts) + if opts then + for key, value in pairs(opts) do + M[key] = value + end + end + + vim.opt.tabline = "%!v:lua.require'minitab'.helpers.tabline()" +end + +return { + helpers = M, + setup = setup, +} diff --git a/lua/minitab/init.lua b/lua/minitab/init.lua deleted file mode 100644 index 22beb44..0000000 --- a/lua/minitab/init.lua +++ /dev/null @@ -1,82 +0,0 @@ -local M = { - moduleNames = { "init.lua", "__init__.py" }, - separatorC = "|", - modifiedC = "+", -} - -M.title = function(bufnr, isSelected) - local file = vim.fn.simplify(vim.fn.bufname(bufnr)) - local tail = vim.fn.fnamemodify(file, ':t') - local buftype = vim.fn.getbufvar(bufnr, '&buftype') - local filetype = vim.fn.getbufvar(bufnr, '&filetype') - - if buftype == "help" then - return "help:" .. vim.fn.fnamemodify(tail, ':r') - elseif filetype == "netrw" then - return file == "" and "netrw" or vim.fn.fnamemodify(file, ':p:~') - elseif buftype ~= "" then - return buftype - elseif file == "" then - return filetype == "" and "[No Name]" or filetype - elseif vim.tbl_contains(M.moduleNames, tail) then - return vim.fn.fnamemodify(vim.fn.fnamemodify(file, ':p:~:h'), ':t') .. "/" .. tail -- e.g. minitab/init.lua - end - - return tail -end - -M.modified = function(bufnr) - return vim.fn.getbufvar(bufnr, '&modified') == 1 and M.modifiedC or "" -end - -M.windowCount = function(index, hl) - local nwins = #vim.tbl_filter(function(winnr) - return vim.fn.win_gettype(winnr) == "" -- windows will appear or disappear at random when editing files etc for no discernible reason - end, vim.api.nvim_tabpage_list_wins(index)) - return nwins > 1 and '%#TabLineSel#' .. nwins .. hl or "" -end - -M.separator = function(index, hl) - return (index < vim.fn.tabpagenr('$') and '%#TabLine#' .. M.separatorC or "") -end - -M.cell = function(index) - local isSelected = vim.fn.tabpagenr() == index - local buflist = vim.fn.tabpagebuflist(index) - local winnr = vim.fn.tabpagewinnr(index) - local bufnr = buflist[winnr] - local hl = (isSelected and '%#TabLineSel#' or '%#TabLine#') - - local prefix = M.windowCount(index, hl) .. M.modified(bufnr) .. " " - return hl .. '%' .. index .. 'T' .. " " .. - (prefix == " " and "" or prefix) .. - M.title(bufnr, isSelected) .. " " .. - M.separator(index) -end - -M.tabline = function() - local line = "" - for i = 1, vim.fn.tabpagenr('$'), 1 do - line = line .. M.cell(i) - end - line = line .. '%#TabLineFill#%=' - if vim.fn.tabpagenr("$") > 1 then - line = line .. '%#TabLine#%999XX' - end - return line -end - -local setup = function(opts) - if opts then - for key, value in pairs(opts) do - M[key] = value - end - end - - vim.opt.tabline = "%!v:lua.require'minitab'.helpers.tabline()" -end - -return { - helpers = M, - setup = setup, -} -- cgit v1.2.3