Restructure project
This commit is contained in:
103
lua/hardline.lua
103
lua/hardline.lua
@@ -3,11 +3,13 @@
|
||||
-- github.com/ojroques
|
||||
|
||||
-------------------- VARIABLES -----------------------------
|
||||
local cmd, vim = vim.cmd, vim
|
||||
local api, cmd, vim = vim.api, vim.cmd, vim
|
||||
local wo = vim.wo
|
||||
local common = require('hardline.common')
|
||||
local M = {}
|
||||
|
||||
-------------------- OPTIONS -------------------------------
|
||||
M.theme = 'default'
|
||||
M.events = {
|
||||
active = {
|
||||
'BufEnter',
|
||||
@@ -23,75 +25,83 @@ M.events = {
|
||||
'WinLeave',
|
||||
},
|
||||
}
|
||||
|
||||
M.sections = {
|
||||
{class = 'A', item = require('hardline.mode').get_item()}, ' ',
|
||||
{class = 'B', item = require('hardline.git').get_item()}, ' ',
|
||||
{class = 'C', item = require('hardline.filename').get_item()}, ' ',
|
||||
{
|
||||
class = nil,
|
||||
item = require('hardline.parts.mode').get_item()
|
||||
}, ' ',
|
||||
{
|
||||
class = 'B',
|
||||
item = require('hardline.parts.git').get_item()
|
||||
}, ' ',
|
||||
{
|
||||
class = 'C',
|
||||
item = require('hardline.parts.filename').get_item()
|
||||
}, ' ',
|
||||
'%=',
|
||||
-- {class = 'error', item = require('hardline.lsp').get_error()}, ' ',
|
||||
-- {class = 'warning', item = require('hardline.lsp').get_warning()}, ' ',
|
||||
-- {class = 'warning', item = require('hardline.whitespace').get_item()}, ' ',
|
||||
-- {class = 'X', item = require('hardline.wordcount').get_item()}, ' ',
|
||||
{class = 'Y', item = require('hardline.filetype').get_item()}, ' ',
|
||||
{class = 'Z', item = require('hardline.line').get_item()},
|
||||
}
|
||||
|
||||
M.colors = {
|
||||
active = {
|
||||
A = {ctermfg='170',guifg='#C678DD'},
|
||||
},
|
||||
inactive = {
|
||||
A = {ctermfg='170',guifg='#C678DD'},
|
||||
-- {
|
||||
-- class = 'Error',
|
||||
-- item = require('hardline.parts.lsp').get_errors()
|
||||
-- }, ' ',
|
||||
-- {
|
||||
-- class = 'Warning',
|
||||
-- item = require('hardline.parts.lsp').get_warnings()
|
||||
-- }, ' ',
|
||||
-- {
|
||||
-- class = 'Warning',
|
||||
-- item = require('hardline.parts.whitespace').get_item()
|
||||
-- }, ' ',
|
||||
-- {
|
||||
-- class = 'X',
|
||||
-- item = require('hardline.parts.wordcount').get_item()
|
||||
-- }, ' ',
|
||||
{
|
||||
class = 'Y',
|
||||
item = require('hardline.parts.filetype').get_item()
|
||||
}, ' ',
|
||||
{
|
||||
class = 'A',
|
||||
item = require('hardline.parts.line').get_item()
|
||||
},
|
||||
}
|
||||
|
||||
-------------------- HELPERS -------------------------------
|
||||
local function echo(hlgroup, msg)
|
||||
cmd(string.format('echohl %s', hlgroup))
|
||||
cmd(string.format('echo "[hardline] %s"', msg))
|
||||
cmd('echohl None')
|
||||
end
|
||||
|
||||
-------------------- STATUSLINE ----------------------------
|
||||
local function color_item(item, class, active)
|
||||
local mode = active and 'Active' or 'Inactive'
|
||||
if not M.colors[string.lower(mode)][class] then return item end
|
||||
return string.format('%%#Hardline%s%s#%s%%##', mode, class, item)
|
||||
local function color_item(item, class)
|
||||
local mode = common.is_active() and 'active' or 'inactive'
|
||||
if not class or not common.theme.colors[class] then return item end
|
||||
return string.format('%%#Hardline%s%s#%s%%##', class, mode, item)
|
||||
end
|
||||
|
||||
local function reload_section(section, active)
|
||||
local function reload_section(section)
|
||||
if type(section) == 'function' then
|
||||
return section()
|
||||
elseif type(section) == 'string' then
|
||||
return section
|
||||
elseif type(section) == 'table' then
|
||||
return color_item(section.item, section.class, active)
|
||||
return color_item(reload_section(section.item), section.class)
|
||||
end
|
||||
echo('WarningMsg', 'Invalid section.')
|
||||
common.echo('WarningMsg', 'Invalid section.')
|
||||
return ''
|
||||
end
|
||||
|
||||
function M.reload(active)
|
||||
local items = {}
|
||||
for _, section in ipairs(M.sections) do
|
||||
table.insert(items, reload_section(section, active))
|
||||
end
|
||||
wo.statusline = table.concat(items)
|
||||
common.set_active(active)
|
||||
wo.statusline = table.concat(vim.tbl_map(reload_section, M.sections))
|
||||
end
|
||||
|
||||
-------------------- SETUP -----------------------------
|
||||
|
||||
local function set_hlgroups()
|
||||
for _, mode in ipairs({'Active', 'Inactive'}) do
|
||||
local m = string.lower(mode)
|
||||
for class, args in pairs(M.colors[m]) do
|
||||
local a = {}
|
||||
for k, v in pairs(args) do
|
||||
table.insert(a, string.format('%s=%s', k, v))
|
||||
end
|
||||
cmd(string.format('hi Hardline%s%s %s', mode, class, table.concat(a, ' ')))
|
||||
for class, attr in pairs(common.theme.colors) do
|
||||
for mode, args in pairs(attr) do
|
||||
local a = common.build_args(args)
|
||||
cmd(string.format('hi Hardline%s%s %s', class, mode, a))
|
||||
end
|
||||
end
|
||||
for mode, args in pairs(common.theme.mode_colors) do
|
||||
local a = common.build_args(args)
|
||||
cmd(string.format('hi HardlineMode%s %s', mode, a))
|
||||
end
|
||||
end
|
||||
|
||||
local function set_autocmds()
|
||||
@@ -107,6 +117,7 @@ local function set_autocmds()
|
||||
end
|
||||
|
||||
function M.setup(user_opts)
|
||||
common.theme = require(string.format('hardline.themes.%s', M.theme))
|
||||
set_hlgroups()
|
||||
set_autocmds()
|
||||
end
|
||||
|
||||
27
lua/hardline/common.lua
Normal file
27
lua/hardline/common.lua
Normal file
@@ -0,0 +1,27 @@
|
||||
local M = {}
|
||||
|
||||
M.theme = require('hardline.themes.default')
|
||||
|
||||
function M.echo(hlgroup, msg)
|
||||
vim.cmd(string.format('echohl %s', hlgroup))
|
||||
vim.cmd(string.format('echo "[hardline] %s"', msg))
|
||||
vim.cmd('echohl None')
|
||||
end
|
||||
|
||||
function M.build_args(tbl_args)
|
||||
local args = {}
|
||||
for k, v in pairs(tbl_args) do
|
||||
table.insert(args, string.format('%s=%s', k, v))
|
||||
end
|
||||
return table.concat(args, ' ')
|
||||
end
|
||||
|
||||
function M.set_active(active)
|
||||
vim.api.nvim_win_set_var(0, 'hardline_active', active)
|
||||
end
|
||||
|
||||
function M.is_active()
|
||||
return vim.api.nvim_win_get_var(0, 'hardline_active')
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,15 +0,0 @@
|
||||
local vim = vim
|
||||
local bo = vim.bo
|
||||
local M = {}
|
||||
|
||||
function M.get_filetype()
|
||||
return bo.filetype
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
[[%{luaeval('require("hardline.filetype").get_filetype()')}]],
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,31 +0,0 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local g = vim.g
|
||||
local M = {}
|
||||
|
||||
function M.get_hunks()
|
||||
if not g.loaded_gitgutter then
|
||||
return ''
|
||||
end
|
||||
local summary = fn.GitGutterGetHunkSummary()
|
||||
return table.concat({
|
||||
string.format('+%d', summary[1]),
|
||||
string.format('~%d', summary[2]),
|
||||
string.format('-%d', summary[3]),
|
||||
}, ' ')
|
||||
end
|
||||
|
||||
function M.get_branch()
|
||||
if not g.loaded_gitgutter then
|
||||
return ''
|
||||
end
|
||||
return string.format(' %s', fn.FugitiveHead())
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
[[%{luaeval('require("hardline.git").get_hunks()')}]],
|
||||
[[%{luaeval('require("hardline.git").get_branch()')}]],
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,24 +0,0 @@
|
||||
local cmd, fn, vim = vim.cmd, vim.fn, vim
|
||||
local M = {}
|
||||
|
||||
function M.get_line()
|
||||
return string.format('%03d/%03d', fn.line('.'), fn.line('$'))
|
||||
end
|
||||
|
||||
function M.get_column()
|
||||
return string.format('%03d/%03d', fn.col('.'), fn.col('$') - 1)
|
||||
end
|
||||
|
||||
function M.get_percent()
|
||||
return ' %03p%%'
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
[[%{luaeval('require("hardline.line").get_line()')}]], ':',
|
||||
[[%{luaeval('require("hardline.line").get_column()')}]],
|
||||
M.get_percent(),
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,8 +0,0 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local M = {}
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({})
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,42 +0,0 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local o, bo, wo = vim.o, vim.bo, vim.wo
|
||||
local modes = {
|
||||
['?'] = '???',
|
||||
['n'] = 'NORMAL',
|
||||
['i'] = 'INSERT',
|
||||
['R'] = 'REPLACE',
|
||||
['v'] = 'VISUAL',
|
||||
['V'] = 'V-LINE',
|
||||
[''] = 'V-BLOCK',
|
||||
['c'] = 'COMMAND',
|
||||
['s'] = 'SELECT',
|
||||
['S'] = 'S-LINE',
|
||||
[''] = 'S-BLOCK',
|
||||
['t'] = 'TERMINAL',
|
||||
}
|
||||
local M = {}
|
||||
|
||||
function M.get_mode()
|
||||
if not modes[fn.mode()] then return modes['?'] end
|
||||
return modes[fn.mode()]
|
||||
end
|
||||
|
||||
function M.get_paste()
|
||||
if not o.paste then return '' end
|
||||
return ' PASTE'
|
||||
end
|
||||
|
||||
function M.get_spell()
|
||||
if not wo.spell then return '' end
|
||||
return string.format(' SPELL [%s]', string.upper(bo.spelllang))
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
[[%{luaeval('require("hardline.mode").get_mode()')}]],
|
||||
[[%{luaeval('require("hardline.mode").get_paste()')}]],
|
||||
[[%{luaeval('require("hardline.mode").get_spell()')}]],
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,18 +1,17 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local M = {}
|
||||
|
||||
function M.get_name()
|
||||
return vim.fn.expand('%:~:.')
|
||||
end
|
||||
|
||||
function M.get_mode()
|
||||
return ' %h%r'
|
||||
end
|
||||
|
||||
function M.get_name()
|
||||
return fn.expand('%:~:.')
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
'%<',
|
||||
[[%{luaeval('require("hardline.filename").get_name()')}]],
|
||||
[[%{luaeval('require("hardline.parts.filename").get_name()')}]],
|
||||
M.get_mode(),
|
||||
})
|
||||
end
|
||||
13
lua/hardline/parts/filetype.lua
Normal file
13
lua/hardline/parts/filetype.lua
Normal file
@@ -0,0 +1,13 @@
|
||||
local M = {}
|
||||
|
||||
function M.get_filetype()
|
||||
return vim.bo.filetype
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
[[%{luaeval('require("hardline.parts.filetype").get_filetype()')}]],
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
25
lua/hardline/parts/git.lua
Normal file
25
lua/hardline/parts/git.lua
Normal file
@@ -0,0 +1,25 @@
|
||||
local M = {}
|
||||
|
||||
function M.get_hunks()
|
||||
if not vim.g.loaded_gitgutter then return '' end
|
||||
local summary = vim.fn.GitGutterGetHunkSummary()
|
||||
return table.concat({
|
||||
string.format('+%d', summary[1]),
|
||||
string.format('~%d', summary[2]),
|
||||
string.format('-%d', summary[3]),
|
||||
}, ' ')
|
||||
end
|
||||
|
||||
function M.get_branch()
|
||||
if not vim.g.loaded_gitgutter then return '' end
|
||||
return string.format(' %s', vim.fn.FugitiveHead())
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
[[%{luaeval('require("hardline.parts.git").get_hunks()')}]],
|
||||
[[%{luaeval('require("hardline.parts.git").get_branch()')}]],
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
23
lua/hardline/parts/line.lua
Normal file
23
lua/hardline/parts/line.lua
Normal file
@@ -0,0 +1,23 @@
|
||||
local M = {}
|
||||
|
||||
function M.get_line()
|
||||
return string.format('%03d/%03d', vim.fn.line('.'), vim.fn.line('$'))
|
||||
end
|
||||
|
||||
function M.get_column()
|
||||
return string.format('%03d/%03d', vim.fn.col('.'), vim.fn.col('$') - 1)
|
||||
end
|
||||
|
||||
function M.get_percent()
|
||||
return ' %03p%%'
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
[[%{luaeval('require("hardline.parts.line").get_line()')}]], ':',
|
||||
[[%{luaeval('require("hardline.parts.line").get_column()')}]],
|
||||
M.get_percent(),
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
8
lua/hardline/parts/lsp.lua
Normal file
8
lua/hardline/parts/lsp.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
local M = {}
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
47
lua/hardline/parts/mode.lua
Normal file
47
lua/hardline/parts/mode.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
local M = {}
|
||||
local modes = {
|
||||
['?'] = {text = '???', color = 'inactive'},
|
||||
['n'] = {text = 'NORMAL', color = 'normal'},
|
||||
['c'] = {text = 'COMMAND', color = 'normal'},
|
||||
['i'] = {text = 'INSERT', color = 'insert'},
|
||||
['R'] = {text = 'REPLACE', color = 'replace'},
|
||||
['v'] = {text = 'VISUAL', color = 'visual'},
|
||||
['V'] = {text = 'V-LINE', color = 'visual'},
|
||||
[''] = {text = 'V-BLOCK', color = 'visual'},
|
||||
['s'] = {text = 'SELECT', color = 'visual'},
|
||||
['S'] = {text = 'S-LINE', color = 'visual'},
|
||||
[''] = {text = 'S-BLOCK', color = 'visual'},
|
||||
['t'] = {text = 'TERMINAL', color = 'normal'},
|
||||
}
|
||||
|
||||
local function color_item(item)
|
||||
local color = modes[vim.fn.mode()].color
|
||||
return string.format('%%#HardlineMode%s#%s%%##', color, item)
|
||||
end
|
||||
|
||||
function M.get_mode()
|
||||
if not modes[vim.fn.mode()] then
|
||||
return modes['?'].text
|
||||
end
|
||||
return modes[vim.fn.mode()].text
|
||||
end
|
||||
|
||||
function M.get_paste()
|
||||
if not vim.o.paste then return '' end
|
||||
return ' | PASTE'
|
||||
end
|
||||
|
||||
function M.get_spell()
|
||||
if not vim.wo.spell then return '' end
|
||||
return string.format(' | SPELL [%s]', string.upper(vim.bo.spelllang))
|
||||
end
|
||||
|
||||
function M.get_item()
|
||||
return color_item(table.concat({
|
||||
[[%{luaeval('require("hardline.parts.mode").get_mode()')}]],
|
||||
[[%{luaeval('require("hardline.parts.mode").get_paste()')}]],
|
||||
[[%{luaeval('require("hardline.parts.mode").get_spell()')}]],
|
||||
}))
|
||||
end
|
||||
|
||||
return M
|
||||
8
lua/hardline/parts/whitespace.lua
Normal file
8
lua/hardline/parts/whitespace.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
local M = {}
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
8
lua/hardline/parts/wordcount.lua
Normal file
8
lua/hardline/parts/wordcount.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
local M = {}
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
18
lua/hardline/themes/default.lua
Normal file
18
lua/hardline/themes/default.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
local M = {}
|
||||
|
||||
M.colors = {
|
||||
A = {
|
||||
active = {ctermfg='170',guifg='#C678DD'},
|
||||
inactive = {ctermfg='172',guifg='#C6080D'},
|
||||
}
|
||||
}
|
||||
|
||||
M.mode_colors = {
|
||||
inactive = {ctermfg='170',guifg='#C678DD'},
|
||||
normal = {ctermfg='170',guifg='#C678DD'},
|
||||
insert = {ctermfg='170',guifg='#C678DD'},
|
||||
replace = {ctermfg='170',guifg='#C678DD'},
|
||||
visual = {ctermfg='170',guifg='#C678DD'},
|
||||
}
|
||||
|
||||
return M
|
||||
0
lua/hardline/themes/one.lua
Normal file
0
lua/hardline/themes/one.lua
Normal file
@@ -1,8 +0,0 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local M = {}
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({})
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,8 +0,0 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local M = {}
|
||||
|
||||
function M.get_item()
|
||||
return table.concat({})
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user