Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add filepermissions component #1341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ sections = {lualine_a = {'mode'}}
- `diff` (git diff status)
- `encoding` (file encoding)
- `fileformat` (file format)
- `filepermissions`
- `filename`
- `filesize`
- `filetype`
Expand Down Expand Up @@ -630,6 +631,20 @@ sections = {
}
```

#### filepermissions component options

```lua
sections = {
lualine_a = {
{
'filepermissions',
octal = false, -- Displays file permissions in octal format if set to true
}
}
}
}
```

#### filename component options

```lua
Expand Down
32 changes: 32 additions & 0 deletions lua/lualine/components/filepermissions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Copyright (c) 2020-2021 shadmansaleh
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend()
local bit = require('bit')

-- Initializer
function M:init(options)
-- Run super()
M.super.init(self, options)
end

-- Function that runs every time statusline is updated
function M:update_status()
local curr_filepath = vim.fn.expand('%')
if curr_filepath == '' then
return ''
end

if not self.options.octal then
return vim.fn.getfperm(curr_filepath)
else
local stat_results = vim.uv.fs_stat(curr_filepath)
if stat_results == nil then
return ''
end
local bitmask = 0b111111111
local octal_perm = bit.band(stat_results.mode, bitmask)
return string.format('o%o', octal_perm)
end
end

return M
24 changes: 24 additions & 0 deletions tests/spec/component_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,30 @@ describe('FileSize component', function()
end)
end)

describe('Filepermissions componnet', function()
it('can show octal permissions', function()
local opts = build_component_opts {
octal = true,
}
vim.cmd(':e test-file.txt')
assert_component('filepermissions', opts, '')
vim.cmd(':w')
vim.fn.setfperm("test-file.txt", "rwxrwxrwx")
assert_component('filename', opts, 'o777')
end)

it('can show regular permissions', function()
local opts = build_component_opts {
octal = false,
}
vim.cmd(':e test-file.txt')
assert_component('filepermissions', opts, '')
vim.cmd(':w')
vim.fn.setfperm("test-file.txt", "rwxrwxrwx")
assert_component('filename', opts, 'rwxrwxrwx')
end)
end)

describe('Filename component', function()
it('works', function()
local opts = build_component_opts {
Expand Down
Loading