From 9725d4acc296c35be653fe7625a24679d2c6787e Mon Sep 17 00:00:00 2001 From: Aaron Lichtman Date: Sun, 1 Dec 2024 22:09:06 -0800 Subject: [PATCH] feat: add filepermissions component --- README.md | 15 ++++++++++ lua/lualine/components/filepermissions.lua | 32 ++++++++++++++++++++++ tests/spec/component_spec.lua | 24 ++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 lua/lualine/components/filepermissions.lua diff --git a/README.md b/README.md index 4c0399871..fb8542947 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,7 @@ sections = {lualine_a = {'mode'}} - `diff` (git diff status) - `encoding` (file encoding) - `fileformat` (file format) +- `filepermissions` - `filename` - `filesize` - `filetype` @@ -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 diff --git a/lua/lualine/components/filepermissions.lua b/lua/lualine/components/filepermissions.lua new file mode 100644 index 000000000..4600372ab --- /dev/null +++ b/lua/lualine/components/filepermissions.lua @@ -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 diff --git a/tests/spec/component_spec.lua b/tests/spec/component_spec.lua index 3311b95b1..9879bfe71 100644 --- a/tests/spec/component_spec.lua +++ b/tests/spec/component_spec.lua @@ -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 {