Skip to content

Commit

Permalink
feat: add filepermissions component
Browse files Browse the repository at this point in the history
  • Loading branch information
alichtman committed Dec 2, 2024
1 parent 2a5bae9 commit 0d5f3b7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/lualine.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ CHANGING COMPONENTS IN LUALINE SECTIONS ~
- `encoding` (file encoding)
- `fileformat` (file format)
- `filename`
- `filepermissions`
- `filesize`
- `filetype`
- `hostname`
Expand Down Expand Up @@ -685,6 +686,17 @@ Component specific options These are options that are available on
}
<

*lualine-filepermissions-component-options*

>
sections = {
lualine_a = {
{
'filepermissions',
octal = true
}
}
}
*lualine-filetype-component-options*
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

0 comments on commit 0d5f3b7

Please sign in to comment.