From 7527afca38d5de7b30d90dba27eafbd01505f703 Mon Sep 17 00:00:00 2001 From: vyfor Date: Sun, 12 Jan 2025 15:48:59 +0500 Subject: [PATCH] feat(manager): allow disabling text categories via boolean values --- .github/wiki/Configuration.md | 53 +++++++++++++++++++--------- .github/wiki/Examples.md | 4 +++ lua/cord/plugin/activity/init.lua | 22 +++++++++--- lua/cord/plugin/activity/manager.lua | 3 ++ lua/cord/plugin/config/init.lua | 30 ++++++++-------- 5 files changed, 75 insertions(+), 37 deletions(-) diff --git a/.github/wiki/Configuration.md b/.github/wiki/Configuration.md index 9bd2116b..4230f4d0 100644 --- a/.github/wiki/Configuration.md +++ b/.github/wiki/Configuration.md @@ -135,22 +135,22 @@ require('cord').setup { ## 📝 Text -| Option | Type | Default | Description | -| ---------------- | -------------------------- | --------------------------------- | -------------------------------------------------- | -| `workspace` | `string \| function(opts)` | `In {workspace_name}` | Text shown when in a workspace | -| `viewing` | `string \| function(opts)` | `Viewing {filename}` | Text shown when viewing a file | -| `editing` | `string \| function(opts)` | `Editing {filename}` | Text shown when editing a file | -| `file_browser` | `string \| function(opts)` | `Browsing files in {tooltip}` | Text shown when in a file browser | -| `plugin_manager` | `string \| function(opts)` | `Managing plugins in {tooltip}` | Text shown when in a plugin manager | -| `lsp` | `string \| function(opts)` | `Configuring LSP in {tooltip}` | Text shown when in an LSP manager | -| `docs` | `string \| function(opts)` | `Reading {tooltip}` | Text shown when in a docs buffer | -| `vcs` | `string \| function(opts)` | `Committing changes in {tooltip}` | Text shown when in a VCS buffer | -| `notes` | `string \| function(opts)` | `Taking notes in {tooltip}` | Text shown when in a notes buffer | -| `debug` | `string \| function(opts)` | `Debugging in {tooltip}` | Text shown when in a debug-related plugin buffer | -| `test` | `string \| function(opts)` | `Testing in {tooltip}` | Text shown when in a testing-related plugin buffer | -| `diagnostics` | `string \| function(opts)` | `Fixing problems in {tooltip}` | Text shown when in a diagnostics buffer | -| `games` | `string \| function(opts)` | `Playing {tooltip}` | Text shown when in a game buffer | -| `dashboard` | `string` | `Home` | Text shown when in a dashboard buffer | +| Option | Type | Default | Description | +| ---------------- | ------------------------------------- | --------------------------------- | -------------------------------------------------- | +| `workspace` | `string \| function(opts) \| boolean` | `In {workspace_name}` | Text shown when in a workspace | +| `viewing` | `string \| function(opts) \| boolean` | `Viewing {filename}` | Text shown when viewing a file | +| `editing` | `string \| function(opts) \| boolean` | `Editing {filename}` | Text shown when editing a file | +| `file_browser` | `string \| function(opts) \| boolean` | `Browsing files in {tooltip}` | Text shown when in a file browser | +| `plugin_manager` | `string \| function(opts) \| boolean` | `Managing plugins in {tooltip}` | Text shown when in a plugin manager | +| `lsp` | `string \| function(opts) \| boolean` | `Configuring LSP in {tooltip}` | Text shown when in an LSP manager | +| `docs` | `string \| function(opts) \| boolean` | `Reading {tooltip}` | Text shown when in a docs buffer | +| `vcs` | `string \| function(opts) \| boolean` | `Committing changes in {tooltip}` | Text shown when in a VCS buffer | +| `notes` | `string \| function(opts) \| boolean` | `Taking notes in {tooltip}` | Text shown when in a notes buffer | +| `debug` | `string \| function(opts) \| boolean` | `Debugging in {tooltip}` | Text shown when in a debug-related plugin buffer | +| `test` | `string \| function(opts) \| boolean` | `Testing in {tooltip}` | Text shown when in a testing-related plugin buffer | +| `diagnostics` | `string \| function(opts) \| boolean` | `Fixing problems in {tooltip}` | Text shown when in a diagnostics buffer | +| `games` | `string \| function(opts) \| boolean` | `Playing {tooltip}` | Text shown when in a game buffer | +| `dashboard` | `string \| function(opts) \| boolean` | `'Home'` | `Home` | Text shown when in a dashboard buffer | > Also see [Text Options](#text-options) @@ -228,7 +228,26 @@ text = { } ``` -To see all available options, refer to the [default configuration](#default-config). +> To see all available options, refer to the [default configuration](#default-config). + +It is also possible to use boolean values to completely disable a category: + +```lua +text = { + workspace = '', -- Omit the text from the activity, meaning it will only have one row of text + games = function() end, -- Returning `nil` is the same as above + + file_browser = true, -- Ignore these types of buffers and the current activity will remain unchanged + plugin_manager = false, -- Hide the activity for these types of buffers + + -- Also applicable to functions + diagnostics = function(opts) + -- Only show diagnostics activity if there are problems, otherwise do nothing + return #vim.diagnostics.get(vim.api.nvim_get_current_buf()) > 0 and 'Fixing problems in ' .. opts.tooltip or true + end, +} +``` + ### Buttons diff --git a/.github/wiki/Examples.md b/.github/wiki/Examples.md index 91f6eb39..765b4814 100644 --- a/.github/wiki/Examples.md +++ b/.github/wiki/Examples.md @@ -35,7 +35,11 @@ text = { ### Omitting Fields ```lua text = { + workspace = '', + -- or workspace = function() end, + -- or + workspace = function() return '' end } ``` diff --git a/lua/cord/plugin/activity/init.lua b/lua/cord/plugin/activity/init.lua index 6ef9744c..31f12d1a 100644 --- a/lua/cord/plugin/activity/init.lua +++ b/lua/cord/plugin/activity/init.lua @@ -37,6 +37,7 @@ local text_config = { dashboard = config.text.dashboard, } +---@return Activity|boolean local function build_activity(opts) if opts.filetype == '' then if opts.filename == '' then @@ -73,22 +74,32 @@ local function build_activity(opts) else if opts.type == 'language' then if opts.is_read_only then - file_text = config_utils:get(text_config.viewing, opts) + local text = config_utils:get(text_config.viewing, opts) + if text == true or text == false then return text end + if text ~= '' then file_text = text end else - file_text = config_utils:get(text_config.editing, opts) + local text = config_utils:get(text_config.editing, opts) + if text == true or text == false then return text end + if text ~= '' then file_text = text end end else - file_text = config_utils:get(text_config[opts.type], opts) + local text = config_utils:get(text_config[opts.type], opts) + if text == true or text == false then return text end + if text ~= '' then file_text = text end end end local details, state if config.display.swap_fields then - details = config_utils:get(text_config.workspace, opts) + local workspace = config_utils:get(text_config.workspace, opts) + if workspace == true or workspace == false then return workspace end + if workspace ~= '' then details = workspace end state = file_text else + local workspace = config_utils:get(text_config.workspace, opts) + if workspace == true or workspace == false then return workspace end + if workspace ~= '' then state = workspace end details = file_text - state = config_utils:get(text_config.workspace, opts) end local large_image, large_text, small_image, small_text @@ -123,6 +134,7 @@ local function build_activity(opts) } end +---@return Activity local function build_idle_activity(opts) local details, state if config.display.swap_fields then diff --git a/lua/cord/plugin/activity/manager.lua b/lua/cord/plugin/activity/manager.lua index aae01ffc..52c2053f 100644 --- a/lua/cord/plugin/activity/manager.lua +++ b/lua/cord/plugin/activity/manager.lua @@ -233,7 +233,10 @@ function ActivityManager:update_activity() if self.config.hooks.on_update then self.config.hooks.on_update(self.opts) end local activity = activities.build_activity(self.opts) + if activity == true then return end + if activity == false then return self:clear_activity() end + ---@diagnostic disable-next-line: param-type-mismatch -- ensured to be of type Activity if self.config.hooks.on_activity then self.config.hooks.on_activity(self.opts, activity) end if self.should_skip_update then diff --git a/lua/cord/plugin/config/init.lua b/lua/cord/plugin/config/init.lua index d924f821..5e278f4f 100644 --- a/lua/cord/plugin/config/init.lua +++ b/lua/cord/plugin/config/init.lua @@ -23,23 +23,23 @@ ---@field details? string|fun(opts: CordOpts):string Details shown when idle ---@field state? string|fun(opts: CordOpts):string State shown when idle ---@field tooltip? string|fun(opts: CordOpts):string Tooltip shown when hovering over idle icon ----@field icon? string Idle icon +---@field icon? string|fun(opts: CordOpts):string Idle icon ---@class CordTextConfig ----@field workspace? string|fun(opts: CordOpts):string Text for workspace activity ----@field viewing? string|fun(opts: CordOpts):string Text for viewing activity ----@field editing? string|fun(opts: CordOpts):string Text for editing activity ----@field file_browser? string|fun(opts: CordOpts):string Text for file browser activity ----@field plugin_manager? string|fun(opts: CordOpts):string Text for plugin manager activity ----@field lsp? string|fun(opts: CordOpts):string Text for LSP manager activity ----@field docs? string|fun(opts: CordOpts):string Text for documentation activity ----@field vcs? string|fun(opts: CordOpts):string Text for VCS activity ----@field notes? string|fun(opts: CordOpts):string Text for notes activity ----@field debug? string|fun(opts: CordOpts):string Text for debugging-related plugin activity ----@field test? string|fun(opts: CordOpts):string Text for testing-related plugin activity ----@field games? string|fun(opts: CordOpts):string Text for games activity ----@field diagnostics? string|fun(opts: CordOpts):string Text for diagnostics activity ----@field dashboard? string|fun(opts: CordOpts):string Text for dashboard activity +---@field workspace? string|fun(opts: CordOpts):string|boolean Text for workspace activity +---@field viewing? string|fun(opts: CordOpts):string|boolean Text for viewing activity +---@field editing? string|fun(opts: CordOpts):string|boolean Text for editing activity +---@field file_browser? string|fun(opts: CordOpts):string|boolean Text for file browser activity +---@field plugin_manager? string|fun(opts: CordOpts):string|boolean Text for plugin manager activity +---@field lsp? string|fun(opts: CordOpts):string|boolean Text for LSP manager activity +---@field docs? string|fun(opts: CordOpts):string|boolean Text for documentation activity +---@field vcs? string|fun(opts: CordOpts):string|boolean Text for VCS activity +---@field notes? string|fun(opts: CordOpts):string|boolean Text for notes activity +---@field debug? string|fun(opts: CordOpts):string|boolean Text for debugging-related plugin activity +---@field test? string|fun(opts: CordOpts):string|boolean Text for testing-related plugin activity +---@field games? string|fun(opts: CordOpts):string|boolean Text for games activity +---@field diagnostics? string|fun(opts: CordOpts):string|boolean Text for diagnostics activity +---@field dashboard? string|fun(opts: CordOpts):string|boolean Text for dashboard activity ---@class CordButtonConfig ---@field label string|fun(opts: CordOpts):string? Button label