repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

custom tabline and statusline

8bb690cd1c6df9245bc07839cb84f5d211ecc1fa

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (7)

neovim/init.lua

Modified
35 35 -- My cheatsheet is an initial buffer (if none was specified) where I put stuff to remember/learn.
36 36 -- Keybinds, commands, todos are all fair game.
37 -local cheatsheet, setup_cheatsheet = require("cheatsheet")()
37 +local cheatsheet = require("cheatsheet").print
38 +local setup_cheatsheet = require("cheatsheet").setup
38 39 cheatsheet("")
39 40 cheatsheet("Stuff for bugabinga to remember about NeoVim")
40 41 cheatsheet("")
41 42 local settings = require("general_editor_settings")
42 43 settings(autocommand, data_path)
45 +-- setup my custom tabline
46 +local setup_tabline = require("tabline").setup
47 +setup_tabline()
49 +-- setup my custom statusline
50 +local setup_statusline = require("statusline").setup
51 +setup_statusline(autocommand)
44 53 -- setup my own keybindings
45 54 require("keybinds")(cheatsheet)
47 56 -- some file specific settings
48 57 require("filetype.yaml")(autocommand)
49 58 require("filetype.ansi_c")(autocommand)
50 -local _, setup_mkdir = require("mkdir")()
59 +local setup_mkdir = require("mkdir").setup
51 60 setup_mkdir(autocommand)
53 62 -- Load nvim plugins

neovim/lua/cheatsheet.lua

Modified
4 4 -- The cheatsheet module takes over the initial buffer of neovim and displays my personal cheat sheet
5 5 -- and other stuff I wish to remember about neovim.
6 6 -- Calling this module returns two functions.
7 7 -- The first one can be used to add to the cheatsheet buffer.
8 8 -- Throughout the configuration code, this function is expected to be called and given lines of text to display.
9 9 -- The second one sets up the custom intro screen, if nvim was started without any arguments.
10 -return function()
11 - local cheatsheet_lines = { 'CHEATSHEET VIM COMMANDS' }
12 - local is_setup = false
13 - return function(message)
14 - table.insert(cheatsheet_lines, message)
15 - if is_setup and vim.api.nvim_buf_get_name(0):match 'cheatsheet' then
16 - vim.cmd [[noautocmd setlocal modifiable]]
17 - -- Adds message by appending to the end of current buffer
18 - vim.api.nvim_buf_set_lines(0, -1, -1, false, { message })
19 - vim.cmd [[noautocmd setlocal nomodifiable nomodified]]
20 - end
21 - end,
22 - function()
23 - if
24 - -- program arguments were given
25 - vim.fn.argc() ~= 0
26 - -- the buffer is not empty
27 - or vim.fn.line2byte '$' ~= -1
28 - -- vim is already in insert mode
29 - or vim.o.insertmode
30 - -- buffer is not modifiable
31 - or not vim.o.modifiable
32 - then
33 - -- simply do nothing, which results in the normal nvim behaviour
34 - return
35 - end
36 - -- disables the default intro screen
37 - vim.o.shortmess = vim.o.shortmess .. 'I'
38 - -- ignore all autocommand event while we setup our start screen
39 - vim.o.eventignore = 'all'
40 - vim.cmd [[noautocmd silent! setlocal bufhidden=wipe colorcolumn= foldcolumn=0 matchpairs= nobuflisted nocursorcolumn nocursorline nolist nonumber norelativenumber nospell noswapfile signcolumn=no synmaxcol& statusline=cheatsheet filetype=intro]]
41 - -- add lines here to buffer
42 - vim.api.nvim_buf_set_name(0, 'cheatsheet')
43 - vim.api.nvim_buf_set_lines(
44 - 0,
45 - 0,
46 - #cheatsheet_lines,
47 - false,
48 - cheatsheet_lines
49 - )
50 - vim.cmd [[noautocmd setlocal nomodifiable nomodified]]
51 - vim.cmd [[silent! %foldopen!]]
52 - vim.cmd [[normal! zb]]
53 - -- when entering insert mode, open new buffer
54 - vim.cmd [[nnoremap <buffer><nowait><silent> i :enew <bar> startinsert<cr>]]
55 - vim.o.eventignore = ''
57 - -- remember if the cheatsheet was already set up, so that the messenger function can instead modify the buffer
58 - is_setup = true
59 - end
60 -end
11 +local cheatsheet_lines = { "CHEATSHEET VIM COMMANDS" }
12 +local is_setup = false
14 +return {
15 + print = function(message)
16 + table.insert(cheatsheet_lines, message)
17 + if is_setup and vim.api.nvim_buf_get_name(0):match("cheatsheet") then
18 + vim.cmd([[noautocmd setlocal modifiable]])
19 + -- Adds message by appending to the end of current buffer
20 + vim.api.nvim_buf_set_lines(0, -1, -1, false, { message })
21 + vim.cmd([[noautocmd setlocal nomodifiable nomodified]])
22 + end
23 + end,
24 + setup = function()
25 + if
26 + -- program arguments were given
27 + vim.fn.argc() ~= 0
28 + -- the buffer is not empty
29 + or vim.fn.line2byte("$") ~= -1
30 + -- vim is already in insert mode
31 + or vim.o.insertmode
32 + -- buffer is not modifiable
33 + or not vim.o.modifiable
34 + then
35 + -- simply do nothing, which results in the normal nvim behaviour
36 + return
37 + end
38 + -- disables the default intro screen
39 + vim.o.shortmess = vim.o.shortmess .. "I"
40 + -- ignore all autocommand event while we setup our start screen
41 + vim.o.eventignore = "all"
42 + vim.cmd(
43 + [[noautocmd silent! setlocal bufhidden=wipe colorcolumn= foldcolumn=0 matchpairs= nobuflisted nocursorcolumn nocursorline nolist nonumber norelativenumber nospell noswapfile signcolumn=no synmaxcol& statusline=cheatsheet filetype=intro]]
44 + )
45 + -- add lines here to buffer
46 + vim.api.nvim_buf_set_name(0, "cheatsheet")
47 + vim.api.nvim_buf_set_lines(0, 0, #cheatsheet_lines, false, cheatsheet_lines)
48 + vim.cmd([[noautocmd setlocal nomodifiable nomodified]])
49 + vim.cmd([[silent! %foldopen!]])
50 + vim.cmd([[normal! zb]])
51 + -- when entering insert mode, open new buffer
52 + vim.cmd([[nnoremap <buffer><nowait><silent> i :enew <bar> startinsert<cr>]])
53 + vim.o.eventignore = ""
55 + -- remember if the cheatsheet was already set up, so that the messenger function can instead modify the buffer
56 + is_setup = true
57 + end,

neovim/lua/general_editor_settings.lua

Modified
9 9 -- show status line and tabline
10 10 vim.opt.laststatus = 2
11 11 vim.opt.showtabline = 2
12 - -- set tabline to a custom rendering
13 - vim.opt.tabline = [[%!v:lua.require'tabline'()]]
14 12 -- convert tabs to spaces
15 13 vim.opt.expandtab = true
16 14 -- Keep indentation of new lines in line with previuos lines

neovim/lua/mkdir.lua

Modified
1 --- Returns two functions.
1 +-- Returns a table with two functions.
2 2 -- The first function creates any missing intermediate directory for the current buffer.
3 3 -- The second function takes in the `autocommand` function and sets up the first function to automatically run before save.
5 5 -- Setup this module like so:
6 6 -- ```
7 --- local _, setup_mkdir = require'mkdir'()
7 +-- local setup_mkdir = require'mkdir'.setup
8 8 -- setup_mkdir(autocommand)
9 9 -- ```
10 -return function()
11 - return function()
12 - -- base directory of current buffer
13 - local directory = vim.fn.expand("%:p:h")
14 - if vim.fn.isdirectory(directory) == 0 then
15 - vim.fn.mkdir(directory, 'p')
16 - end
17 - end,
18 - function(autocommand)
19 - autocommand{setup_mkdir_before_save = [[ BufWritePre * lua require'mkdir'() ]]}
20 - end
21 -end
10 +return {
11 + run = function()
12 + -- base directory of current buffer
13 + local directory = vim.fn.expand("%:p:h")
14 + if vim.fn.isdirectory(directory) == 0 then
15 + vim.fn.mkdir(directory, "p")
16 + end
17 + end,
18 + setup = function(autocommand)
19 + autocommand({ setup_mkdir_before_save = [[ BufWritePre * lua require'mkdir'.run() ]] })
20 + end,

neovim/lua/plugins.lua

Modified
135 135 "nvim-treesitter/playground",
136 136 config = function()
137 - local cheatsheet = require("cheatsheet")
137 + local cheatsheet = require("cheatsheet").print
138 + cheatsheet("")
138 139 cheatsheet(
139 140 ":TSHighlightCapturesUnderCursor => This command shows the highlight group under the cursor, when TreeSitter is used"

neovim/lua/statusline.lua

Modified
1 -return function() end
1 +local branch_icon = ""
2 +local vcs_branch_name = ""
3 +local special_windows = {
4 + intro = { "CheatSheet", "" },
5 + NvimTree = { "Files", "" },
6 + packer = { "Plugins", "" },
7 + help = { "Help", "龎" },
8 +}
9 +local lsp_symbols = { Error = "", Information = "", Warning = "", Hint = "" }
11 +local get_lsp_diagnostics_count = function()
12 + local diagnostics_count = ""
13 + for type, icon in pairs(lsp_symbols) do
14 + local count = vim.lsp.diagnostic.get_count(0, type)
15 + local highlight = "%#LspDiagnosticsDefault" .. type .. "#"
16 + local rendered_count = count > 0 and highlight .. icon .. " " .. count .. " " or ""
17 + diagnostics_count = diagnostics_count .. rendered_count
18 + end
19 + return diagnostics_count
20 +end
22 +return {
23 + -- refresh the statuslines of all windows.
24 + -- the current window gets special treatment. it is "active".
25 + update_statuslines = function()
26 + for _, window_number in pairs(vim.api.nvim_list_wins()) do
27 + if vim.api.nvim_get_current_win() == window_number then
28 + vim.opt.statusline = [[%!v:lua.require'statusline'.render("current")]]
29 + elseif vim.api.nvim_buf_get_name(0) ~= "" then
30 + vim.opt.statusline = [[%!v:lua.require'statusline'.render()]]
31 + end
32 + end
33 + end,
34 + -- update the vcs branch name, if vcs is available
35 + update_vcs_branch_name = function()
36 + if not pcall(require, "plenary") then
37 + return ""
38 + end
39 + require("plenary.job")
40 + :new({
41 + command = "git",
42 + args = { "branch", "--show-current" },
43 + cwd = vim.fn.expand("%:p:h"), -- parent directory of current buffer
44 + on_exit = function(output)
45 + local result = output:result()[1]
46 + if result then
47 + vcs_branch_name = "%#LineNr#%=" .. branch_icon .. " " .. result
48 + else
49 + vcs_branch_name = ""
50 + end
51 + end,
52 + })
53 + :start()
54 + end,
55 + -- generate the statusline string
56 + render = function(active)
57 + local filetype = vim.bo.filetype
58 + local special_window = special_windows[filetype]
59 + if active and special_window then
60 + return "%#LineNr#%=" .. special_window[2] .. " " .. special_window[1] .. "%="
61 + end
62 + local edited = vim.bo.mod and "" or ""
63 + local modifiable = vim.bo.modifiable and "" or ""
64 + local diagnostics = get_lsp_diagnostics_count()
66 + local statusline = "%#Statusline"
67 + .. (active and "" or "NC")
68 + .. "#"
69 + .. edited
70 + .. " "
71 + .. modifiable
72 + .. " "
73 + .. "%F" -- full file path
74 + .. (diagnostics ~= "" and "  " .. diagnostics .. "  " or " ")
75 + .. (active and vcs_branch_name or "")
77 + return statusline
78 + end,
79 + -- setup a custom statusline
80 + setup = function(autocommand)
81 + autocommand({
82 + update_statusline = "CursorHold,CursorHoldI * lua require'statusline'.update_statuslines()",
83 + update_vcs_branch_name = "BufEnter,BufWinEnter,WinEnter,BufReadPost * lua require'statusline'.update_vcs_branch_name()",
84 + })
85 + end,

neovim/lua/tabline.lua

Modified
1 -return function()
2 - local buffer_names_to_ignore = {"NvimTree"}
3 - local get_icon = function(name, extension)
4 - if pcall(require, "nvim-web-devicons") then return require"nvim-web-devicons".get_icon(name ,extension, {default=true}) end
5 - return ""
6 - end
7 - local tabline = ""
8 - for _, buffer_number in pairs(vim.api.nvim_list_bufs()) do
9 - if vim.api.nvim_buf_is_valid(buffer_number) and vim.api.nvim_buf_is_loaded(buffer_number) then
10 - local modified = vim.bo.modified and "" or " "
11 - -- we only care about the last part of the path
12 - local stylish_buffer_name = vim.api.nvim_buf_get_name(buffer_number):match(".-([^\\/]-)$") or ""
13 - if vim.tbl_contains(buffer_names_to_ignore, stylish_buffer_name) then goto continue end
14 - local icon = ""
15 - if stylish_buffer_name == "" then
16 - stylish_buffer_name = "no name"
17 - icon = ""
18 - elseif stylish_buffer_name == "cheatsheet" then
1 +-- this module renders and sets up a custom tabline.
2 +-- this tabline is similar to the default one except:
3 +-- * it only shows simple base file names
4 +-- * the modified state has a different icon
5 +-- * it uses nvim-web-devicons, if available
7 +-- gets a file icon based on extension and/or name
8 +-- tries to use nvim-web-devicons, otherwise falls back to some default icon
9 +local get_icon = function(name, extension)
10 + if pcall(require, "nvim-web-devicons") then return require"nvim-web-devicons".get_icon(name ,extension, {default=true}) end
11 + return ""
12 +end
14 +-- this function renders the tabline and is meant to be set as an expression into the `tabline` option
15 +return
16 + {
17 + render = function()
18 + local buffer_names_to_ignore = {'NvimTree'}
19 + local tabline = ""
20 + -- FIXME change this to tabpage instead of buf
21 + for _, buffer_number in pairs(vim.api.nvim_list_bufs()) do
22 + if vim.api.nvim_buf_is_valid(buffer_number) and vim.api.nvim_buf_is_loaded(buffer_number) then
23 + local modified = vim.bo.modified and "" or " "
24 + -- we only care about the last part of the path
25 + local stylish_buffer_name = vim.api.nvim_buf_get_name(buffer_number):match(".-([^\\/]-)$") or ""
26 + if vim.tbl_contains(buffer_names_to_ignore, stylish_buffer_name) or stylish_buffer_name == "" then goto continue end
27 + local icon = ""
28 +--FIXME this is the wrong way to handle new empty buffers: how to do it whithout showing tabs for all kinds of auxillary buffers (checkhealt for example)
29 +--[[ if stylish_buffer_name == "" then
30 +stylish_buffer_name = "no name"
31 + icon = "" ]]
32 + if stylish_buffer_name == "cheatsheet" then
19 33 icon = ""
20 34 else
21 35 local extension = stylish_buffer_name:match("%w+%.(.+)")
22 36 icon = get_icon(stylish_buffer_name, extension)
23 - end
24 - stylish_buffer_name = " " .. stylish_buffer_name .. " "
25 - local is_current_buffer = vim.api.nvim_get_current_buf() == buffer_number
26 - tabline = tabline .. "%#TabLine" ..
27 - (is_current_buffer and "Sel" or "") .. "#" ..
28 - " " .. icon .. stylish_buffer_name .. (is_current_buffer and modified or " ")
29 - end
30 - -- goto here to return early from one iteration step of the for loop
31 - ::continue::
32 - end
33 - return tabline .. "%#TabLineFill#"
37 + end
38 + stylish_buffer_name = " " .. stylish_buffer_name .. " "
39 + local is_current_buffer = vim.api.nvim_get_current_buf() == buffer_number
40 + tabline = tabline .. "%#TabLine" ..
41 + (is_current_buffer and "Sel" or "") .. "#" ..
42 + " " .. icon .. stylish_buffer_name .. (is_current_buffer and modified or " ")
43 + end
44 + -- goto here to return early from one iteration step of the for loop
45 + ::continue::
46 + end
47 + return tabline .. "%#TabLineFill#"
48 +end,
49 + setup = function()
50 + -- set tabline to a custom rendering
51 + -- sets the tabline with some crazy vimscript syntax?
52 + -- my guess: this reevaluates this code snippet any time nvim requests a fresh tabline
53 + vim.opt.tabline = [[%!v:lua.require'tabline'.render()]]