From fa6aa3c3244abc06d016c64a67169ce5f2862008 Mon Sep 17 00:00:00 2001 From: hoob3rt Date: Wed, 30 Dec 2020 15:48:51 +0100 Subject: [PATCH] initial lualine setup --- .luacheckrc | 4 + lua/lualine.lua | 108 +++++++++++++++++++++ lua/lualine/components/branch.lua | 25 +++++ lua/lualine/components/encoding.lua | 6 ++ lua/lualine/components/extensions/fzf.lua | 17 ++++ lua/lualine/components/fileformat.lua | 6 ++ lua/lualine/components/filename.lua | 5 + lua/lualine/components/filetype.lua | 23 +++++ lua/lualine/components/location.lua | 5 + lua/lualine/components/mode.lua | 18 ++++ lua/lualine/components/progress.lua | 5 + lua/lualine/highlight.lua | 47 +++++++++ lua/lualine/themes/gruvbox.lua | 111 ++++++++++++++++++++++ lua/lualine/utils.lua | 33 +++++++ 14 files changed, 413 insertions(+) create mode 100644 .luacheckrc create mode 100644 lua/lualine.lua create mode 100644 lua/lualine/components/branch.lua create mode 100644 lua/lualine/components/encoding.lua create mode 100644 lua/lualine/components/extensions/fzf.lua create mode 100644 lua/lualine/components/fileformat.lua create mode 100644 lua/lualine/components/filename.lua create mode 100644 lua/lualine/components/filetype.lua create mode 100644 lua/lualine/components/location.lua create mode 100644 lua/lualine/components/mode.lua create mode 100644 lua/lualine/components/progress.lua create mode 100644 lua/lualine/highlight.lua create mode 100644 lua/lualine/themes/gruvbox.lua create mode 100644 lua/lualine/utils.lua diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..f97f58b --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,4 @@ +globals = { + "vim", + "vimp", +} diff --git a/lua/lualine.lua b/lua/lualine.lua new file mode 100644 index 0000000..6912f3f --- /dev/null +++ b/lua/lualine.lua @@ -0,0 +1,108 @@ +local utils = require('lualine.utils') +local highlight = require('lualine.highlight') + +local M = { } + +M.theme = 'gruvbox' + +M.separator = '|' + +M.sections = { + lualine_a = { 'mode' }, + lualine_b = { 'branch' }, + lualine_c = { 'filename' }, + lualine_x = { 'encoding', 'fileformat', 'filetype' }, + lualine_y = { 'progress' }, + lualine_z = { 'location' }, + lualine_diagnostics = { } +} + +M.inactiveSections = { + lualine_a = { }, + lualine_b = { }, + lualine_c = { 'filename' }, + lualine_x = { 'location' }, + lualine_y = { }, + lualine_z = { } +} + +M.extensions = { +} + +local function loadComponents() + local function loadSections(sections) + for _, section in pairs(sections) do + for index, component in pairs(section) do + if type(component) == 'string' then + section[index] = require('lualine.components.' .. component) + end + end + end + end + loadSections(M.sections) + loadSections(M.inactiveSections) +end + +local function loadExtensions() + for _, extension in pairs(M.extensions) do + if type(extension) == 'string' then + require('lualine.components.extensions.' .. extension).loadExtension() + end + if type(extension) == 'table' then + extension.loadExtension() + end + if type(extension) == 'function' then + extension() + end + end +end + +local function StatusLine(isFocused) + local sections = M.sections + if not isFocused then + sections = M.inactiveSections + end + if type(M.theme) == 'string' then + M.theme = utils.setTheme(M.theme) + highlight.createHighlightGroups(M.theme) + end + local status = '' + if sections.lualine_a ~= nil then + status = status .. highlight.formatHighlight(isFocused, 'lualine_a') + status = status .. utils.drawSection(sections.lualine_a, M.separator) + end + if sections.lualine_b ~= nil then + status = status .. highlight.formatHighlight(isFocused, 'lualine_b') + status = status .. utils.drawSection(sections.lualine_b, M.separator) + end + if sections.lualine_c ~= nil then + status = status .. highlight.formatHighlight(isFocused, 'lualine_c') + status = status .. utils.drawSection(sections.lualine_c, M.separator) + end + status = status .. [[%=]] + if sections.lualine_x ~= nil then + status = status .. highlight.formatHighlight(isFocused, 'lualine_c') + status = status .. utils.drawSection(sections.lualine_x, M.separator) + end + if sections.lualine_y ~= nil then + status = status .. highlight.formatHighlight(isFocused, 'lualine_b') + status = status .. utils.drawSection(sections.lualine_y, M.separator) + end + if sections.lualine_z ~= nil then + status = status .. highlight.formatHighlight(isFocused, 'lualine_a') + status = status .. utils.drawSection(sections.lualine_z, M.separator) + end + return status +end + +function M.status() + loadComponents() + loadExtensions() + _G.statusline = StatusLine + vim.cmd([[autocmd WinEnter,BufEnter * setlocal statusline=%!v:lua.statusline(1)]]) + vim.cmd([[autocmd WinLeave,BufLeave * setlocal statusline=%!v:lua.statusline()]]) +end + +M.status() + +return M diff --git a/lua/lualine/components/branch.lua b/lua/lualine/components/branch.lua new file mode 100644 index 0000000..05a3cad --- /dev/null +++ b/lua/lualine/components/branch.lua @@ -0,0 +1,25 @@ +local function Branch() + local branch = vim.fn.systemlist( + 'cd '..vim.fn.expand('%:p:h:S')..' 2>/dev/null && git status --porcelain -b 2>/dev/null')[1] + if not branch or #branch == 0 then + return '' + end + branch = branch:gsub([[^## No commits yet on (%w+)$]], '%1') + branch = branch:gsub([[^##%s+(%w+).*$]], '%1') + local ok,devicons = pcall(require,'nvim-web-devicons') + if ok then + local icon = devicons.get_icon('git') + if icon ~= nil then + return icon .. ' ' .. branch + end + return branch + end + ok = (vim.fn.exists('*WebDevIconsGetFileTypeSymbol')) + if ok then + local icon = '' + return icon .. ' ' .. branch + end + return branch +end + +return Branch diff --git a/lua/lualine/components/encoding.lua b/lua/lualine/components/encoding.lua new file mode 100644 index 0000000..a548b87 --- /dev/null +++ b/lua/lualine/components/encoding.lua @@ -0,0 +1,6 @@ +local function Encoding() + local encoding = [[%{strlen(&fenc)?&fenc:&enc}]] + return encoding +end + +return Encoding diff --git a/lua/lualine/components/extensions/fzf.lua b/lua/lualine/components/extensions/fzf.lua new file mode 100644 index 0000000..1e18341 --- /dev/null +++ b/lua/lualine/components/extensions/fzf.lua @@ -0,0 +1,17 @@ +local function fzfStatusline() + vim.cmd([[hi clear fzf1]]) + vim.cmd([[hi link fzf1 lualine_a_normal]]) + vim.cmd([[hi clear fzf2]]) + vim.cmd([[hi link fzf2 lualine_c_normal]]) + return ([[%#fzf1# FZF %#fzf2#]]) + +end + +local function loadExtension() + _G.fzfStatusline = fzfStatusline + vim.cmd(([[autocmd! User FzfStatusLine setlocal statusline=%!v:lua.fzfStatusline()]])) +end + +return { + loadExtension = loadExtension +} diff --git a/lua/lualine/components/fileformat.lua b/lua/lualine/components/fileformat.lua new file mode 100644 index 0000000..4012018 --- /dev/null +++ b/lua/lualine/components/fileformat.lua @@ -0,0 +1,6 @@ +local function FileFormat() + local fileformat = [[%{&ff}]] + return fileformat +end + +return FileFormat diff --git a/lua/lualine/components/filename.lua b/lua/lualine/components/filename.lua new file mode 100644 index 0000000..be5bb12 --- /dev/null +++ b/lua/lualine/components/filename.lua @@ -0,0 +1,5 @@ +local function FileName() + return [[%t %m]] +end + +return FileName diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua new file mode 100644 index 0000000..4994c3c --- /dev/null +++ b/lua/lualine/components/filetype.lua @@ -0,0 +1,23 @@ +local function Filetype() + local filetype = vim.bo.filetype + if filetype:len() > 0 then + local ok,devicons = pcall(require,'nvim-web-devicons') + if ok then + local f_name,f_extension = vim.fn.expand('%:t'),vim.fn.expand('%:e') + local icon = devicons.get_icon(f_name,f_extension) + if icon ~= nil then + return icon .. ' ' .. filetype + end + return filetype + end + ok = (vim.fn.exists('*WebDevIconsGetFileTypeSymbol')) + if ok then + local icon = vim.call('WebDevIconsGetFileTypeSymbol') + return icon .. ' ' .. filetype + end + return filetype + end + return '' +end + +return Filetype diff --git a/lua/lualine/components/location.lua b/lua/lualine/components/location.lua new file mode 100644 index 0000000..2de53a9 --- /dev/null +++ b/lua/lualine/components/location.lua @@ -0,0 +1,5 @@ +local function Location() + return[[%3l:%-2c]] +end + +return Location diff --git a/lua/lualine/components/mode.lua b/lua/lualine/components/mode.lua new file mode 100644 index 0000000..6814ca1 --- /dev/null +++ b/lua/lualine/components/mode.lua @@ -0,0 +1,18 @@ +local function Mode() + local mode_map = { + ['__'] = '------', + ['n'] = 'NORMAL', + ['i'] = 'INSERT', + ['v'] = 'VISUAL', + ['V'] = 'V-LINE', + [''] = 'V-BLOCK', + ['R'] = 'REPLACE', + ['r'] = 'REPLACE', + ['Rv'] = 'V-REPLACE', + ['c'] = 'COMMAND', + ['t'] = 'TERMINAL', + } + return mode_map[vim.fn.mode()] +end + +return Mode diff --git a/lua/lualine/components/progress.lua b/lua/lualine/components/progress.lua new file mode 100644 index 0000000..aac92d8 --- /dev/null +++ b/lua/lualine/components/progress.lua @@ -0,0 +1,5 @@ +local function Progress() + return [[%3P]] +end + +return Progress diff --git a/lua/lualine/highlight.lua b/lua/lualine/highlight.lua new file mode 100644 index 0000000..8ff99b1 --- /dev/null +++ b/lua/lualine/highlight.lua @@ -0,0 +1,47 @@ +local M = { } + +local function highlight (name, foreground, background, special) + if special == nil then + special = 'none' + end + local command = 'highlight ' + command = command .. name .. ' ' + command = command .. 'guifg=' .. foreground .. ' ' + command = command .. 'guibg=' .. background .. ' ' + if special then + command = command .. 'gui=' .. special .. ' ' + end + return command +end + +function M.createHighlightGroups(theme) + for mode, sections in pairs(theme) do + for section, colorscheme in pairs(sections) do + if section == 'a' then + vim.cmd(highlight('lualine_' .. section .. '_' .. mode, colorscheme.fg, colorscheme.bg ,'bold')) + else + vim.cmd(highlight('lualine_' .. section .. '_' .. mode, colorscheme.fg, colorscheme.bg )) + end + end + end +end + +function M.formatHighlight(isFocused, highlighGroup) + local mode = require('lualine.components.mode')() + highlighGroup = [[%#]] .. highlighGroup + if not isFocused then + highlighGroup = highlighGroup .. [[_inactive]] + else + if mode == 'V-BLOCK' or mode == 'V-LINE' then + highlighGroup = highlighGroup .. '_visual' + elseif mode == 'V-REPLACE' then + highlighGroup = highlighGroup .. '_replace' + else + highlighGroup = highlighGroup .. '_' .. mode:lower() + end + end + highlighGroup = highlighGroup .. [[#]] + return highlighGroup +end + +return M diff --git a/lua/lualine/themes/gruvbox.lua b/lua/lualine/themes/gruvbox.lua new file mode 100644 index 0000000..29e1db5 --- /dev/null +++ b/lua/lualine/themes/gruvbox.lua @@ -0,0 +1,111 @@ +local M = { } + +local Colors = { + black = "#282828", + white = '#ebdbb2', + red = '#fb4934', + green = '#b8bb26', + blue = '#83a598', + yellow = '#fe8019', + + gray = '#a89984', + darkgray = '#3c3836', + + lightgray = '#504945', + inactivegray = '#7c6f64', +} + +M.normal = { + a = { + bg = Colors.gray, + fg = Colors.black, + }, + b = { + bg = Colors.lightgray, + fg = Colors.white, + }, + c = { + bg = Colors.darkgray, + fg = Colors.gray + } +} + +M.insert = { + a = { + bg = Colors.blue, + fg = Colors.black, + }, + b = { + bg = Colors.lightgray, + fg = Colors.white, + }, + c = { + bg = Colors.lightgray, + fg = Colors.white + } +} + + +M.visual = { + a = { + bg = Colors.yellow, + fg = Colors.black, + }, + b = { + bg = Colors.lightgray, + fg = Colors.white, + }, + c = { + bg = Colors.inactivegray, + fg = Colors.black + }, +} + +M.replace = { + a = { + bg = Colors.red, + fg = Colors.black, + }, + b = { + bg = Colors.lightgray, + fg = Colors.white, + }, + c = { + bg = Colors.black, + fg = Colors.white + }, +} + +M.command = { + a = { + bg = Colors.green, + fg = Colors.black, + }, + b = { + bg = Colors.lightgray, + fg = Colors.white, + }, + c = { + bg = Colors.inactivegray, + fg = Colors.black + }, +} + +M.terminal = M.normal + +M.inactive = { + a = { + bg = Colors.darkgray, + fg = Colors.gray, + }, + b = { + bg = Colors.darkgray, + fg = Colors.gray, + }, + c = { + bg = Colors.darkgray, + fg = Colors.gray + }, +} + +return M diff --git a/lua/lualine/utils.lua b/lua/lualine/utils.lua new file mode 100644 index 0000000..83a8f8b --- /dev/null +++ b/lua/lualine/utils.lua @@ -0,0 +1,33 @@ +local M = { } + +function M.setTheme(theme) + return require('lualine.themes.'..theme) +end + +function M.drawSection(section, separator) + local status = '' + for index, statusFunction in pairs(section) do + local localstatus = statusFunction() + if localstatus:len() > 0 then + if separator:len() > 0 then + if index > 1 then + status = status .. separator .. ' ' + end + status = status .. localstatus + status = status .. ' ' + else + status = status .. localstatus + status = status .. ' ' + end + end + end + if status:len() > 0 then + if separator:len() > 0 and table.maxn(section) > 1 then + return ' ' .. status .. ' ' + end + return ' ' .. status + end + return '' +end + +return M