1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 07:20:04 +08:00

feat(statusline): add lua statusline

This commit is contained in:
Eric Wong 2024-06-09 10:37:42 +08:00
parent d22a341fc0
commit 6b7c5a14ce
9 changed files with 1439 additions and 62 deletions

View File

@ -316,7 +316,6 @@ function! s:apply_custom_leader_keybindings() abort
endfor
endfunction
function! s:disable_welcome() abort
augroup SPwelcome
au!

View File

@ -21,13 +21,120 @@
" - `major_mode_cache`: Enable/disable major mode cache, enabled by default.
scriptencoding utf-8
if exists('g:_spacevim_statusline_loaded')
finish
endif
let g:_spacevim_statusline_loaded = 1
if has('nvim-0.10.0')
" 核心逻辑移至 lua兼容 VIML 函数接口
function! SpaceVim#layers#core#statusline#winnr(id) abort
return v:lua.require('spacevim.plugin.statusline').winnr(a:id)
endfunction
function! SpaceVim#layers#core#statusline#get(...) abort
if a:0 > 0
return v:lua.require('spacevim.plugin.statusline').get(a:1)
else
return v:lua.require('spacevim.plugin.statusline').get()
endif
endfunction
function! SpaceVim#layers#core#statusline#init() abort
return v:lua.require('spacevim.plugin.statusline').init()
endfunction
function! SpaceVim#layers#core#statusline#def_colors() abort
return v:lua.require('spacevim.plugin.statusline').def_colors()
endfunction
function! SpaceVim#layers#core#statusline#register_mode(mode) abort
if has_key(a:mode, 'func') && type(a:mode.func) == 2
let mode = a:mode
let mode.func = string(a:mode.func)[10:-3]
return v:lua.require('spacevim.plugin.statusline').register_mode(mode)
else
return v:lua.require('spacevim.plugin.statusline').register_mode(a:mode)
endif
endfunction
function! SpaceVim#layers#core#statusline#toggle_mode(name) abort
return v:lua.require('spacevim.plugin.statusline').toggle_mode(a:name)
endfunction
function! SpaceVim#layers#core#statusline#toggle_section(name) abort
return v:lua.require('spacevim.plugin.statusline').toggle_section(a:name)
endfunction
function! SpaceVim#layers#core#statusline#rsep() abort
return v:lua.require('spacevim.plugin.statusline').rsep()
endfunction
function! SpaceVim#layers#core#statusline#set_variable(var) abort
return v:lua.require('spacevim.plugin.statusline').set_variable(a:var)
endfunction
function! SpaceVim#layers#core#statusline#config() abort
return v:lua.require('spacevim.plugin.statusline').config()
endfunction
function! SpaceVim#layers#core#statusline#ctrlp(focus, byfname, regex, prev, item, next, marked) abort
return v:lua.require('spacevim.plugin.statusline').ctrlp(a:focus, a:byfname, a:regex, a:prev, a:item, a:next, a:marked)
endfunction
function! SpaceVim#layers#core#statusline#ctrlp_status(str) abort
return v:lua.require('spacevim.plugin.statusline').ctrlp_status(a:str)
endfunction
function! SpaceVim#layers#core#statusline#jump(i) abort
return v:lua.require('spacevim.plugin.statusline').jump(a:i)
endfunction
function! SpaceVim#layers#core#statusline#mode(mode) abort
return v:lua.require('spacevim.plugin.statusline').mode(a:mode)
endfunction
function! SpaceVim#layers#core#statusline#mode_text(mode) abort
return v:lua.require('spacevim.plugin.statusline').mode_text(a:mode)
endfunction
function! SpaceVim#layers#core#statusline#denite_status(argv) abort
return v:lua.require('spacevim.plugin.statusline').denite_status(a:argv)
endfunction
function! SpaceVim#layers#core#statusline#denite_mode() abort
return v:lua.require('spacevim.plugin.statusline').denite_mode()
endfunction
function! SpaceVim#layers#core#statusline#unite_mode() abort
return v:lua.require('spacevim.plugin.statusline').unite_mode()
endfunction
function! SpaceVim#layers#core#statusline#register_sections(name, func) abort
return v:lua.require('spacevim.plugin.statusline').register_sections(a:name, string(a:func)[10:-3])
endfunction
function! SpaceVim#layers#core#statusline#check_section(name) abort
return v:lua.require('spacevim.plugin.statusline').check_section(a:name)
endfunction
function! SpaceVim#layers#core#statusline#remove_section(name) abort
return v:lua.require('spacevim.plugin.statusline').remove_section(a:name)
endfunction
function! SpaceVim#layers#core#statusline#health() abort
return v:lua.require('spacevim.plugin.statusline').health()
endfunction
finish
endif
" APIs
let s:MESSLETTERS = SpaceVim#api#import('messletters')
let s:TIME = SpaceVim#api#import('time')
@ -414,7 +521,6 @@ function! s:filesize() abort
return printf('%.1f', l:size/1024.0/1024.0/1024.0) . 'g '
endif
endfunction
function! SpaceVim#layers#core#statusline#get(...) abort
for nr in range(1, winnr('$'))
call setwinvar(nr, 'winwidth', winwidth(nr))

View File

@ -1,5 +1,9 @@
augroup signify_config
autocmd!
autocmd User Signify let &l:statusline = SpaceVim#layers#core#statusline#get(1)
if has('nvim-0.9.5')
autocmd User Signify let lua require('spacevim.plugin.statusline').active()
else
autocmd User Signify let &l:statusline = SpaceVim#layers#core#statusline#get(1)
endif
augroup END

View File

@ -0,0 +1,21 @@
local M = {}
M.__aliases = {
typescript = 'TypeScript',
typescriptreact = 'TypeScript React',
python = 'Python',
java = 'Java',
smalltalk = 'SmallTalk',
objc = 'Objective-C',
postscript = 'PostScript',
}
function M.get_alias(ft)
if ft and ft ~= '' and M.__aliases[ft] then
return M.__aliases[ft]
else
return ft
end
end
return M

View File

@ -6,83 +6,91 @@
-- License: GPLv3
--=============================================================================
local M = {}
function M.circled_letter(c)
local nr = vim.fn.char2nr(c)
if nr - 64 >= 1 and nr - 64 <= 26 then
return vim.fn.nr2char(9397 + nr - 64)
elseif nr - 96 >= 1 and nr - 96 <= 26 then
return vim.fn.nr2char(9423 + nr - 96)
else
return ''
end
end
function M.bubble_num(num, t)
local list = {}
local list = {}
table.insert(list, {'', '', '', '', '', '', '', '', '', ''})
table.insert(list, {'', '', '', '', '', '', '', '', '', ''})
table.insert(list, {'', '', '', '', '', '', '', '', '', ''})
table.insert(list, { '', '', '', '', '', '', '', '', '', '' })
table.insert(list, { '', '', '', '', '', '', '', '', '', '' })
table.insert(list, { '', '', '', '', '', '', '', '', '', '' })
local n = ''
local n = ''
pcall(function ()
n = list[t + 1][num]
end)
pcall(function()
n = list[t + 1][num]
end)
return n
return n
end
function M.circled_num(num, t)
local nr2char = vim.fn.nr2char
local range = vim.fn.range
local index = vim.fn.index
if t == 0 then
if num == 0 then
return nr2char(9471)
elseif index(range(1, 10), num) ~= -1 then
return nr2char(10102 + num - 1)
elseif index(range(11, 20), num) ~= -1 then
return nr2char(9451 + num - 11)
else
return ''
end
elseif t == 1 then
if index(range(20), num) ~= -1 then
if num == 0 then
return nr2char(9450)
else
return nr2char(9311 + num)
end
else
return ''
end
elseif t == 2 then
if index(range(1, 10), num) ~= -1 then
return nr2char(9461 + num - 1)
else
return ''
end
elseif t == 3 then
return num
local nr2char = vim.fn.nr2char
local range = vim.fn.range
local index = vim.fn.index
if t == 0 then
if num == 0 then
return nr2char(9471)
elseif index(range(1, 10), num) ~= -1 then
return nr2char(10102 + num - 1)
elseif index(range(11, 20), num) ~= -1 then
return nr2char(9451 + num - 11)
else
return ''
end
elseif t == 1 then
if index(range(20), num) ~= -1 then
if num == 0 then
return nr2char(9450)
else
return nr2char(9311 + num)
end
else
return ''
end
elseif t == 2 then
if index(range(1, 10), num) ~= -1 then
return nr2char(9461 + num - 1)
else
return ''
end
elseif t == 3 then
return num
end
end
function M.index_num(num)
local nums = {8304, 185, 178, 179, 8308, 8309, 8310, 8311, 8312, 8313}
if vim.fn.index(vim.fn.range(1, 10), num) ~= -1 then
return vim.fn.nr2char(nums[num + 1])
end
return ''
local nums = { 8304, 185, 178, 179, 8308, 8309, 8310, 8311, 8312, 8313 }
if vim.fn.index(vim.fn.range(1, 10), num) ~= -1 then
return vim.fn.nr2char(nums[num + 1])
end
return ''
end
function M.parenthesized_num(num)
if vim.fn.index(vim.fn.range(1, 20), num) ~= -1 then
return vim.fn.nr2char(9331 + num)
else
return ''
end
if vim.fn.index(vim.fn.range(1, 20), num) ~= -1 then
return vim.fn.nr2char(9331 + num)
else
return ''
end
end
function M.num_period(num)
if vim.fn.index(vim.fn.range(1, 20), num) ~= -1 then
return vim.fn.nr2char(9351 + num)
else
return ''
end
if vim.fn.index(vim.fn.range(1, 20), num) ~= -1 then
return vim.fn.nr2char(9351 + num)
else
return ''
end
end
return M

11
lua/spacevim/api/time.lua Normal file
View File

@ -0,0 +1,11 @@
local M = {}
function M.current_time()
return vim.fn.strftime('%I:%M %p')
end
function M.current_date()
return vim.fn.strftime('%a %b %d')
end
return M

View File

@ -47,4 +47,8 @@ function M.executable(bin)
return vim.fn.executable(bin) == 1
end
function M.is_qf_win(winnr)
end
return M

View File

@ -52,4 +52,89 @@ function M.close_float()
end
end
function M.check_width(len, sec, winwidth)
return len + M.len(sec) < winwidth
end
function M.len(sec)
if not sec then return 0 end
local str = vim.fn.matchstr(sec, '%{.*}')
if vim.fn.empty(str) == 0 then
local pos = vim.fn.match(str, '}')
return vim.fn.len(sec) - vim.fn.len(str) + vim.fn.len(vim.fn.eval(string.sub(str, 3, pos))) + 4
else
return vim.fn.len(sec) + 4
end
end
function M.eval(sec)
return vim.fn.substitute(sec, '%{.*}', '', 'g')
end
function M.build(left_sections, right_sections, lsep, rsep, fname, tag, hi_a, hi_b, hi_c, hi_z, winwidth)
local l = '%#' .. hi_a .. '#' .. left_sections[1]
l = l .. '%#' .. hi_a .. '_' .. hi_b .. '#' .. lsep
local flag = true
local len = 0
for _, sec in ipairs(vim.tbl_filter(function(v)
return vim.fn.empty(v) == 0
end, vim.list_slice(left_sections, 2))) do
if M.check_width(len, sec, winwidth) then
if flag then
l = l .. '%#' .. hi_b .. '#' .. sec
l = l .. '%#' .. hi_b .. '_' .. hi_c .. '#' .. lsep
else
l = l .. '%#' .. hi_c .. '#' .. sec
l = l .. '%#' .. hi_c .. '_' .. hi_b .. '#' .. lsep
end
flag = not flag
end
end
l = string.sub(l, 1, #l - #lsep)
if #right_sections == 0 then
if flag then
return l .. '%#' .. hi_c .. '#'
else
return l .. '%#' .. hi_b .. '#'
end
end
if M.check_width(len, fname, winwidth) then
len = len + M.len(fname)
if flag then
l = l .. '%#' .. hi_c .. '_' .. hi_z .. '#' .. lsep .. '%#' .. hi_z .. '#' .. fname .. '%='
else
l = l .. '%#' .. hi_b .. '_' .. hi_z .. '#' .. lsep .. '%#' .. hi_z .. '#' .. fname .. '%='
end
else
if flag then
l = l .. '%#' .. hi_c .. '_' .. hi_z .. '#' .. lsep .. '%='
else
l = l .. '%#' .. hi_b .. '_' .. hi_z .. '#' .. lsep .. '%='
end
end
if M.check_width(len, tag, winwidth) and vim.g.spacevim_enable_statusline_tag == 1 then
l = l .. '%#' .. hi_z .. '#' .. tag
end
l = l .. '%#' .. hi_b .. '_' .. hi_z .. '#' .. rsep
flag = true
for _, sec in ipairs(vim.tbl_filter(function(v)
return vim.fn.empty(v) == 0
end, right_sections)) do
if M.check_width(len, sec, winwidth) then
len = len + M.len(sec)
if flag then
l = l .. '%#' .. hi_b .. '#' .. sec
l = l .. '%#' .. hi_c .. '_' .. hi_b .. '#' .. rsep
else
l = l .. '%#' .. hi_c .. '#' .. sec
l = l .. '%#' .. hi_b .. '_' .. hi_c .. '#' .. rsep
end
flag = not flag
end
end
l = string.sub(l, 1, #l - #rsep)
return l
end
return M

File diff suppressed because it is too large Load Diff