Skip to content

Commit 9725d4a

Browse files
committed
feat: add filepermissions component
1 parent 2a5bae9 commit 9725d4a

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ sections = {lualine_a = {'mode'}}
257257
- `diff` (git diff status)
258258
- `encoding` (file encoding)
259259
- `fileformat` (file format)
260+
- `filepermissions`
260261
- `filename`
261262
- `filesize`
262263
- `filetype`
@@ -630,6 +631,20 @@ sections = {
630631
}
631632
```
632633

634+
#### filepermissions component options
635+
636+
```lua
637+
sections = {
638+
lualine_a = {
639+
{
640+
'filepermissions',
641+
octal = false, -- Displays file permissions in octal format if set to true
642+
}
643+
}
644+
}
645+
}
646+
```
647+
633648
#### filename component options
634649

635650
```lua
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- Copyright (c) 2020-2021 shadmansaleh
2+
-- MIT license, see LICENSE for more details.
3+
local M = require('lualine.component'):extend()
4+
local bit = require('bit')
5+
6+
-- Initializer
7+
function M:init(options)
8+
-- Run super()
9+
M.super.init(self, options)
10+
end
11+
12+
-- Function that runs every time statusline is updated
13+
function M:update_status()
14+
local curr_filepath = vim.fn.expand('%')
15+
if curr_filepath == '' then
16+
return ''
17+
end
18+
19+
if not self.options.octal then
20+
return vim.fn.getfperm(curr_filepath)
21+
else
22+
local stat_results = vim.uv.fs_stat(curr_filepath)
23+
if stat_results == nil then
24+
return ''
25+
end
26+
local bitmask = 0b111111111
27+
local octal_perm = bit.band(stat_results.mode, bitmask)
28+
return string.format('o%o', octal_perm)
29+
end
30+
end
31+
32+
return M

tests/spec/component_spec.lua

+24
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,30 @@ describe('FileSize component', function()
585585
end)
586586
end)
587587

588+
describe('Filepermissions componnet', function()
589+
it('can show octal permissions', function()
590+
local opts = build_component_opts {
591+
octal = true,
592+
}
593+
vim.cmd(':e test-file.txt')
594+
assert_component('filepermissions', opts, '')
595+
vim.cmd(':w')
596+
vim.fn.setfperm("test-file.txt", "rwxrwxrwx")
597+
assert_component('filename', opts, 'o777')
598+
end)
599+
600+
it('can show regular permissions', function()
601+
local opts = build_component_opts {
602+
octal = false,
603+
}
604+
vim.cmd(':e test-file.txt')
605+
assert_component('filepermissions', opts, '')
606+
vim.cmd(':w')
607+
vim.fn.setfperm("test-file.txt", "rwxrwxrwx")
608+
assert_component('filename', opts, 'rwxrwxrwx')
609+
end)
610+
end)
611+
588612
describe('Filename component', function()
589613
it('works', function()
590614
local opts = build_component_opts {

0 commit comments

Comments
 (0)