Skip to content

Commit

Permalink
feat: idle support
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Dec 30, 2024
1 parent c0df288 commit 4ea5390
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 110 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ You can configure state, details and git integration by changing Discord Presenc
"discord_presence": {
"initialization_options": {
// application id for the rich presence (required, keep it if you don't know what you're doing)
"application_id": "1263505205522337886"
"application_id": "1263505205522337886",
// Base url for all language icons
"base_icons_url": "https://raw.githubusercontent.com/xhyrom/zed-discord-presence/main/assets/icons/",

Expand All @@ -45,12 +45,21 @@ You can configure state, details and git integration by changing Discord Presenc
"small_image": "{base_icons_url}/zed.png",
"small_text": "Zed",

// Idle state
"idle": {
"timeout": 300, // in seconds, 300 seconds = 5 minutes
"state": "Idling",
"details": "In Zed",
"large_image": "{base_icons_url}/zed.png",
"large_text": "Zed",
"small_image": "{base_icons_url}/idle.png",
"small_text": "Idle",
}

// Rules - disable presence in some workspaces
"rules": {
"mode": "blacklist", // or whitelist
"paths": [
"absolute path"
]
"paths": ["absolute path"]
},

"git_integration": true
Expand Down
2 changes: 1 addition & 1 deletion lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
discord-rich-presence = "0.2.4"
tokio = { version = "1.37.0", features = ["rt-multi-thread", "io-std", "macros"] }
tokio = { version = "1.37.0", features = ["rt-multi-thread", "io-std", "macros", "time"] }
tower-lsp = "0.20.0"
git2 = { version = "0.19.0", default-features = false }
serde_json = { version = "1.0.122", features = ["std"] }
Expand Down
42 changes: 42 additions & 0 deletions lsp/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ impl Rules {
}
}

#[derive(Debug)]
pub struct Idle {
pub timeout: u64, // in seconds

pub state: Option<String>,
pub details: Option<String>,

pub large_image: Option<String>,
pub large_text: Option<String>,
pub small_image: Option<String>,
pub small_text: Option<String>,
}

impl Default for Idle {
fn default() -> Self {
Idle {
timeout: 300,

state: Some("Idling".to_string()),
details: Some("In Zed".to_string()),

large_image: Some(String::from("{base_icons_url}/zed.png")),
large_text: Some(String::from("Zed")),
small_image: Some(String::from("{base_icons_url}/idle.png")),
small_text: Some(String::from("Idle")),
}
}
}

#[derive(Debug)]
pub struct Configuration {
pub application_id: String,
Expand All @@ -67,6 +96,8 @@ pub struct Configuration {

pub rules: Rules,

pub idle: Idle,

pub git_integration: bool,
}

Expand Down Expand Up @@ -104,6 +135,7 @@ impl Configuration {
small_image: Some(String::from("{base_icons_url}/zed.png")),
small_text: Some(String::from("Zed")),
rules: Rules::default(),
idle: Idle::default(),
git_integration: true,
}
}
Expand Down Expand Up @@ -141,6 +173,16 @@ impl Configuration {
});
}

if let Some(idle) = options.get("idle") {
self.idle.timeout = idle.get("timeout").and_then(|t| t.as_u64()).unwrap_or(300);
set_option!(self, idle, state, "state");
set_option!(self, idle, details, "details");
set_option!(self, idle, large_image, "large_image");
set_option!(self, idle, large_text, "large_text");
set_option!(self, idle, small_image, "small_image");
set_option!(self, idle, small_text, "small_text");
}

if let Some(git_integration) = options.get("git_integration") {
self.git_integration = git_integration.as_bool().unwrap_or(true);
}
Expand Down
22 changes: 10 additions & 12 deletions lsp/src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

use std::{
sync::{Mutex, MutexGuard},
time::{Duration, SystemTime, UNIX_EPOCH},
};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::{Mutex, MutexGuard};

use discord_rich_presence::{
activity::{Activity, Assets, Button, Timestamps},
Expand Down Expand Up @@ -55,28 +53,28 @@ impl Discord {
self.client = Some(Mutex::new(discord_client));
}

pub fn connect(&self) {
let mut client = self.get_client();
pub async fn connect(&self) {
let mut client = self.get_client().await;
let result = client.connect();
result.unwrap();
}

pub fn kill(&self) {
let mut client = self.get_client();
pub async fn kill(&self) {
let mut client = self.get_client().await;
let result = client.close();
result.unwrap();
}

pub fn get_client(&self) -> MutexGuard<'_, DiscordIpcClient> {
pub async fn get_client(&self) -> MutexGuard<'_, DiscordIpcClient> {
self.client
.as_ref()
.expect("Discord client not initialized")
.lock()
.expect("Failed to lock discord client")
.await
}

#[allow(clippy::too_many_arguments)]
pub fn change_activity(
pub async fn change_activity(
&self,
state: Option<String>,
details: Option<String>,
Expand All @@ -86,7 +84,7 @@ impl Discord {
small_text: Option<String>,
git_remote_url: Option<String>,
) {
let mut client = self.get_client();
let mut client = self.get_client().await;
let timestamp: i64 = self.start_timestamp.as_millis() as i64;

let activity = Activity::new()
Expand Down
Loading

0 comments on commit 4ea5390

Please sign in to comment.