From 60db657343eb2d4aa6e69cec4da15bfe4955b42a Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 15 Jun 2022 04:50:28 +0200 Subject: vim: tweak alpha again don't load by default --- .vim/lua/plugins.lua | 154 ++++++++++++++++++++++++++------------------------- 1 file changed, 79 insertions(+), 75 deletions(-) (limited to '.vim/lua/plugins.lua') diff --git a/.vim/lua/plugins.lua b/.vim/lua/plugins.lua index 6d109d8..2a2643c 100644 --- a/.vim/lua/plugins.lua +++ b/.vim/lua/plugins.lua @@ -961,102 +961,105 @@ require('packer').startup({function() } end} -- }}} - use {'goolord/alpha-nvim', -- {{{ + use {'goolord/alpha-nvim', cmd = 'Alpha', module = {'alpha'}, -- {{{ config = function() + local MAX_WIDTH = 80 local alpha = require('alpha') local them = require('alpha.themes.theta') - local MAX_WIDTH = 80 - local header = require('alpha.themes.dashboard').section.header - local button = function(...) + local button = function(...) -- {{{ local b = require('alpha.themes.dashboard').button(...) b.opts = vim.tbl_extend('force', b.opts, { hl = 'Normal', width = MAX_WIDTH, }) return b - end - local pad = function(str, max_width) - return str .. string.rep(' ', ((max_width or vim.opt.columns:get()) - vim.fn.strdisplaywidth(str))) - end - local hr = function(margin, max_width) -- {{{ - return { - type = 'text', - val = string.rep(' ', margin) .. string.rep('─', max_width - (2*margin)), - opts = { - position = 'center', - hl = 'Comment', - } - } end -- }}} - local fortune = function(margin, max_width) -- {{{ + local fit_path = function(path, max_width) -- {{{ + local Path = require('plenary.path') + path = vim.fn.fnamemodify(path, ':.') + if vim.fn.strdisplaywidth(path) > max_width then + path = Path.new(path):shorten(1, {-2, -1}) + end + if vim.fn.strdisplaywidth(path) > max_width then + path = '_ ' .. path:sub((vim.fn.strdisplaywidth(path) - max_width), -1) + end + return path + end -- }}} + local hr = function(margin, max_width, label, opts) -- {{{ + max_width = max_width - (2*margin) + label = label and (' ' .. label .. ' ') or '' + opts = vim.tbl_extend('force', opts or {}, { position = 'center', hl = 'Comment' }) return { type = 'text', + opts = opts, val = function() - local Job = require('plenary.job') - local ok, j = pcall(Job.new, Job, { - command = 'fortune', - args = {vim.env.HOME..'/devel/projects/zig/fortune/fortunes'}, - enable_recording = true, - }) - if not ok then return {} end - local lines, exit_code = j:sync() - if not exit_code == 0 then return {} end - local new = {} - for i = 1, #lines do - if not (vim.trim(lines[i]) == '') then - local line = string.rep(' ', margin) .. lines[i] - new[#new+1] = line - end + local label_len = vim.fn.strdisplaywidth(label) + -- exit here if we're not showing a label + if label_len == 0 then + return string.rep(' ', margin) .. string.rep('─', max_width) .. string.rep(' ', margin) + end + if label_len > max_width then + return error('overfull box: ' .. label) end - return new - -- return require('alpha.fortune')(max_width) + max_width = max_width - label_len + return string.rep(' ', margin) .. string.rep('─', max_width/2) .. label .. string.rep('─', max_width/2) .. string.rep(' ', margin) end, - opts = { - position = 'center', - hl = 'AlphaFortune', - }, } end -- }}} - local here = function(max_width) -- {{{ + local fortune = function(margin, max_width) -- {{{ + max_width = max_width - (margin*2) + local Job = require('plenary.job') + local ok, j = pcall(Job.new, Job, { + command = 'sh', + args = {'-c', 'fortune ' .. vim.env.HOME..'/devel/projects/zig/fortune/fortunes' .. ' | fmt -w ' .. max_width}, + enable_recording = true, + }) + if not ok then return {} end + local lines, exit_code = j:sync() + if not exit_code == 0 then return {} end + local val = {} + for i = 1, #lines do + if vim.fn.strdisplaywidth(lines[i]) > max_width then + return error('overful box: ' .. lines[i]) + end + table.insert(val, + (string.rep(' ', margin) .. lines[i] .. string.rep(' ', margin))) + end + if vim.trim(val[#val]) == '' then table.remove(val, #val) end return { type = 'text', - val = function() return pad('❱ ' .. vim.loop.cwd(), max_width) end, + val = val, opts = { position = 'center', - hl = 'Title', - } + hl = 'AlphaFortune', + }, } end -- }}} local mru = function(max_width) -- {{{ return { type = 'group', val = function() - local cwd = vim.loop.cwd() + local items = {} + + -- initialize the oldfiles table local oldfiles = {} + local cwd = vim.loop.cwd() for _, v in pairs(vim.v.oldfiles) do if #oldfiles == 10 then break end if vim.startswith(v, cwd) and (vim.fn.filereadable(v) == 1) then table.insert(oldfiles, v) end end + -- exit early if there's nothing to show if not oldfiles or #oldfiles == 0 then return {} end - local items = {} - table.insert(items, { - type = 'text', - val = pad('MRU:', max_width), - opts = { position = 'center', hl = 'Comment' } - }) + + table.insert(items, hr(0, max_width, 'MRU: ' .. fit_path(cwd, max_width))) + table.insert(items, { type = 'padding', val = 1 }) for i, fn in pairs(oldfiles) do - local shortfn = vim.fn.fnamemodify(fn, ':.') - if vim.fn.strdisplaywidth(shortfn) > max_width then - shortfn = require('plenary.path').new(shortfn):shorten(1, {-2, -1}) - end - if vim.fn.strdisplaywidth(shortfn) > max_width then - local over = vim.fn.strdisplaywidth(shortfn) - max_width - over = over + 4 - shortfn = '_ ' .. shortfn:sub(over+1, -1) - end - table.insert(items, button(tostring(i-1), shortfn, ":e " .. fn .. " ")) + table.insert(items, button( + tostring(i-1), -- keybind is + fit_path(fn, max_width), + ":edit " .. fn .. " ")) end return items end, @@ -1070,24 +1073,27 @@ require('packer').startup({function() type = 'group', val = function() local items = {} + + -- get listed buffers local bufnrs = vim.tbl_filter(function(bufnr) if 1 ~= vim.fn.buflisted(bufnr) then return false end return true end, vim.api.nvim_list_bufs()) + + --sort by last_used table.sort(bufnrs, function(a, b) return vim.fn.getbufinfo(a)[1].lastused > vim.fn.getbufinfo(b)[1].lastused end) + + -- exit early if there's nothing to show if not bufnrs or #bufnrs == 0 then return {} end - -- add padding and hr + + table.insert(items, {type = 'padding', val = 1}) + table.insert(items, hr(0, max_width, 'Buffers')) table.insert(items, {type = 'padding', val = 1}) - table.insert(items, hr(0, MAX_WIDTH)) - table.insert(items, { - type = 'text', - val = pad('Buffers:', max_width), - opts = { position = 'center', hl = 'Comment' } - }) + for _, bufnr in ipairs(bufnrs) do local bufpath = require('plenary.path').new(vim.fn.getbufinfo(bufnr)[1].name):shorten(1, {-2, -1}) table.insert(items, button('b'..bufnr, bufpath, ':b'..bufnr..'')) @@ -1099,15 +1105,15 @@ require('packer').startup({function() }, } end -- }}} - them.config.layout = { + them.config.layout = { -- {{{ {type = 'padding', val = 2}, - header, + require('alpha.themes.dashboard').section.header, {type = 'padding', val = 1}, - fortune(0, MAX_WIDTH), + fortune(5, MAX_WIDTH), {type = 'padding', val = 1}, hr(0, MAX_WIDTH), {type = 'padding', val = 1}, - {type = 'group', val = { + {type = 'group', val = { -- buttons {{{ button('e', '󰈔 new', [[ene startinsert]]), button('r', '󰈸 live grep', [[lua require('misc').live_grep()]]), button('f', '󰝰 find files', [[lua require('misc').find_files()]]), @@ -1117,13 +1123,11 @@ require('packer').startup({function() button('u', '󰚰 update plugins', [[lua require('packer').sync()]]), button('v', '󰂓 edit config', [[lua require('telescope.builtin').find_files {search_dirs={vim.env.HOME..'/.vim/'}}]]), button('M', '󰇮 mail', [[Himalaya]]), - }}, - {type = 'padding', val = 1}, - here(MAX_WIDTH), + }}, -- }}} {type = 'padding', val = 1}, mru(MAX_WIDTH), buffers(MAX_WIDTH), - } + } -- }}} alpha.setup(them.config) do -- cgit 1.4.1