From 0d5f3b7081a0a86c48ec5c466ffcce1051219f26 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 --- doc/lualine.txt | 12 ++++++++ lua/lualine/components/filepermissions.lua | 32 ++++++++++++++++++++++ tests/spec/component_spec.lua | 24 ++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 lua/lualine/components/filepermissions.lua diff --git a/doc/lualine.txt b/doc/lualine.txt index ccc6ebede..0b1fca2a1 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -258,6 +258,7 @@ CHANGING COMPONENTS IN LUALINE SECTIONS ~ - `encoding` (file encoding) - `fileformat` (file format) - `filename` +- `filepermissions` - `filesize` - `filetype` - `hostname` @@ -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* 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 {