From c2030611c8e31e1bc82fb20b6a5a08ecc3a420ba Mon Sep 17 00:00:00 2001 From: Kyle Chui <48545987+kylechui@users.noreply.github.com> Date: Thu, 20 May 2021 05:04:56 +0000 Subject: [PATCH] Dynamic Text Update (#15) * Add context-specific status * Add "browsing" support for file tree explorers * Move status text into its own function * Add commit and netrw support * Fix git commit file recognition * Add readonly clause * Revert editing_text option Co-authored-by: Andrew Kwon * Delete copied line * Dynamic text update (#2) * Add basic plugin support and rewrite status_text * Fix syntax error * Fix errors pt. 2 * Change options -> self.options * editing_text -> status_text * Try to get rid of dictionary bug * Try to fix dictionary bug pt. 2 * Remove self.options * Reorder status_text initialisation * Reinsert self.options * Remove global status_text var * options still broken * Tentatively done with dynamic text * Remove global status_text variable * Remove unnecessary lines/clean up code * Dynamic text update (#3) * Clean up status text options * General code cleanup * Fix whitespace issue Co-authored-by: Andrew Kwon --- lua/presence/init.lua | 43 +++++++++++++++++++------------- lua/presence/plugin_managers.lua | 5 ++++ 2 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 lua/presence/plugin_managers.lua diff --git a/lua/presence/init.lua b/lua/presence/init.lua index 4fb4034..2dff2ff 100644 --- a/lua/presence/init.lua +++ b/lua/presence/init.lua @@ -72,6 +72,7 @@ local serpent = require("deps.serpent") local Discord = require("presence.discord") local file_assets = require("presence.file_assets") local file_trees = require("presence.file_trees") +local plugin_managers = require("presence.plugin_managers") function Presence:setup(options) options = options or {} @@ -87,13 +88,19 @@ function Presence:setup(options) self.log:info("Using user-defined Discord client id") end + -- General options self:set_option("auto_update", 1) - self:set_option("main_image", "neovim") - self:set_option("editing_text", self.get_status_text) - self:set_option("workspace_text", "Working on %s") - self:set_option("neovim_image_text", "The One True Text Editor") self:set_option("client_id", "793271441293967371") self:set_option("debounce_timeout", 15) + self:set_option("main_image", "neovim") + self:set_option("neovim_image_text", "The One True Text Editor") + -- Status text options + self:set_option("editing_text", "Editing %s") + self:set_option("file_tree_text", "Browsing %s") + self:set_option("git_commit_text", "Committing changes") + self:set_option("plugin_manager_text", "Managing plugins") + self:set_option("reading_text", "Reading %s") + self:set_option("workspace_text", "Working on %s") local discord_socket = self:get_discord_socket() if not discord_socket then @@ -346,19 +353,24 @@ function Presence.get_file_extension(path) end -- Get the status text for the current buffer -function Presence.get_status_text(filename) +function Presence:get_status_text(filename) if vim.bo.modifiable and not vim.bo.readonly then if vim.bo.filetype == "gitcommit" then - return string.format("Committing changes") + return string.format(self.options.git_commit_text, filename) + else + return string.format(self.options.editing_text, filename) end - return string.format("Editing %s", filename) - elseif file_trees[filename:match "[^%d]+"] then - return string.format("Browsing %s", file_trees[filename:match "[^%d]+"]) else - if vim.bo.filetype == "netrw" then - return string.format("Browsing Netrw") + local file_tree = file_trees[filename:match "[^%d]+"] + if file_tree then + return string.format(self.options.file_tree_text, file_tree) + elseif vim.bo.filetype == "netrw" then + return string.format(self.options.file_tree_text, "Netrw") + elseif plugin_managers[vim.bo.filetype] then + return string.format(self.options.plugin_manager_text, filename) + else + return string.format(self.options.reading_text, filename) end - return string.format("Reading %s", filename) end end @@ -483,13 +495,10 @@ function Presence:update_for_buffer(buffer, should_debounce) small_text = use_file_as_main_image and neovim_image_text or file_text, } - local editing_text = self.options.editing_text - editing_text = type(editing_text) == "function" - and editing_text(filename, buffer) - or string.format(editing_text, filename) + local status_text = self:get_status_text(filename) local activity = { - state = editing_text, + state = status_text, assets = assets, timestamps = { start = activity_set_at, diff --git a/lua/presence/plugin_managers.lua b/lua/presence/plugin_managers.lua new file mode 100644 index 0000000..3789ba3 --- /dev/null +++ b/lua/presence/plugin_managers.lua @@ -0,0 +1,5 @@ +-- Different plugin manager names +return { + ["packer"] = "packer", + ["vim-plug"] = "vim-plug", +}