Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
refactor: separate provider options from component values
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The arguments for provider functions have been changed.
  • Loading branch information
famiu committed Sep 23, 2021
1 parent 1b973c9 commit 3f89bc9
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 143 deletions.
256 changes: 145 additions & 111 deletions README.md

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions lua/feline/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,18 @@ end
-- Parse component provider
local function parse_provider(provider, component, winid)
local icon
local opts = {}

if type(provider) == "string" and type(providers[provider]) == "function" then
provider, icon = providers[provider](component, winid)
-- If provider is a string and its name matches the name of a registered provider, use it
if type(provider) == "string" and providers[provider] then
provider, icon = providers[provider](winid, opts, component)
-- If provider is a function, just evaluate it normally
elseif type(provider) == "function" then
provider, icon = provider(component, winid)
provider, icon = provider(winid, opts, component)
-- If provider is a table, get the provider name and opts and evaluate the provider
elseif type(provider) == "table" then
opts = provider.opts
provider = providers[provider.name](winid, opts, component)
end

return provider, icon
Expand Down
6 changes: 3 additions & 3 deletions lua/feline/providers/cursor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ local api = vim.api

local M = {}

function M.position(_, winid)
function M.position(winid)
return string.format('%3d:%-2d', unpack(api.nvim_win_get_cursor(winid)))
end

function M.line_percentage(_, winid)
function M.line_percentage(winid)
local curr_line = api.nvim_win_get_cursor(winid)[1]
local lines = api.nvim_buf_line_count(api.nvim_win_get_buf(winid))

Expand All @@ -19,7 +19,7 @@ function M.line_percentage(_, winid)
end
end

function M.scroll_bar(_, winid)
function M.scroll_bar(winid)
local blocks = {'', '', '', '', '', '', '', ''}
local width = 2

Expand Down
31 changes: 15 additions & 16 deletions lua/feline/providers/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,23 @@ local function get_unique_filename(filename, shorten)
return string.reverse(string.sub(filename, 1, index))
end

function M.file_info(component, winid)
function M.file_info(winid, opts, component)
local filename = api.nvim_buf_get_name(api.nvim_win_get_buf(winid))
local type = opts.type or 'base-only'

component.type = component.type or 'base-only'

if component.type == 'short-path' then
if type == 'short-path' then
filename = fn.pathshorten(filename)
elseif component.type == 'base-only' then
elseif type == 'base-only' then
filename = fn.fnamemodify(filename, ':t')
elseif component.type == 'relative' then
elseif type == 'relative' then
filename = fn.fnamemodify(filename, ":~:.")
elseif component.type == 'relative-short' then
elseif type == 'relative-short' then
filename = fn.pathshorten(fn.fnamemodify(filename, ":~:."))
elseif component.type == 'unique' then
elseif type == 'unique' then
filename = get_unique_filename(filename)
elseif component.type == 'unique-short' then
elseif type == 'unique-short' then
filename = get_unique_filename(filename, true)
elseif component.type ~= 'full-path' then
elseif type ~= 'full-path' then
filename = fn.fnamemodify(filename, ':t')
end

Expand All @@ -104,7 +103,7 @@ function M.file_info(component, winid)

icon = { str = icon_str }

if component.colored_icon == nil or component.colored_icon then
if opts.colored_icon == nil or opts.colored_icon then
local fg = api.nvim_get_hl_by_name(icon_hlname, true).foreground

if fg then
Expand All @@ -118,13 +117,13 @@ function M.file_info(component, winid)
local bufnr = api.nvim_win_get_buf(winid)

if bo[bufnr].readonly then
readonly_str = component.file_readonly_icon or '🔒'
readonly_str = opts.file_readonly_icon or '🔒'
else
readonly_str = ''
end

if bo[bufnr].modified then
modified_str = (component.file_modified_icon or '')
modified_str = opts.file_modified_icon or ''

if modified_str ~= '' then modified_str = modified_str .. ' ' end
else
Expand All @@ -134,7 +133,7 @@ function M.file_info(component, winid)
return ' ' .. readonly_str .. filename .. ' ' .. modified_str, icon
end

function M.file_size(_, winid)
function M.file_size(winid)
local suffix = {'b', 'k', 'M', 'G', 'T', 'P', 'E'}
local index = 1

Expand All @@ -150,11 +149,11 @@ function M.file_size(_, winid)
return string.format(index == 1 and '%g' or '%.2f', fsize) .. suffix[index]
end

function M.file_type(_, winid)
function M.file_type(winid)
return bo[api.nvim_win_get_buf(winid)].filetype:upper()
end

function M.file_encoding(_, winid)
function M.file_encoding(winid)
local bufnr = api.nvim_win_get_buf(winid)
local enc = (bo[bufnr].fenc ~= '' and bo[bufnr].fenc) or vim.o.enc
return enc:upper()
Expand Down
8 changes: 4 additions & 4 deletions lua/feline/providers/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local api = vim.api

local M = {}

function M.git_branch(_, winid)
function M.git_branch(winid)
local ok, head = pcall(api.nvim_buf_get_var, api.nvim_win_get_buf(winid), 'gitsigns_head')

if not ok then head = g.gitsigns_head or '' end
Expand All @@ -21,15 +21,15 @@ local function git_diff(winid, type)
return ''
end

function M.git_diff_added(_, winid)
function M.git_diff_added(winid)
return git_diff(winid, 'added'), ''
end

function M.git_diff_removed(_, winid)
function M.git_diff_removed(winid)
return git_diff(winid, 'removed'), ''
end

function M.git_diff_changed(_, winid)
function M.git_diff_changed(winid)
return git_diff(winid, 'changed'), ''
end

Expand Down
10 changes: 5 additions & 5 deletions lua/feline/providers/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function M.diagnostics_exist(severity, bufnr)
return diagnostics_count and diagnostics_count > 0
end

function M.lsp_client_names(_, winid)
function M.lsp_client_names(winid)
local clients = {}

for _, client in pairs(lsp.buf_get_clients(api.nvim_win_get_buf(winid))) do
Expand All @@ -50,19 +50,19 @@ local function diagnostics(winid, severity)
return tostring(count)
end

function M.diagnostic_errors(_, winid)
function M.diagnostic_errors(winid)
return diagnostics(winid, 'Error'), ''
end

function M.diagnostic_warnings(_, winid)
function M.diagnostic_warnings(winid)
return diagnostics(winid, 'Warning'), ''
end

function M.diagnostic_hints(_, winid)
function M.diagnostic_hints(winid)
return diagnostics(winid, 'Hint'), ''
end

function M.diagnostic_info(_, winid)
function M.diagnostic_info(winid)
return diagnostics(winid, 'Information'), ''
end

Expand Down
2 changes: 1 addition & 1 deletion lua/feline/providers/vi_mode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function M.get_mode_highlight_name()
return 'StatusComponentVim' .. title_case(M.get_vim_mode())
end

function M.vi_mode(component)
function M.vi_mode(_, _, component)
if component.icon == '' then
return M.get_vim_mode()
elseif component.icon == nil then
Expand Down

0 comments on commit 3f89bc9

Please sign in to comment.