-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ex.progress): the new component to show the progress in differen…
…t modes .
- Loading branch information
1 parent
35aad95
commit efb0738
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
-- stylua: ignore start | ||
local progress_bar = { '█', '▇', '▆', '▅', '▄', '▃', '▂', '▁', ' ' } | ||
-- stylua: ignore end | ||
|
||
local Progress = require('lualine.ex.component'):extend({ | ||
mode = 'percent', | ||
top = 'Top', | ||
bottom = 'Bot', | ||
}) | ||
|
||
local function mode_percent(self, line, total) | ||
if (line == total) and self.options.bottom then | ||
return self.options.bottom | ||
end | ||
if (line == 1) and self.options.top then | ||
return self.options.top | ||
end | ||
return string.format('%3d%%%%', 99 * line / total + 1) | ||
end | ||
|
||
local function mode_table(t) | ||
return function(self, line, total) | ||
if (line == total) and self.options.bottom then | ||
return self.options.bottom | ||
end | ||
if (line == 1) and self.options.top then | ||
return self.options.top | ||
end | ||
local idx = math.floor(line * (#t - 1) / total) + 1 | ||
return t[idx] | ||
end | ||
end | ||
|
||
function Progress:post_init() | ||
if type(self.options.mode) == 'string' then | ||
self.options.mode = (self.options.mode == 'bar') and mode_table(progress_bar) | ||
or mode_percent | ||
end | ||
if type(self.options.mode) == 'table' then | ||
self.options.mode = mode_table(self.options.mode) | ||
end | ||
end | ||
|
||
function Progress:update_status() | ||
return self.options.mode(self, vim.fn.line('.'), vim.fn.line('$')) | ||
end | ||
|
||
return Progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
local l = require('tests.ex.lualine') | ||
local t = require('tests.ex.busted') --:ignore_all_tests() | ||
|
||
local eq = assert.are.equal | ||
|
||
local orig = { | ||
line = vim.fn.line, | ||
} | ||
local mock = { | ||
line = {}, | ||
} | ||
|
||
local component_name = 'ex.progress' | ||
|
||
describe(component_name, function() | ||
before_each(function() | ||
vim.fn.line = function(arg) | ||
return mock.line[arg] | ||
end | ||
end) | ||
|
||
after_each(function() | ||
vim.fn.line = orig.line | ||
end) | ||
|
||
describe('in "percent" (default) mode', function() | ||
it('should show "Top" when the cursor is on bottom of the buffer', function() | ||
-- { line, total, expected percent } | ||
local cases = { | ||
{ 1, 100, 'Top' }, | ||
{ 1, 1000, 'Top' }, | ||
{ 2, 1000, ' 1%%' }, | ||
} | ||
for _, case in ipairs(cases) do | ||
mock.line['.'] = case[1] | ||
mock.line['$'] = case[2] | ||
local c = l.render_component(component_name) | ||
eq(case[3], c, vim.inspect(case)) | ||
end | ||
end) | ||
it('should show "Bot" when the cursor is on bottom of the buffer', function() | ||
-- { line, total, expected percent } | ||
local cases = { | ||
{ 100, 100, 'Bot' }, | ||
{ 1000, 1000, 'Bot' }, | ||
{ 999, 1000, ' 99%%' }, | ||
} | ||
for _, case in ipairs(cases) do | ||
mock.line['.'] = case[1] | ||
mock.line['$'] = case[2] | ||
local c = l.render_component(component_name) | ||
eq(case[3], c, vim.inspect(case)) | ||
end | ||
end) | ||
it('should show expected persentage of the buffer', function() | ||
-- { line, total, expected percent } | ||
local cases = { | ||
{ 1, 100, 1 }, | ||
{ 100, 100, 100 }, | ||
{ 17, 111, 16 }, | ||
} | ||
for _, case in ipairs(cases) do | ||
mock.line['.'] = case[1] | ||
mock.line['$'] = case[2] | ||
local c = l.render_component(component_name, { top = false, bottom = false }) | ||
eq(string.format('%3d%%%%', case[3]), c, vim.inspect(case)) | ||
end | ||
end) | ||
end) | ||
|
||
describe('in "bar" mode, or "tabe" mode', function() | ||
it('should show appropriate symbol', function() | ||
-- { line, total, expected percent } | ||
local cases = { | ||
{ 1, 100, '█' }, | ||
{ 100, 100, ' ' }, | ||
{ 99, 100, '▁' }, | ||
{ 7, 9, '▂' }, | ||
{ 19, 31, '▄' }, | ||
} | ||
for _, case in ipairs(cases) do | ||
mock.line['.'] = case[1] | ||
mock.line['$'] = case[2] | ||
local c = l.render_component( | ||
component_name, | ||
{ top = false, bottom = false, mode = 'bar' } | ||
) | ||
eq(case[3], c, vim.inspect(case)) | ||
end | ||
end) | ||
end) | ||
end) |