diff --git a/README.md b/README.md index ba9558de..8590d718 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,12 @@ - Lightweight and dependency-free - Cross-platform support (Windows, Linux, macOS) - Blazingly fast startup due to non-blocking, asynchronous nature -- Highly [configurable](https://github.com/vyfor/cord.nvim#-configuration) in Lua +- Highly [configurable](#-configuration) in Lua - Offers a rich icon set for various components - Automatically detects working directory and repository based on Git - Identifies problems across active buffers - Supports configurable idle status detection +- Provides [user commands](#%EF%B8%8F-user-commands) for managing the presence - Is written in native code and uses Lua FFI for integration ## 🔌 Requirements @@ -104,7 +105,7 @@ require('cord').setup({ }) ``` -### ⌨️ User commands (WIP) +### ⌨️ User commands - `:CordConnect` - Initialize presence client internally and connect to Discord - `:CordReconnect` - Reconnect to Discord - `:CordDisconnect` - Disconnect from Discord diff --git a/lua/cord.lua b/lua/cord.lua index 460a78a4..7d31b2f3 100644 --- a/lua/cord.lua +++ b/lua/cord.lua @@ -57,7 +57,7 @@ local last_updated = os.clock() local last_presence local function connect(config) - return discord.init( + discord.init( config.editor.client, config.editor.image, config.editor.tooltip, @@ -127,6 +127,7 @@ local function update_presence(config) end local function start_timer(config) + timer:stop() if vim.g.cord_started == nil then vim.g.cord_started = true if not utils.validate_severity(config) then return end @@ -136,7 +137,6 @@ local function start_timer(config) discord.update_time() end end - timer:stop() timer:start(0, config.timer.interval, vim.schedule_wrap(function() update_presence(config) end)) end @@ -147,13 +147,9 @@ function cord.setup(userConfig) local work = vim.loop.new_async(vim.schedule_wrap(function() discord = utils.init_discord(ffi) - local err = connect(config) - if err ~= nil then - vim.api.nvim_err_writeln('[cord.nvim] Caught unexpected error: ' .. ffi.string(err)) - else - if config.timer.enable then - start_timer(config) - end + connect(config) + if config.timer.enable then + start_timer(config) end vim.api.nvim_create_autocmd('ExitPre', { callback = function() discord.disconnect() end }) @@ -172,21 +168,15 @@ end function cord.setup_usercmds(config) vim.api.nvim_create_user_command('CordConnect', function() - local err = connect(config) - if err ~= nil then - vim.api.nvim_err_writeln('[cord.nvim] Caught unexpected error: ' .. ffi.string(err)) - end + connect(config) start_timer(config) end, {}) vim.api.nvim_create_user_command('CordReconnect', function() timer:stop() discord.disconnect() - local err = connect(config) - if err ~= nil then - enabled = false - vim.api.nvim_err_writeln('[cord.nvim] Caught unexpected error: ' .. ffi.string(err)) - end + last_presence = nil + connect(config) start_timer(config) enabled = true end, {}) @@ -195,6 +185,7 @@ function cord.setup_usercmds(config) timer:stop() discord.disconnect() enabled = false + last_presence = nil end, {}) vim.api.nvim_create_user_command('CordTogglePresence', function() @@ -202,13 +193,13 @@ function cord.setup_usercmds(config) timer:stop() discord.clear_presence() enabled = false + last_presence = nil else start_timer(config) enabled = true end end, {}) - -- user commands for showing and hiding the presence vim.api.nvim_create_user_command('CordShowPresence', function() start_timer(config) enabled = true @@ -218,6 +209,7 @@ function cord.setup_usercmds(config) timer:stop() discord.clear_presence() enabled = false + last_presence = nil end, {}) end diff --git a/src/ipc/platform/unix_connection.rs b/src/ipc/platform/unix_connection.rs index ae39a1f5..db9e83b0 100644 --- a/src/ipc/platform/unix_connection.rs +++ b/src/ipc/platform/unix_connection.rs @@ -4,6 +4,7 @@ use std::os::unix::net::UnixStream; use crate::ipc::client::{Connection, RichClient}; use crate::ipc::utils; +use crate::rpc::packet::Packet; impl Connection for RichClient { fn connect(client_id: u64) -> Result> { diff --git a/src/json/serialize.rs b/src/json/serialize.rs index 0d247c7d..b08c7238 100644 --- a/src/json/serialize.rs +++ b/src/json/serialize.rs @@ -16,14 +16,12 @@ impl Packet { json_str.push_str(",\"args\":{"); write!(&mut json_str, "\"pid\":{}", self.pid)?; - if let Some(activity) = &self.activity { - json_str.push_str(",\"activity\":{"); + json_str.push_str(",\"activity\":"); activity.push_json(&mut json_str)?; - json_str.push('}'); } - json_str.push_str("}"); + json_str.push_str("}}"); Ok(json_str) } @@ -31,7 +29,7 @@ impl Packet { impl Activity { pub fn push_json(&self, json_str: &mut String) -> Result<(), Error> { - json_str.push_str("\"type\":0"); + json_str.push_str("{\"type\":0"); if let Some(timestamp) = &self.timestamp { write!(json_str, ",\"timestamps\":{{\"start\":{}}}", timestamp)?; @@ -93,5 +91,3 @@ impl Activity { Ok(()) } } - - diff --git a/src/lib.rs b/src/lib.rs index be22929b..a33b8062 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -275,6 +275,7 @@ pub extern "C" fn disconnect() { } if let Some(mut client) = RICH_CLIENT.take() { client.close().expect("Failed to close connection"); + INITIALIZED = false; } } }