Skip to content

Commit 35aad95

Browse files
feat(ex.location): the new component to show cursor location
.
1 parent a1e2d7c commit 35aad95

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ for `lualine.nvim` with additional components.
1111
- [🧩 Provided components](#provided-components)
1212
- [ex.spellcheck](#exspellcheck)
1313
- [ex.cwd](#excwd)
14+
- [ex.location](#exlocation)
1415
- [ex.relative_filename](#exrelative_filename)
1516
- [ex.git.branch](#exgitbranch)
1617
- [ex.lsp.single](#exlspsingle)
@@ -137,6 +138,34 @@ sections = {
137138
The absolute value of the {depth} will be decreased until the length of the path becomes less then
138139
{max_length}.
139140

141+
### ex.location
142+
143+
This component shows the current cursor position in configurable format. Comparing to the default
144+
`location` component, this component can show total number of lines, and may be flexibly configured.
145+
146+
| pattern | example |
147+
|:---:|:---:|
148+
| `'%2C:%-3L/%T'` | <img height=18 alt="ex location" src="https://github.com/dokwork/lualine-ex/assets/6939832/743ffc33-a5f4-4f95-9204-e217fa9cdbf7"> |
149+
| `'%3L:%-2C'` | <img height=18 alt="ex location-2" src="https://github.com/dokwork/lualine-ex/assets/6939832/2e9dfa90-7363-4a99-a8d1-c1cc9033d5f7"> |
150+
151+
152+
```lua
153+
sections = {
154+
lualine_a = {
155+
{
156+
'ex.location',
157+
158+
-- The pattern to show the cursor position. Here three possible specifiers:
159+
-- 'L' means 'line' - the number of the line where is the cursor now;
160+
-- 'C' means 'column' - the number of the virtual column where is the cursor now;
161+
-- 'T' means 'total' - the total count of lines in the current buffer;
162+
-- Every specifier can be used in similar maner to %d in the {string.format} function.
163+
-- The pattern similar to the default 'location' component is '%3L:%-2C'
164+
pattern = '%2C:%-3L/%T'
165+
}
166+
}
167+
}
168+
```
140169

141170
### ex.relative_filename
142171

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local log = require('plenary.log').new({ plugin = 'ex.location' })
2+
3+
local Location = require('lualine.ex.component'):extend({
4+
pattern = '%2C:%-3L/%T',
5+
})
6+
7+
function Location:post_init()
8+
self.__substitutions = {}
9+
self.__pattern = string.gsub(self.options.pattern, '(%%%-?%d*[LCT]+)', function(template)
10+
return string.gsub(template, '([LCT])', function(value)
11+
table.insert(self.__substitutions, value)
12+
return 'd'
13+
end)
14+
end)
15+
log.debug(self.__substitutions)
16+
log.debug(self.__pattern)
17+
end
18+
19+
function Location:update_status()
20+
local values = {
21+
L = vim.fn.line('.'),
22+
C = vim.fn.virtcol('.'),
23+
T = vim.fn.line('$'),
24+
}
25+
local substitutions = vim.tbl_map(function(key)
26+
return values[key]
27+
end, self.__substitutions)
28+
return string.format(self.__pattern, unpack(substitutions))
29+
end
30+
31+
return Location

tests/components/location_spec.lua

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local l = require('tests.ex.lualine')
2+
local t = require('tests.ex.busted') --:ignore_all_tests()
3+
4+
local eq = assert.are.equal
5+
6+
local orig = {
7+
line = vim.fn.line,
8+
virtcol = vim.fn.virtcol,
9+
}
10+
local mock = {
11+
line = {},
12+
virtcol = {},
13+
}
14+
15+
local component_name = 'ex.location'
16+
describe(component_name, function()
17+
before_each(function()
18+
vim.fn.line = function(arg)
19+
return mock.line[arg]
20+
end
21+
vim.fn.virtcol = function(arg)
22+
return mock.virtcol[arg]
23+
end
24+
end)
25+
26+
after_each(function()
27+
vim.fn.line = orig.line
28+
vim.fn.virtcol = orig.virtcol
29+
end)
30+
31+
describe('default patter', function()
32+
it('should show {line}:{column}/{total}', function()
33+
mock.virtcol['.'] = 11
34+
mock.line['.'] = 222
35+
mock.line['$'] = 3333
36+
local component = l.render_component(component_name)
37+
eq('11:222/3333', component)
38+
end)
39+
it('should fill numbers by space', function()
40+
mock.virtcol['.'] = 1
41+
mock.line['.'] = 2
42+
mock.line['$'] = 3
43+
local component = l.render_component(component_name)
44+
eq(' 1:2 /3', component)
45+
end)
46+
end)
47+
end)

0 commit comments

Comments
 (0)